📄 561-570.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 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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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’s ship and the Fire sprites (which draw the missiles that the player fires). As usual, this initialization occurs in the ShipManager’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’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’t keep a key state buffer, the game would only respond to a single keystroke at a time!</P>
<P>Here’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) {
switch(k) {
case LEFT:
keyState[LEFTINDEX] = true;
break;
case RIGHT:
keyState[RIGHTINDEX] = true;
break;
...
}
public void keyUp(Event e,int k) {
switch(k) {
case LEFT:
keyState[LEFTINDEX] = false;
break;
case RIGHT:
keyState[RIGHTINDEX] = false;
break;
...
}
</PRE>
<!-- END CODE //-->
<P>The ShipManager’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() {
// update fire sprites
for ( int i=0 ; i<MAX_SHOTS; i++) {
f[i].update();
}
</PRE>
<!-- END CODE SNIP //-->
<P>Then, it reads the key buffer, and tells the Ship sprite what to do based on buffer contents. Here’s an excerpt of this:
</P>
<!-- CODE //-->
<PRE>
// if ship's alive
if (shipAlive) {
// 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 {
// coordinates of the Ship sprite polygon
static final int tpx[] = { 0, -7, 7, 0};
static final int tpy[] = { 13, -7, -7, 13};
// 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) {
// 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<MAX_SHOTS; i++) {
f[i] = new Fire(Color.white,width,height);
f[i].suspend();
}
// 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();
}
/////////////////////////////////////////////////////////////////
// Accessor methods : provide access to privates:
// Ship and Fire sprites
/////////////////////////////////////////////////////////////////
// return Ship sprite
public Ship getShip() {
return s;
}
// return array of Fire sprites
public Fire[] getFire() {
return f;
}
// suspend the given Fire sprite
public void stopFire(int i) {
f[i].suspend();
}
// give an extra ship
public void extraShip() {
shipsLeft++;
}
/////////////////////////////////////////////////////////////////
// Destroy the Ship
/////////////////////////////////////////////////////////////////
// waiting period for the next ship
static int waitPeriod;
public void destroyShip() {
if (gameOver) return;
// if there's no shield on, destroy the ship
if (!(keyState[SHIELDINDEX] && shieldStrength > 0)) {
shipAlive = false; // set boolean
s.suspend(); // suspend sprite
shipsLeft--; // update count
if (shipsLeft < 0) { // if no ships left
game.setGameOver(); // then game's over
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -