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

📄 539-543.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=539-543//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="533-539.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="543-547.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">The Asteroid Sprite</FONT></H4>
<P>The Asteroid sprite, shown in Listing 13-10, derives from RotateablePolygon. The variable <I>value</I> stores the number of points the player gets for shooting it There are three intersection calculations in the Asteroid definition&#151;one for each type of asteroid-object collision. Again, we use the simple algorithm of determining whether the object&#146;s center is within <I>max_radius</I> of the Asteroid.</P>
<P><B>Listing 13-10</B> Asteroid class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Asteroid sprite class
//
/////////////////////////////////////////////////////////////////

public class Asteroid extends RotateablePolygon &#123;

  // maximum rate of rotation
  static final int MAX_ROTATE_RATE = 8;

  // value of this asteroid
  public int value;

  public Asteroid(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;              // set value
                            // set rotation rate
    rate = GameMath.getRand(2*MAX_ROTATE_RATE)-MAX_ROTATE_RATE;
    setFill(true);          // fill the 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 asteroid
  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;

  // check for intersection with Enemy
  //    see if Enemy's center is within max_radius of this
  //    asteroid, with 2 pixels of leeway on each side
  public boolean intersect(Enemy e) &#123;
    return isActive() &#38;&#38; e.isActive() &#38;&#38;
      (Math.abs(e.locx - locx&#43;2) &lt; max_radius) &#38;&#38;
       (Math.abs(e.locy - locy&#43;2) &lt; max_radius) ;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">The Ship Sprite</FONT></H4>
<P>Ship is the sprite that the player controls. It inherits from RotateablePolygon, and provides methods for rotating left, rotating right, and thrusting. Ship overrides RotateablePolygon&#146;s update() method (which provides the continuous rotation used by Asteroid). The new update() method rotates the ship only in response to a rotateLeft() or rotateRight() message. The thrust() method updates the ship&#146;s velocity, based on the angle <I>theta</I> (inherited from RotateablePolygon). The Ship class is shown in Listing 13-11.</P>
<P><B>Listing 13-11</B> Ship class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Ship sprite: adds a few methods to RotateablePolygon
//
/////////////////////////////////////////////////////////////////

public class Ship extends RotateablePolygon &#123;

  // default rate of rotation is 15 degrees
  static final int ROT_RATE = 15 ;

  // has ship rotated?
  boolean rotated = false;

  // actual amount of rotation
  int rotation_amount;

/////////////////////////////////////////////////////////////////
// Ship constructor
/////////////////////////////////////////////////////////////////

  public Ship(int tx[], int ty[], int n,   // coordinates
             int centerx, int centery,    // location
             Color c,                     // color
             int w, int h,                // screen bounds
             int r) &#123;                     // ship length

    super(tx,ty,n,centerx,centery,c,w,h,r);

    // align the nose of the ship with theta = 0
    // this is necessary because the direction of ship's
    // thrust is based on theta, so the angles must be
    // rotated so that the angle of the ship's nose
    // and theta match.
    for (int i=0 ; i &lt; angle.length; i&#43;&#43;) &#123;
      angle[i] = checkAngleBounds(angle[i]-90);
    &#125;
    rotate(-90);                           // initially ship
                                          //   faces up
  &#125;

/////////////////////////////////////////////////////////////////
//
// Rotation methods: called by ShipManager
//
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// rotate left =&gt; decreasing theta in screen coordinate system
/////////////////////////////////////////////////////////////////
  public void rotateLeft() &#123;
    rotated = true;
    rotation_amount = -ROT_RATE;
  &#125;

/////////////////////////////////////////////////////////////////
// rotate right =&gt; increasing theta in screen coordinate system
/////////////////////////////////////////////////////////////////
  public void rotateRight() &#123;
    rotated = true;
    rotation_amount = ROT_RATE;
  &#125;

/////////////////////////////////////////////////////////////////
// override update to prevent continuous rotation
/////////////////////////////////////////////////////////////////

  public void update() &#123;
    if (isActive()) &#123;
      updatePosition();

      if (rotated) &#123;
       rotate(rotation_amount);
       rotated = false;
      &#125;
      else &#123;
       updatePoints();
      &#125;

    &#125;
  &#125;

/////////////////////////////////////////////////////////////////
//
// handle ship thrusting:
//   update ship's velocity based on direction it's pointed
//
/////////////////////////////////////////////////////////////////

  static final double ACCEL_FACTOR = 1.0;

  public void thrust() &#123;

    vx = (int)Math.round((double)vx &#43;
                      GameMath.cos(theta)*ACCEL_FACTOR);
    vy = (int)Math.round((double)vy &#43;
                      GameMath.sin(theta)*ACCEL_FACTOR);

  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="533-539.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="543-547.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -