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

📄 561-570.html

📁 java game programming e-book
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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 the JAVAroids 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=13//-->
<!--PAGES=561-570//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="547-561.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="570-575.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">The Ship Manager</FONT></H4>
<P>The ShipManager class is in charge of initializing the player&#146;s ship and the Fire sprites (which draw the missiles that the player fires). As usual, this initialization occurs in the ShipManager&#146;s constructor.
</P>
<P>The next function of the ShipManager is translating player input into messages to the Ship sprite. To process user input, we&#146;ll keep a buffer that tracks whether a key is pressed or not. This allows the game to react to multiple keystrokes, so you can fire and thrust simultaneously, for example. If we didn&#146;t keep a key state buffer, the game would only respond to a single keystroke at a time!</P>
<P>Here&#146;s the code that defines and sets the appropriate booleans in the key buffer. The keyboard event handlers in the main applet will call these keyUp() and keyDown() methods of the ShipManager:</P>
<!-- CODE //-->
<PRE>
// declare the key buffer
  static boolean keyState[] = new boolean[5];

  public void keyDown(Event e,int k) &#123;
    switch(k) &#123;
    case LEFT:
      keyState[LEFTINDEX] = true;
      break;
    case RIGHT:
      keyState[RIGHTINDEX] = true;
      break;
    ...
  &#125;
  public void keyUp(Event e,int k) &#123;
    switch(k) &#123;
    case LEFT:
      keyState[LEFTINDEX] = false;
      break;
    case RIGHT:
      keyState[RIGHTINDEX] = false;
      break;
    ...
  &#125;
</PRE>
<!-- END CODE //-->
<P>The ShipManager&#146;s update() method does two things. First, it updates the Fire sprites associated with the player, stored in the array <I>f[]:</I></P>
<!-- CODE SNIP //-->
<PRE>
public void update() &#123;

  // update fire sprites
  for ( int i=0 ; i&lt;MAX_SHOTS; i&#43;&#43;) &#123;
    f[i].update();
  &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Then, it reads the key buffer, and tells the Ship sprite what to do based on buffer contents. Here&#146;s an excerpt of this:
</P>
<!-- CODE //-->
<PRE>
// if ship's alive
if (shipAlive) &#123;

  // check the key buffer and perform the
  //   associated action

  if (keyState[LEFTINDEX])
    s.rotateLeft();
  if (keyState[RIGHTINDEX])
    s.rotateRight();
  ...
</PRE>
<!-- END CODE //-->
<P>Thus, multiple keystrokes are handled successfully!
</P>
<P>Finally, the paint() method of this manager class tells the Fire sprites and Ship sprite to paint. In addition, it creates a status display of the number of ships left, and the shield power remaining. If the shield button is down, paint() also draws the shield around the Ship sprite.</P>
<P>Listing 13-15 shows the ShipManager code.</P>
<P><B>Listing 13-15</B> ShipManager class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// ShipManager class -- responsible for tracking ship data
//                      (e.g. shipsLeft, shield)
//
//                   -- responsible for translating player input
//                      to Ship sprite methods
/////////////////////////////////////////////////////////////////

public class ShipManager extends Object &#123;
  // coordinates of the Ship sprite polygon
  static final int tpx[] = &#123; 0, -7, 7, 0&#125;;
  static final int tpy[] = &#123; 13, -7, -7, 13&#125;;

  // 5 shots at a time
  static final int MAX_SHOTS = 5;

  // length of ship's nose
  static final int SHIP_LENGTH = 13;

  static int width,height;                 // screen dimensions
  private int shipsLeft;
  private boolean shipAlive;
  private boolean shieldOn;
  private int shieldStrength;
  private boolean gameOver;

  private Ship s;                          // Ship sprite
  private Fire f[] = new Fire[MAX_SHOTS];  // Fire sprites
  EffManager efm;
  GameManager game;

/////////////////////////////////////////////////////////////////
// ShipManager constructor
/////////////////////////////////////////////////////////////////
  public ShipManager(int width,int height,
                   EffManager efm,GameManager game) &#123;

    // initialize Ship sprite
    s = new Ship(tpx,tpy,tpx.length,       // coords of Ship
               width/2,height/2,         // initial position
               Color.yellow,             // color
               width,height,             // screen bounds
               SHIP_LENGTH);             // length of ship

    // initialize Fire sprites
    for ( int i=0 ; i&lt;MAX_SHOTS; i&#43;&#43;) &#123;
      f[i] = new Fire(Color.white,width,height);
      f[i].suspend();
    &#125;

    // initialize variables
    this.efm = efm;
    this.game = game;
    this.width = width;
    this.height = height;
    shipsLeft = 2;
    shipAlive = true;
    shieldOn = false;
    shieldStrength = 100;
    gameOver = false;

    // activate Ship sprite
    s.restore();
  &#125;

/////////////////////////////////////////////////////////////////
// Accessor methods : provide access to privates:
//                    Ship and Fire sprites
/////////////////////////////////////////////////////////////////

  // return Ship sprite
  public Ship getShip() &#123;
    return s;
  &#125;

  // return array of Fire sprites
  public Fire[] getFire() &#123;
    return f;
  &#125;

  // suspend the given Fire sprite
  public void stopFire(int i) &#123;
    f[i].suspend();
  &#125;

  // give an extra ship
  public void extraShip() &#123;
    shipsLeft&#43;&#43;;
  &#125;

/////////////////////////////////////////////////////////////////
// Destroy the Ship
/////////////////////////////////////////////////////////////////

  // waiting period for the next ship
  static int waitPeriod;

  public void destroyShip() &#123;
    if (gameOver) return;

    // if there's no shield on, destroy the ship
    if (!(keyState[SHIELDINDEX] &#38;&#38; shieldStrength &gt; 0)) &#123;

      shipAlive = false;              // set boolean
      s.suspend();                    // suspend sprite
      shipsLeft--;                    // update count
      if (shipsLeft &lt; 0) &#123;            // if no ships left
        game.setGameOver();           //   then game's over

⌨️ 快捷键说明

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