📄 533-539.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Building the JAVAroids Game</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></script><SCRIPT><!--function popUp(url) { var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) { /* get the query value */ var i = escape(fm.query.value); if (i == "") { alert('Please enter a search word or phrase'); return false; } /* query is blank, dont run the .jsp file */ else return true; /* execute the .jsp file */}//--></script></HEAD><BODY>
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
<font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
<br>
<font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
<br>
Sams, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</td>
</tr>
</table>
<P>
<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=13//-->
<!--PAGES=533-539//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="530-533.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="539-543.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading16"></A><FONT COLOR="#000077">Building the Game Sprites</FONT></H3>
<P>There are five types of sprites in JAVAroids:
</P>
<DL>
<DD><B>•</B> Enemy: the enemy ship
<DD><B>•</B> Fire: the fire from the enemy ship or the player’s ship
<DD><B>•</B> Asteroid: the colored, rotating slab of rock
<DD><B>•</B> Ship: what the player controls
<DD><B>•</B> Explosion: the result of a collision
</DL>
<P>Figure 13-13 shows what these sprites look like, and Figure 13-14 shows where the Sprite classes will inherit from.
</P>
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/13-13.jpg',520,636 )"><IMG SRC="images/13-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-13.jpg',520,636)"><FONT COLOR="#000077"><B>Figure 13-13</B></FONT></A> JAVAroids sprites</P>
<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/13-14.jpg',600,750 )"><IMG SRC="images/13-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-14.jpg',600,750)"><FONT COLOR="#000077"><B>Figure 13-14</B></FONT></A> Inheritance hierarchy for JAVAroids Sprite classes</P>
<P>Let’s define these sprites, one by one.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">The Enemy Sprite</FONT></H4>
<P>The Enemy sprite, shown in Listing 13-8, will be a MoveablePolygon. In addition to all the functionality from its parent, the Enemy will also track its <I>value</I> (how much it’s worth when it’s hit). The Enemy contains methods to determine if an intersection with a Ship or Fire sprite has occurred.</P>
<P><B>Listing 13-8</B> Enemy class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Enemy sprite class
//
/////////////////////////////////////////////////////////////////
public class Enemy extends MoveablePolygon {
public int value; // value of this enemy
public Enemy(int tx[], int ty[], int n,
int centerx, int centery, Color c,
int w, int h, int r,int v) {
super(tx,ty,n,centerx,centery,c,w,h,r);
value = v;
setFill(false); // don't fill polygon
}
/////////////////////////////////////////////////////////////////
// intersection routines
/////////////////////////////////////////////////////////////////
// check for intersection with fire:
// compute midpt of the fire
// and see if it's within the max_radius of this enemy
public boolean intersect(Fire f) {
if (isActive() && f.isActive()) {
int midptx = (f.p.xpoints[1] + f.p.xpoints[0]) / 2;
int midpty = (f.p.ypoints[1] + f.p.ypoints[0]) / 2;
return (Math.abs(midptx - locx) < max_radius) &&
(Math.abs(midpty - locy) < max_radius);
}
else
return false;
}
// check for intersection with Ship
// see if ship's center is within max_radius of this
// asteroid, with 2 pixels of leeway on each side
public boolean intersect(Ship s) {
return isActive() && s.isActive() &&
(Math.abs(s.locx - locx+2) < max_radius) &&
(Math.abs(s.locy - locy+2) < max_radius) ;
}
}
</PRE>
<!-- END CODE //-->
<P>The intersection algorithm is simple, but sufficient. If the center of the Fire sprite or the Ship sprite is within <I>max_radius</I> pixels of the Enemy’s center, we’ll say there’s an intersection. Thus, collisions occur only when there are direct hits to the middle of the Enemy sprite. Figure 13-15 illustrates when a collision with the Enemy sprite is detected.</P>
<P><A NAME="Fig15"></A><A HREF="javascript:displayWindow('images/13-15.jpg',461,461 )"><IMG SRC="images/13-15t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-15.jpg',461,461)"><FONT COLOR="#000077"><B>Figure 13-15</B></FONT></A> Enemy sprite collision detection</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">The Fire Sprite</FONT></H4>
<P>The Fire class, shown in Listing 13-9, also derives from MoveablePolygon, and in the current implementation, it’s a straight line (i.e., polygon with two points). To start a Fire sprite in motion at a given location (<I>x,y</I>), and an angle <I>a</I>, use the instance method initialize(x,y,a). The Fire sprite has a finite lifetime when it’s active, and each time it’s updated, <I>count</I> is incremented. When <I>count</I> hits a threshold, the Fire sprite automatically suspends. In this way, the fire from an enemy or player’s ship stops after traveling a certain distance.</P>
<P><B>Listing 13-9</B> Fire class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Fire sprite class: this sprite automatically after a
// certain number of updates
//
/////////////////////////////////////////////////////////////////
public class Fire extends MoveablePolygon {
// fire is a polygon with two points, i. e. a straight line
static final int firex[] = { 0, 0};
static final int firey[] = { 0, 0};
// length of the fire sprite
static final int DEFAULT_LENGTH = 23;
int fire_length = DEFAULT_LENGTH;
// fire sprite lasts for this many updates
int max_updates = 14;
// count the number of updates
int count = 0;
/////////////////////////////////////////////////////////////////
// Fire sprite constructors
/////////////////////////////////////////////////////////////////
public Fire(Color c,int w, int h ) {
super(firex,firey,2,
0,0,c,
w,h,DEFAULT_LENGTH);
setFill(false);
}
public Fire(Color c,int w, int h,int length,int updates ) {
super(firex,firey,2,0,0,c,w,h,length);
fire_length = length;
max_updates = updates;
setFill(false);
}
/////////////////////////////////////////////////////////////////
//initialize a fire sprite from x,y at specified angle
/////////////////////////////////////////////////////////////////
public void initialize(int x, int y, int angle) {
tx[1] = (int)Math.round((fire_length*GameMath.cos(angle)));
ty[1] = (int)Math.round((fire_length*GameMath.sin(angle)));
setPosition(x,y);
setVelocity(tx[1],ty[1]);
count = 0;
restore();
}
/////////////////////////////////////////////////////////////////
// update fire sprite: bump counter, suspend sprite if
// counter's high enough
/////////////////////////////////////////////////////////////////
public void update() {
if (isActive()) {
count++;
if (count == max_updates) {
suspend();
}
else
super.update();
}
}
// check if fire intersects the ship
public boolean intersect(Ship s) {
return isActive() && s.isActive() &&
(Math.abs(s.locx - locx+2) < max_radius) &&
(Math.abs(s.locy - locy+2) < max_radius) ;
}
}
</PRE>
<!-- END CODE //-->
<P>There’s also a method that checks if the Fire sprite hits the player’s ship; this is analogous to the intersection routine used by Enemy.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="530-533.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="539-543.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -