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

📄 193-199.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:Extending Your 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=6//-->
<!--PAGES=193-199//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch05/188-192.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="199-203.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 6<BR>Extending Your Video Game
</FONT></H2>
<P><I>Joel Fan</I></P>
<P><FONT SIZE="+1"><B>Goals:</B></FONT></P>
<P>Add features to the Alien Landing simulation using incremental development
</P>
<P>Complete the Alien Landing video game</P>
<P>In this chapter, you&#146;ll extend the Alien Landing simulation of the previous chapter into a complete video game, by adding explosions, a status display, levels of play, scoring, and other features. In addition, you&#146;ll create an introductory screen, so that new players understand how to play your game, and a closing sequence, so that they know when the game is over.</P>
<P>There are two types of extensions you will implement on top of the existing Alien Landing simulation:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Extensions that primarily involve a single class
<DD><B>&#149;</B>&nbsp;&nbsp;Extensions that require messaging between classes
</DL>
<P>For example, explosions will be handled within the UFO class, once the bitmaps are loaded and passed in. On the other hand, scoring will require communication between the UFO and the GameManager, and when the UFO sprite is hit, it sends a message to GameManager to update the player&#146;s point total.
</P>
<P>By developing and testing these extensions one at a time, you can build on the game simulation with a minimum of errors. This process is called <I>incremental development</I>, and it can cut down on the amount of time you spend tracking down nasty bugs! As you progress through this chapter, feel free to implement and test each extension individually, so you really understand how the pieces fit together.</P>
<P>Let&#146;s get started. First, let&#146;s extend the UFO class so the animation loop changes, depending on the state of the alien.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Changing the UFO Animations</FONT></H3>
<P>When the alien gets shot by a missile, it should explode in a ball of fire. But if the alien is in attack mode, it is invulnerable to the player&#146;s missiles. Let&#146;s see how to signify these alien states&#151;exploding and attacking&#151;by changing the animation loop in the UFO class. The UFOManager and GameManager must also be modified to provide the initializations needed.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Extending the UFO Class</FONT></H4>
<P>Figures 6-1A and 6-1B show the sequence of bitmaps that animate attacking and exploding aliens.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/06-01.jpg',771,169 )"><IMG SRC="images/06-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-01.jpg',771,169)"><FONT COLOR="#000077"><B>Figure 6-1A</B></FONT></A>&nbsp;&nbsp;Attacking aliens</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/06-02.jpg',555,162 )"><IMG SRC="images/06-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-02.jpg',555,162)"><FONT COLOR="#000077"><B>Figure 6-1B</B></FONT></A>&nbsp;&nbsp;Exploding aliens</P>
<P>Remember that the UFO class extends the BitmapLoop class, and that it inherits BitmapLoop&#146;s paint() method:
</P>
<!-- CODE //-->
<PRE>
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;
</PRE>
<!-- END CODE //-->
<P>The <I>images</I> variable refers to the array of bitmaps that comprise the animation. Right now, <I>images</I> refers to the UFO animation, but if we store references to the exploding, attacking, and UFO sequences, we can switch animations by assigning <I>images</I> the correct image array. Thus, the first change is in the constructor to the UFO class, which now refers to the various animation sequences in the variables <I>ufo</I>, <I>attack</I>, and <I>explode</I>.</P>
<!-- CODE //-->
<PRE>
// bitmap animations
protected Image ufo[];             // ufo animation
protected Image attack[];          // attack animation
protected Image explode[];         // explosion sequence
...
// constructor: initialize image references, instance vars
public UFO(Image ufoImages[],
          Image attackImages[],
          Image explodeImages[],
          int max_x,int max_y,
          UFOManager um,
          Applet a) &#123;
  ...
  ufo = ufoImages;
  attack = attackImages;
  explode = explodeImages;
  ...
</PRE>
<!-- END CODE //-->
<P>Now <I>images</I> will be assigned the appropriate animation sequence, depending on the alien state. As Figure 6-2 shows, the change in the animation loop occurs only when the alien starts or exits the Attack state. Thus, only the methods that implement state transitions to and from the Attack state are modified. These are the UFO methods startAttack() and startRetreat():</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/06-03.jpg',467,400 )"><IMG SRC="images/06-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-03.jpg',467,400)"><FONT COLOR="#000077"><B>Figure 6-2</B></FONT></A>&nbsp;&nbsp;UFO transition diagram</P>
<!-- CODE //-->
<PRE>
// start attack state
protected void startAttack() &#123;
  vx = getRand(10)-5;
  vy = getRand(5)&#43;7;
  images = attack;            // change to attack animation loop
  state = ATTACK;
&#125;
// start retreating state
protected void startRetreat() &#123;
  vx = 0;
  vy = -getRand(3) - 2;
  images = ufo;               // change to usual animation loop
  state = RETREAT;
&#125;
</PRE>
<!-- END CODE //-->
<P>In addition, let&#146;s add a new state, Explode, which signifies an exploding alien, and a method, startExplode(), which causes a transition to this state:
</P>
<!-- CODE //-->
<PRE>
static final byte EXPLODE = 4;

// start explosion state
protected void startExplode() &#123;
  images = explode;         // set bitmap to explosion sequence
  currentImage = 0;         // start at beginning of animation
  explosion_counter = 0;    // count the number of frames
  um.playExplosion();       // play explosion sound:
                            //   (um is reference to the
                            //    UFOManager class)
  state = EXPLODE;

&#125;
</PRE>
<!-- END CODE //-->
<P>The startExplode() method is called when the alien is hit by a missile and the alien is not in the Attack state (so it is vulnerable). startExplode() also calls the UFOManager method playExplosion(), which plays the explosion sound.
</P>
<P>Now modify the hit() method of the UFO class so it calls startExplode(). Let&#146;s also add a feature so that attacking aliens get repulsed, or pushed upward, by a missile hit. This is accomplished by subtracting a constant from the y coordinate of the attacking alien:</P>
<!-- CODE //-->
<PRE>
// this is called if a missile hits the alien
public void hit() &#123;
  // alien is invulnerable when it's attacking
  //   but it gets "pushed back"
  if (state == ATTACK) &#123;
    locy -= 17;
  &#125;
  // otherwise explode!
  else if (state != EXPLODE) &#123;
    startExplode();              // start explode state
    ...
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>Finally, UFO&#146;s update() needs to be changed, so that it takes the Explode state into account and suspends the sprite after the explosion animation is complete:
</P>
<!-- CODE SNIP //-->
<PRE>
    case EXPLODE:
    explosion_counter&#43;&#43;;     // bump counter

                                     // suspend once animation
                                     //   is finished
    if (explosion_counter == explode.length) &#123;
     suspend();
    &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>You can find these changes to the UFO class in Listing 6-5.
</P>
<P>Now let&#146;s modify the UFOManager and the GameManager so that they load and pass these new animation sequences to the UFO class.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch05/188-192.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="199-203.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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