185-188.html

来自「java game programming e-book」· HTML 代码 · 共 239 行

HTML
239
字号
<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=185-188//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="179-185.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="188-192.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading28"></A><FONT COLOR="#000077">The UFOManager Class</FONT></H4>
<P>The UFOManager&#146;s constructor allocates the UFO sprites that will be used during the course of the game, circumventing the need for dynamic allocation during game play. In addition, the UFOManager sets the initial position of the UFOs, and tells them when to update and paint. If a UFO sprite is hit by a missile, it becomes inactive, so it disappears from the screen.
</P>
<P>For this simulation, the UFOManager&#146;s update() method simply restores the sprite at a different location, making it appear as if a new UFO has entered the fray, but keeping the number of aliens constant. In the next chapter, you will learn how to create a UFOManager that will increase the number of active UFOs as the game progresses, making it harder to play! Listing 5-12 defines the UFOManager class.</P>
<P><B>Listing 5-12</B> UFOManager class</P>
<!-- CODE //-->
<PRE>
public class UFOManager &#123;

  static int width, height;            // applet dimensions
  private UFO ufo[];
  static final int NUM_UFOS = 7;

  public UFOManager(int width,int height,
                   Image ufoImages[],Applet a) &#123;
    this.width = width;
    this.height =  height;

    ufo = new UFO[NUM_UFOS];
    for (int i=0; i&lt;ufo.length; i&#43;&#43;) &#123;
      ufo[i] = new UFO(ufoImages,width,height,a);
      initializePosition(ufo[i]);
    &#125;
  &#125;

  // This method tells the UFO class where
  //   the gun is (so the UFOs know if they&#146;ve
  //   collided with it)
  public void initialize(GunManager gm) &#123;
    UFO.initialize(gm);
  &#125;

  private void initializePosition(Moveable m) &#123;
    m.setPosition(UFO.getRand(width - 100)&#43;50,
                UFO.getRand(height - 150)&#43;10);

  &#125;

  // accessor method, so the missile knows where
  //   the targets are!
  public UFO[] getUFO() &#123;
    return ufo;
  &#125;

  public void paint(Graphics g) &#123;
    for (int i=0; i&lt;ufo.length; i&#43;&#43;) &#123;
      ufo[i].paint(g);
    &#125;
  &#125;

  public void update() &#123;
    for (int i=0; i&lt;ufo.length; i&#43;&#43;) &#123;
      if (ufo[i].isActive()) &#123;
       ufo[i].update();
      &#125;
      else &#123;                  // restore ufo
                              //   at different location
       initializePosition(ufo[i]);
       ufo[i].restore();
      &#125;
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading29"></A><FONT COLOR="#000077">Defining the GameManager</FONT></H3>
<P>The GameManager, the final building block of this chapter, has the following responsibilities, as noted earlier:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Initializes the GunManager and UFOManager
<DD><B>&#149;</B>&nbsp;&nbsp;Relays player input to the GunManager (event handling)
<DD><B>&#149;</B>&nbsp;&nbsp;Implements the Video Game Loop
</DL>
<P>As with the animation driver you used at the end of Chapter 4, Adding Interactivity, it uses MediaTracker to load the images.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading30"></A><FONT COLOR="#000077">Two Responsibilities of the GameManager Class</FONT></H4>
<P>Let&#146;s examine two responsibilities of this class.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading31"></A><FONT COLOR="#000077">Passing Mouse Input to the GunManager</FONT></H4>
<P>First, here&#146;s the way the GameManager passes mouse input to the GunManager:
</P>
<!-- CODE //-->
<PRE>
public boolean mouseMove(Event e,int x,int y) &#123;
  gm.moveGun(x);
  return true;
&#125;
public boolean mouseDrag(Event e,int x,int y) &#123;
  gm.moveGun(x);
  return true;
&#125;
public boolean mouseDown(Event e,int x,int y) &#123;
  gm.fireMissile(x);
  return true;
&#125;
</PRE>
<!-- END CODE //-->
<P>Each event handler passes the x coordinate of the mouse location to the appropriate method of the GunManager. mouseDrag must be handled, so that the player can move the gun even if the mouse button is pressed.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">Implementing the Video Game Loop</FONT></H4>
<P>Next, let&#146;s look at how the GameManager implements the Video Game Loop:
</P>
<!-- CODE //-->
<PRE>
public void run() &#123;
  while (true) &#123;
    repaint();
    updateManagers();
    Thread.yield();
    try &#123;
     Thread.sleep (REFRESH_RATE);
    &#125; catch (Exception exc) &#123; &#125;;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>As you see, this code is pretty general, and you can adapt a loop like this for your own games.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="179-185.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="188-192.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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