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

📄 177-179.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 a Video 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=5//-->
<!--PAGES=177-179//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="179-185.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Notice that the missile is fired by calling its init() method. This is much faster than creating a new MissileSprite object for each mouse click, and it illustrates another general rule when writing games<I>: Avoid dynamic allocation of objects during game play</I>. Try to allocate all the objects you will use at the very beginning, if possible, so the runtime system doesn&#146;t need to construct one when the game is running.</P>
<P>Now, let&#146;s create the aliens!</P>
<H3><A NAME="Heading22"></A><FONT COLOR="#000077">Defining the UFOManager</FONT></H3>
<P>The UFOManager is responsible for initializing the individual UFO sprites, and telling them when to paint and update. Let&#146;s create the UFO class first, before defining UFOManager<I>.</I></P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">The UFO Class</FONT></H4>
<P>The UFO class will animate the sequence of bitmaps shown in Figure 5-2, so it becomes a subclass derived from the BitmapLoop sprite, which we introduced in Chapter 4, Adding Interactivity.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">The BitmapLoop Sprite Class</FONT></H4>
<P>Listing 5-10 shows the current definition of BitmapLoop.
</P>
<P><B>Listing 5-10</B> BitmapLoop class</P>
<!-- CODE //-->
<PRE>
class BitmapLoop extends BitmapSprite implements Moveable&#123;
  protected Image images[];       // sequence of bitmaps
  protected int currentImage;     // the current bitmap
  protected boolean foreground;   // are there foreground images?
  protected boolean background;   // is there background image?
  // constructor. Assumes that background image is already
  // loaded. (use MediaTracker)

  public BitmapLoop(int x,int y,Image b,Image f[],Applet a) &#123;
    super(x,y,b,a);
    if (image != null) &#123;           // if there's a background image
      background = true;
    &#125;
    else &#123;
      background = false;
    &#125;

    images = f;
    currentImage = 0;
    if (images == null || images.length == 0) &#123;
      foreground = false;          // nothing in images[]
    &#125;
    else &#123;
      foreground = true;
      if (!background) &#123;               // if no background
       width = images[0].getWidth(a); // get size of images[0]
       height = images[0].getHeight(a);
      &#125;
    &#125;
  &#125;

  // cycle currentImage if sprite is active, and there
  //   are foreground images
  public void update() &#123;
    if (active &#38;&#38; foreground) &#123;
      currentImage = (currentImage &#43; 1) % images.length;
    &#125;
    updatePosition();
  &#125;

  public void paint(Graphics g) &#123;
    if (visible) &#123;
      if (background) &#123;
       g.drawImage(image,locx,locy,applet);
      &#125;
      if (foreground) &#123;
       g.drawImage(images[currentImage],locx,locy,applet);
      &#125;
    &#125;
  &#125;

// implement moveable interface

  public void setPosition(int x,int y) &#123;
    locx = x;
    locy = y;
  &#125;
  protected int vx;
  protected int vy;

  public void setVelocity(int x,int y) &#123;
    vx = x;
    vy = y;
  &#125;

  // update position according to velocity
  public void updatePosition() &#123;
    locx &#43;= vx;
    locy &#43;= vy;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>The UFO reuses most of the code from the BitmapLoop, but it overrides update() to provide alienlike behaviors. The new update() implements a state machine that permits the alien to switch behavior at random moments. You saw a simple example of state machines in the DancingRect classes of Chapter 2, Using Objects for Animation; the UFO machine is just a bit more complex. By using state machines, you create a simple kind of machine intelligence in your enemies.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">The Four UFO Behavioral States</FONT></H4>
<P>The UFO has four behaviors, each represented by one of the following states :
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Standby. When the UFO is in Standby mode, it moves back and forth horizontally.
<DD><B>&#149;</B>&nbsp;&nbsp;Attack. An attacking UFO moves quickly downward, toward the missile launcher, and it is invulnerable to your missiles.
<DD><B>&#149;</B>&nbsp;&nbsp;Retreat. The UFO can break off the attack at any moment, and retreat, which means that it moves up, toward the top of the screen.
<DD><B>&#149;</B>&nbsp;&nbsp;Land. Finally, the alien can try to land, and to do this it descends vertically at a slow rate.
</DL>
<P>Figure 5-13 illustrates these various UFO behaviors.
</P>
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/05-13.jpg',562,382 )"><IMG SRC="images/05-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/05-13.jpg',562,382)"><FONT COLOR="#000077"><B>Figure 5-13</B></FONT></A>&nbsp;&nbsp;UFO behaviors</P>
<P>Now let&#146;s describe how the UFO can make transitions from state to state.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="179-185.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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