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

📄 188-192.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=188-192//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="185-188.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/193-199.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading33"></A><FONT COLOR="#000077">Implementing the GameManager Class</FONT></H4>
<P>Finally, the definition of the GameManager is shown in Listing 5-13. Compile it along with the other classes defined in this chapter, and run the Alien Landing simulation!
</P>
<P><B>Listing 5-13</B> GameManager class</P>
<!-- CODE //-->
<PRE>
public class GameManager extends Applet implements Runnable &#123;

  Thread animation;

  Graphics offscreen;
  Image image;

  static final int REFRESH_RATE = 80; // in ms

  Image ufoImages[] =  new Image[6]; // 6 ufo Images
  Image gunImage;                    // gun image
  GunManager gm;
  UFOManager um;

  int width, height;                   // applet dimensions

  public void init() &#123;
    showStatus("Loading Images -- WAIT!");
    setBackground(Color.black);        // applet background
    width = bounds().width;            // set applet dimensions
    height = bounds().height;
    loadImages();
    um = new UFOManager(width,height,ufoImages,this);
    gm = new GunManager(width,height,gunImage,
                      um.getUFO(),
                      this);
    um.initialize(gm);                 // initialize gun parameters
    image = createImage(width,height); // make offscreen buffer
    offscreen = image.getGraphics();
 &#125;

  public void loadImages() &#123;

    MediaTracker t = new MediaTracker(this);
    gunImage = getImage(getCodeBase(),"image/gun.gif");
    t.addImage(gunImage,0);
    for (int i=0; i&lt;6; i&#43;&#43;) &#123;
      ufoImages[i] = getImage(getCodeBase(),
                     "image/ufo" &#43; i &#43; ".gif");
      t.addImage(ufoImages[i],0);
    &#125;

    // wait for all images to finish loading //
    try &#123;
      t.waitForAll();
    &#125;  catch (InterruptedException e) &#123;
    &#125;

    // check for errors //
    if (t.isErrorAny()) &#123;
      showStatus("Error Loading Images!");
    &#125;
    else if (t.checkAll()) &#123;
      showStatus("Images successfully loaded");
    &#125;
    // initialize the BitmapLoop

  &#125;

   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;

  public void start() &#123;

    showStatus("Starting Game!");
    animation = new Thread(this);
     if (animation != null) &#123;
       animation.start();
     &#125;
  &#125;

  public void updateManagers() &#123;
    gm.update();
    um.update();
  &#125;

  // override update so it doesn't erase screen
  public void update(Graphics g) &#123;
    paint(g);
  &#125;

  //
  public void paint(Graphics g) &#123;

    offscreen.setColor(Color.black);
    offscreen.fillRect(0,0,width,height);  // clear buffer

    gm.paint(offscreen);
    um.paint(offscreen);

    g.drawImage(image,0,0,this);
  &#125;

  public void run() &#123;
    while (true) &#123;
      repaint();
      updateManagers();
      Thread.currentThread().yield();
      try &#123;
       Thread.sleep (REFRESH_RATE);
      &#125; catch (Exception exc) &#123; &#125;;
    &#125;
  &#125;
  public void stop() &#123;

    showStatus("Game Stopped");
    if (animation != null) &#123;
      animation.stop();
      animation = null;
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->

<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="100%" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Recommended Applet Tag to Run the Alien Landing Game</B></FONT>
<P>&lt;applet code=&#147;GameManager.class&#148; width=240 height=300&gt;
</P>
</TABLE>

<H3><A NAME="Heading34"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Right now, the missile launcher fires only one missile at a time. Give it the ability to fire multiple missiles by defining a MissileSprite array in the GunManager class. You&#146;ll also want to modify the update() and paint() methods so they communicate to all members of this array.
<DD><B>&#149;</B>&nbsp;&nbsp;The UFO animation stays the same, regardless of its behavior. How would you modify the UFO class so that the animation loop is different for the attacking state? (We&#146;ll cover the answer to this in the next chapter.)
<DD><B>&#149;</B>&nbsp;&nbsp;Define another UFO class that has the ability to fire back. You have all the building blocks, such as the Intersect interface and the MissileSprite class, to do this easily.
<DD><B>&#149;</B>&nbsp;&nbsp;Add sound to the game simulation. You know how to do this already, and it&#146;s really easy. Perhaps you can add a sound when collisions occur, and another sound if an alien lands successfully.
<DD><B>&#149;</B>&nbsp;&nbsp;Draw a background image. You might want to use a bitmap of a city, in keeping with the theme of the game.
</DL>
<H3><A NAME="Heading35"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>There&#146;s a lot of code in this chapter, but it is structured into units that have specific responsibilities and consistent behavior. This way, your program is understandable and extensible, which cuts down on the number of bugs, and the amount of time you will need to write a complex game.
</P>
<P>In the next chapter, we&#146;ll extend the GameManger unit so it also handles the initial and closing sequences of Alien Landing, and keeps track of the score. We&#146;ll also modify the UFOManager so the difficulty level increases as time goes on. By the end, you&#146;ll have a real video game!</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="185-188.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/193-199.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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