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

📄 510-514.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=510-514//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="503-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="514-519.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Choosing a Parent for the PolygonSprite Class</FONT></H4>
<P>What is the best parent class for a PolygonSprite? This would be the most specific class in our Sprite hierarchy that provides functionality that we can build upon. We&#146;re not going to use bitmaps to represent the polygons, so the BitmapSprite or BitmapLoop provides unnecessary functionality. On the other hand, a RectSprite displays rectangles, so it&#146;s a bit too specific. The best choice of parent class for a PolygonSprite is Sprite2D, which encapsulates sprites that rely on methods defined in the Graphics class. Listing 13-1 shows the definition of Sprite2D and its parent, Sprite.
</P>
<P><B>Listing 13-1</B> Sprite and Sprite2D classes</P>
<!-- CODE //-->
<PRE>
abstract class Sprite &#123;
  protected boolean visible;           // is sprite visible
  protected boolean active;            // is sprite updatable

  // abstract methods:
  abstract void paint (Graphics g);
  abstract void update();

  // accessor methods:
  public boolean isVisible() &#123;
    return visible;
  &#125;

  public void setVisible(boolean b) &#123;
    visible = b;
  &#125;

  public boolean isActive() &#123;
    return active;
  &#125;

  public void setActive(boolean b) &#123;
    active = b;
  &#125;

  // suspend the sprite
  public void suspend() &#123;
    setVisible(false);
    setActive(false);
  &#125;

  // restore the sprite
  public void restore() &#123;
    setVisible(true);
    setActive(true);
  &#125;

&#125;

abstract class Sprite2D extends Sprite &#123;

  protected int locx;                // x location
  protected int locy;                // y location

  protected Color color;             // the color of sprite
  protected boolean fill;            // filled or not

  public boolean getFill() &#123;
    return fill;
  &#125;

  public void setFill(boolean b) &#123;
    fill = b;
  &#125;

  public void setColor(Color c) &#123;
    color = c;
  &#125;

  public Color getColor() &#123;
    return color;
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>After deriving PolygonSprite, we&#146;ll subclass MoveablePolygon to represent the firing and the enemy ships, and RotateablePolygon to display the player&#146;s ship and the asteroids.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Defining PolygonSprite</FONT></H4>
<P>To implement PolygonSprite, we need some way of representing a polygon. Fortunately, java.awt provides a Polygon class that&#146;s convenient to use.
</P>

<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="100%" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>java.awt.Polygon</B></FONT>
<P>Variables
</P>
<DL>
<DT><B><I>npoints</I>:
</B>
<DD>The number of points in the polygon
<DT><B><I>xpoints</I>:
</B>
<DD>The array of x coordinates
<DT><B><I>ypoints</I>:
</B>
<DD>The array of y coordinates
</DL>

<P>Selected Methods
</P>
<DL>
<DT><B>Polygon():</B>
<DD>Creates an empty polygon
<DT><B>Polygon(int xpoints[], int ypoints[], int npoints):</B>
<DD>Creates and initializes a polygon with the specified parameters
<DT><B>addPoint(int x, int y):</B>
<DD>Appends point (x,y) to polygon
</DL>

</TABLE>

<P>To paint a Polygon <I>p</I> in Graphics context <I>g</I>, use</P>
<!-- CODE SNIP //-->
<PRE>
g.drawPolygon(p);
</PRE>
<!-- END CODE SNIP //-->
<P>or
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillPolygon(p);
</PRE>
<!-- END CODE SNIP //-->
<P>Of course, it&#146;s possible to draw a polygon by using a sequence of g.drawLine() commands, but it&#146;s definitely faster to use the polygon-draw from the Graphics class. Let&#146;s use java.awt.Polygon to help us with the PolygonSprite class, shown in Listing 13-2.
</P>
<P><B>Listing 13-2</B> PolygonSprite class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// PolygonSprite class: encapsulates polygon, position
//                     it appears, visibility
//
/////////////////////////////////////////////////////////////////

class PolygonSprite extends Sprite2D &#123;

  protected Polygon p;

  // methods:

/////////////////////////////////////////////////////////////////
// constructor: take absolute coords
/////////////////////////////////////////////////////////////////

  public PolygonSprite(int x[], int y[], int n, Color c) &#123;
    p = new Polygon(x,y,n);
    color = c;
    visible = true;
    fill = false;                           // don't fill polygon
    locx = locy = 0;
  &#125;

/////////////////////////////////////////////////////////////////
// add point to the polygon
/////////////////////////////////////////////////////////////////

  public void addPoint(int x, int y) &#123;
    p.addPoint(x,y);
  &#125;

/////////////////////////////////////////////////////////////////
// paint polygon based on variables from Sprite and Sprite2D
/////////////////////////////////////////////////////////////////

  public void paint(Graphics g) &#123;
    if (visible) &#123;
      g.setColor(color);
      if (fill)
       g.fillPolygon(p);

      else
       g.drawPolygon(p);
    &#125;
  &#125;

/////////////////////////////////////////////////////////////////
// no update operation
/////////////////////////////////////////////////////////////////

  public void update() &#123;
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>As you see, it&#146;s pretty straightforward! Now the MoveablePolygon can be derived from PolygonSprite. In Listing 13-3, we&#146;ll use the Moveable interface (defined in Chapter 3, Animating Sprites), which specifies methods to move a Spriteclass.
</P>
<P><B>Listing 13-3</B> Moveable interface</P>
<!-- CODE SNIP //-->
<PRE>
public interface Moveable &#123;
  public abstract void setPosition(int x, int y);
  public abstract void setVelocity(int x, int y);
  public abstract void updatePosition();
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Remember that a class implements an interface by providing the code for methods specified in the interface. One way of moving the polygon (as required by updatePosition()) is to store the polygon in <I>local</I> coordinates, and compute the <I>screen</I> coordinates before painting. Using a local coordinate system is also going to make rotating a polygon really easy, since we can store the points in <I>polar</I> coordinates. Did you catch all that? If not, here&#146;s a little review...</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="503-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="514-519.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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