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

📄 203-207.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=203-207//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="199-203.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="207-211.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Finally, the UFOManager methods update() and paint() will take the number of sprites for each level into account:
</P>
<!-- CODE //-->
<PRE>
// paint all ufos in a level
public void paint(Graphics g) &#123;
  for (int i=0; i&lt;level; i&#43;&#43;) &#123;
    ufo[i].paint(g);
  &#125;
&#125;

// update all ufos in a level. Otherwise start
//   ufo if it's not on screen
public void update() &#123;
  for (int i=0; i&lt;level; i&#43;&#43;) &#123;
    if (ufo[i].isActive()) &#123;
     ufo[i].update();
    &#125;
    else &#123;             // make new ufo
     initializePosition(ufo[i]);
     ufo[i].init();
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>Try out each of these changes. At first, you will see two UFOs on the screen, then three, then four, and so on, and the game gets harder and harder to play!
</P>
<P>Now let&#146;s modify the GameManager so it will track and display information about the score, and the number of aliens landed.</P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Tracking Game Status</FONT></H3>
<P>The GameManager is responsible for handling input from the player and relaying game information back. It will track and display two pieces of information that are vital to the player: the score and the number of aliens landed. This will require messaging from the UFO class to the GameManager. Figure 6-4 diagrams the messaging between the two classes.
</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/06-05.jpg',463,317 )"><IMG SRC="images/06-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-05.jpg',463,317)"><FONT COLOR="#000077"><B>Figure 6-4</B></FONT></A>&nbsp;&nbsp;Communication between UFO sprite and GameManager</P>
<P>First, let&#146;s implement a simple scoring system in which each alien is worth UFO_VALUE points. GameManager needs a public method that the UFO can call when it has been killed. This method is called incrementScore(), and it increases the <I>score</I> by the appropriate amount:</P>
<!-- CODE SNIP //-->
<PRE>
static final int UFO_VALUE = 130;   // 130 points

private int score;

  // increase score
  public void incrementScore() &#123;
    score &#43;= UFO_VALUE;
  &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The UFO sprite will call incrementScore() from its hit() method. The variable <I>game</I> is assigned in the constructor for UFO, and it refers to the GameManager.</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
    game.incrementScore();       // add to score
    um.killed();                 // tell UFOManager
                                 //  another UFO's dead
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>Now <I>score</I> gets updated every time you shoot an alien.</P>
<P>Tracking the number of landed aliens involves similar modifications. Define a variable <I>numLanded</I> in the GameManager that tracks the current number of aliens landed. The public method alienLanded() provides the interface that the UFO class calls when it lands. If too many aliens land, then gameOver() gets called.</P>
<!-- CODE //-->
<PRE>
static final int MAX_LANDED = 5;    // at most 5 aliens
                                    //   can land
private int numLanded;              // num of aliens landed

// count number of ufo's landed
public void alienLanded() &#123;
  numLanded&#43;&#43;;
  if (numLanded == MAX_LANDED) &#123;
    gameOver();
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>The call to alienLanded() occurs in the UFO&#146;s landingRoutine() method, which is triggered if its y coordinate is high enough. The following is an excerpt from the UFO&#146;s update():
</P>
<!-- CODE //-->
<PRE>
case LAND:

  if (r1 &gt; LAND_EXIT) &#123;
   startStandby();
  &#125;
  else if (locy &gt;= max_y-height) &#123;
   landingRoutine();
  &#125;
  break;
</PRE>
<!-- END CODE //-->
<P>The new version of landingRoutine() is defined as
</P>
<!-- CODE SNIP //-->
<PRE>
// when the alien lands successfully
protected void landingRoutine() &#123;
  game.alienLanded();        // tell game manager that
                             //   the UFO's landed
  suspend();
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The modifications of this section allow GameManager to keep track of the score and the number of UFOs landed. We&#146;ll put the code for displaying <I>score</I> and <I>numLanded</I> into GameManager&#146;s paint() method. In the version in Listing 6-1, the coordinates of the Strings are hardcoded for simplicity. You can also use the FontMetrics class, which you saw in Chapter 4, Adding Interactivity, to dynamically compute the coordinates based on the length of the string.</P>
<P>If you compile and play the game now, you will rack up pretty good scores, because your missile launcher is immune to alien attacks. Let&#146;s change this next!</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Modifying GunManager</FONT></H3>
<P>There are at least three ways that the missile gun could respond to alien hits:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The gun blows up. You can provide multiple guns, and when they are all destroyed, the game is over.
<DD><B>&#149;</B>&nbsp;&nbsp;The gun&#146;s ability is impaired. For example, it fires slower, or more missiles are needed to kill a UFO.
<DD><B>&#149;</B>&nbsp;&nbsp;The gun loses energy. When there is no energy left, the game ends.
</DL>
<P>Let&#146;s implement the last option. We&#146;ll store the current amount of energy in the GunManager. When the GunSprite gets hit, it tells the GunManager, which updates the energy level. The diagram in Figure 6-5 depicts the communication between the GunSprite and the GunManager.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/06-06.jpg',464,316 )"><IMG SRC="images/06-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-06.jpg',464,316)"><FONT COLOR="#000077"><B>Figure 6-5</B></FONT></A>&nbsp;&nbsp;Communication between GunSprint and GunManager</P>
<P>Now for the details. When a UFO collides with the player&#146;s gun, it calls the GunSprite&#146;s hit() method. This, in turn, calls GunManager&#146;s handleHit(). In GunSprite&#146;s hit() method, <I>gm</I> refers to the GunManager:</P>
<!-- CODE SNIP //-->
<PRE>
public void hit() &#123;

  gm.handleHit();           // notify manager of hit

&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The GunManager will track the energy level, and decrease it when there is an alien hit. This happens in GunManager&#146;s handleHit():
</P>
<!-- CODE //-->
<PRE>
// handles a hit from an alien
  public void handleHit() &#123;
    displayHit = true;             // set display hit flag
    energy -= energyDec;           // update energy
    if (energy &lt;= 0) &#123;     // game over if energy &lt;= 0
      game.gameOver();             // notify game manager
      gun.suspend();               // turn off sprites
      missile.suspend();
    &#125;
  &#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="199-203.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="207-211.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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