⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 533-539.html

📁 java game programming e-book
💻 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,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</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>&#149;</B>&nbsp;&nbsp;Enemy: the enemy ship
<DD><B>&#149;</B>&nbsp;&nbsp;Fire: the fire from the enemy ship or the player&#146;s ship
<DD><B>&#149;</B>&nbsp;&nbsp;Asteroid: the colored, rotating slab of rock
<DD><B>&#149;</B>&nbsp;&nbsp;Ship: what the player controls
<DD><B>&#149;</B>&nbsp;&nbsp;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>&nbsp;&nbsp;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>&nbsp;&nbsp;Inheritance hierarchy for JAVAroids Sprite classes</P>
<P>Let&#146;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&#146;s worth when it&#146;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 &#123;
  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) &#123;
    super(tx,ty,n,centerx,centery,c,w,h,r);
    value = v;
    setFill(false);  // don't fill polygon
  &#125;

/////////////////////////////////////////////////////////////////
// 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) &#123;
    if (isActive() &#38;&#38; f.isActive()) &#123;
      int midptx = (f.p.xpoints[1] &#43; f.p.xpoints[0]) / 2;
      int midpty = (f.p.ypoints[1] &#43; f.p.ypoints[0]) / 2;
      return (Math.abs(midptx - locx) &lt; max_radius) &#38;&#38;
       (Math.abs(midpty - locy) &lt; max_radius);
    &#125;
    else
      return false;
  &#125;

  // 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) &#123;
    return isActive() &#38;&#38; s.isActive() &#38;&#38;
      (Math.abs(s.locx - locx&#43;2) &lt; max_radius) &#38;&#38;
       (Math.abs(s.locy - locy&#43;2) &lt; max_radius) ;
  &#125;

&#125;
</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&#146;s center, we&#146;ll say there&#146;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>&nbsp;&nbsp;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&#146;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&#146;s active, and each time it&#146;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&#146;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 &#123;

  // fire is a polygon with two points, i. e. a straight line
  static final int firex[] = &#123; 0, 0&#125;;
  static final int firey[] = &#123; 0, 0&#125;;

  // 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 ) &#123;
    super(firex,firey,2,
         0,0,c,
         w,h,DEFAULT_LENGTH);
    setFill(false);
  &#125;

  public Fire(Color c,int w, int h,int length,int updates ) &#123;
    super(firex,firey,2,0,0,c,w,h,length);
    fire_length = length;
    max_updates = updates;
    setFill(false);
  &#125;

/////////////////////////////////////////////////////////////////
//initialize a fire sprite from x,y at specified angle
/////////////////////////////////////////////////////////////////

  public void initialize(int x, int y, int angle) &#123;
    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();
  &#125;

/////////////////////////////////////////////////////////////////
// update fire sprite: bump counter, suspend sprite if
//                     counter's high enough
/////////////////////////////////////////////////////////////////

  public void update() &#123;
    if (isActive()) &#123;
      count&#43;&#43;;
      if (count == max_updates) &#123;
       suspend();
      &#125;
      else
       super.update();
    &#125;
  &#125;

  // check if fire intersects the ship
  public boolean intersect(Ship s) &#123;
    return isActive() &#38;&#38; s.isActive() &#38;&#38;
      (Math.abs(s.locx - locx&#43;2) &lt; max_radius) &#38;&#38;
       (Math.abs(s.locy - locy&#43;2) &lt; max_radius) ;
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>There&#146;s also a method that checks if the Fire sprite hits the player&#146;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 + -