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

📄 522-526.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=522-526//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="519-521.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="526-530.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Moving and Rotating Polygons</FONT></H3>
<P>We&#146;re going to subclass two Sprite classes now: MoveablePolygon and RotateablePolygon.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">The MoveablePolygon Class</FONT></H4>
<P>MoveablePolygon will subclass from PolygonSprite, which we defined above. The polygon will be stored in the int arrays <I>tx[]</I> and <I>ty[],</I> which are the x and y coordinates of polygon&#146;s vectors in its local coordinate system:</P>
<!-- CODE SNIP //-->
<PRE>
protected int tx[],ty[];                     // offset vectors
</PRE>
<!-- END CODE SNIP //-->
<P>The MoveablePolygon constructor will receive the local vectors and store them in <I>tx[]</I> and <I>ty[].</I> By storing these vectors, it&#146;ll be easy to compute the new screen coordinates of the polygon every time it moves. Since it&#146;s so convenient to specify a polygon in a local system, we will also provide, in Listing 13-5, a new constructor for PolygonSprite that takes local coordinates and converts them to the screen coordinates required by Polygon <I>p</I>.</P>
<P><B>Listing 13-5</B> PolygonSprite constructor for local coordinates</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
// constructor: take center and offset vectors
/////////////////////////////////////////////////////////////////

  public PolygonSprite(int tx[], int ty[], int n,
                     int centerx, int centery, Color c) &#123;
    int x[], y[];
    x = new int[n];
    y = new int[n];
    for (int i=0; i&lt;n; i&#43;&#43;) &#123;                  // compute abs coords
      x[i] = centerx &#43; tx[i];
      y[i] = centery &#43; ty[i];
    &#125;
    p = new Polygon(x,y,n);
    locx = centerx;
    locy = centery;
    color = c;
    visible = true;
    fill = false;                               // don't fill polygon
  &#125;
</PRE>
<!-- END CODE //-->
<P>The MoveablePolygon constructor will call this PolygonSprite constructor before storing the local vectors. In addition, MoveablePolygon declares the instance variables <I>height</I> and <I>width</I>, which store the screen bounds, and <I>max_radius</I>, which is the approximate maximum radius of the polygon. The method updatePosition() uses <I>max_radius</I> to decide if the polygon should wrap around to the other side. Here&#146;s a small excerpt of updatePosition() (from MoveablePolygon), which wraps the polygon from the right side of the screen to the left.</P>
<!-- CODE SNIP //-->
<PRE>
// if polygon is off the rhs of display
// move it to the left

if (locx - max_radius &gt; width) &#123;
locx -= (width &#43; 2*max_radius);
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Another important method is updatePoints(), which accomplishes the local to screen coordinate transformation:
</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
// convert from local coords to screen coords of the
//   polygon points
/////////////////////////////////////////////////////////////////

  public void updatePoints() &#123;

    for (int i=0; i&lt;p.npoints; i&#43;&#43;) &#123;
      p.xpoints[i] = locx &#43; tx[i];
      p.ypoints[i] = locy &#43; ty[i];

    &#125;
  &#125;
</PRE>
<!-- END CODE //-->
<P>The source for the MoveablePolygon is shown in Listing 13-6. Note how this class builds upon the PolygonSprite.
</P>
<P><B>Listing 13-6</B> MoveablePolygon class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// MoveablePolygon: encapsulates a moveable PolygonSprite
//
/////////////////////////////////////////////////////////////////

class MoveablePolygon extends PolygonSprite
implements Moveable

&#123;

  protected int tx[],ty[];                     // offset vectors
  protected int vx;                            // velocity x;
  protected int vy;                            // velocity y;
  protected int height = 1000,
                width = 1000;           // default screen limits
  protected int max_radius = 0;         // max radius of polygon
  // methods:

/////////////////////////////////////////////////////////////////
// Constructor: construct with offset vectors
/////////////////////////////////////////////////////////////////

  public MoveablePolygon(int tx[], int ty[], int n,
                     int centerx, int centery, Color c) &#123;
    super(tx,ty,n,centerx,centery,c);           // constructor
    vx = vy = 0;
    this.tx = new int[n];
    this.ty = new int[n];
    for (int i=0; i&lt;n; i&#43;&#43;) &#123;
      this.tx[i] = tx[i];                       // save offset vectors
      this.ty[i] = ty[i];
    &#125;

  &#125;

/////////////////////////////////////////////////////////////////
// Constructor: construct with offset vectors,
//              initialize screen bounds and max_radius
/////////////////////////////////////////////////////////////////

  public MoveablePolygon(int tx[], int ty[], int n,
                     int centerx, int centery, Color c,
                     int w, int h, int r) &#123;
    super(tx,ty,n,centerx,centery,c);           // constructor
    vx = vy = 0;
    this.tx = new int[n];
    this.ty = new int[n];
    for (int i=0; i&lt;n; i&#43;&#43;) &#123;
      this.tx[i] = tx[i];                       // save offset vectors
      this.ty[i] = ty[i];
    &#125;
    height = h;
    width = w;
    max_radius = r;

  &#125;

/////////////////////////////////////////////////////////////////
// implement Moveable methods
/////////////////////////////////////////////////////////////////

  // move polygon to specifiedlocation
  public void setPosition(int x, int y) &#123;
    locx = x;
    locy = y;

    // compute screen coords of polygon from local coords
    for (int i=0; i&lt;p.npoints; i&#43;&#43;) &#123;
      p.xpoints[i] = locx &#43; tx[i];
      p.ypoints[i] = locy &#43; ty[i];
    &#125;
  &#125;

  // set velocity of polygon
  public void setVelocity(int x, int y) &#123;
    vx = x;
    vy = y;
  &#125;

  public void scale(double factor) &#123;
    for (int i=0; i&lt;p.npoints; i&#43;&#43;) &#123;   // scale offset vectors
      tx[i] = (int)Math.round(factor*tx[i]);
      ty[i] = (int)Math.round(factor*ty[i]);
    &#125;
    updatePoints();
  &#125;

  // update position of center of polygon. Wrap center
  //   to other side of screen if polygon exits screen bounds
  public void updatePosition() &#123;
    locx &#43;= vx;
    locy &#43;= vy;

    // if center of polygon is off the rhs of display
    // move it to the left
    if (locx - max_radius &gt; width) &#123;
      locx -= (width &#43; 2*max_radius);
    &#125;

    // if center of polygon is off the lhs of display
    // move it to the right
    else if (locx &lt; -max_radius ) &#123;
      locx &#43;= (width &#43; 2*max_radius);
    &#125;

    // if center of polygon is off the bottom of display
    // move it to the top
    if (locy - max_radius &gt; height) &#123;
      locy -= (height &#43; 2*max_radius);
    &#125;

    // if center of polygon is off the top of display
    // move it to the bottom
    else if (locy &lt; -max_radius ) &#123;
      locy &#43;= (height &#43; 2*max_radius);
    &#125;
  &#125;

/////////////////////////////////////////////////////////////////
// convert from local coords to screen coords of the
//   polygon points
/////////////////////////////////////////////////////////////////

  public void updatePoints() &#123;

    for (int i=0; i&lt;p.npoints; i&#43;&#43;) &#123;
      p.xpoints[i] = locx &#43; tx[i];
      p.ypoints[i] = locy &#43; ty[i];

    &#125;
  &#125;

/////////////////////////////////////////////////////////////////
// default update method
/////////////////////////////////////////////////////////////////

  public void update() &#123;
    if (isActive()) &#123;
      updatePosition();      // move center
      updatePoints();        // compute polygon points
    &#125;
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="519-521.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="526-530.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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