📄 602-605.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:Daleks!</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=14//-->
<!--PAGES=602-605//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="599-602.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="605-608.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Making the Right Moves</FONT></H4>
<P>We can only hope for the player’s sake that he or she has made a wise move. Fortunately, the applet’s only job is to analyze the player’s input and update the game board according to the rules of the game. The final <I>else</I> clause (with the comment “analyze keypress and execute the next turn”) is where the entire turn unfolds, shown in Listing 14-8.</P>
<P><B>Listing 14-8</B> Executing player’s move in handleKeyPress(int whichKey); executing next turn in handleKeyPress(int whichKey)</P>
<!-- CODE //-->
<PRE>
public void handleKeyPress(int whichKey) {
if (animIntro) {
// stop the intro screen and begin the game
...
}
else if (levelComplete) {
// set up for the next level
...
}
else if (playerDead) {
// animate the intro screen
...
}
else {
// analyze keypress and execute the next turn
char ASCIImove = (char)whichKey;
boolean validMove = movePlayer(ASCIImove);
if (validMove) {
if (lastStand)
repaint();
else {
if (screwdriverUsed) {
calcSonic();
play(getCodeBase(), "sonic.au");
}
moveDaleks()
if (newRubble)
play(getCodeBase(), "crash.au");
}
}
repaint();
checkDaleks();
if ((drA) && (allDead))
levelComplete = true;
else if (!drA) {
prepareScreen();
repaint();
playerDead = true;
}
}
}
</PRE>
<!-- END CODE //-->
<P>First, the player’s move is converted from an integer to an ASCII character for simplification. Then the movePlayer(char) method is called. This method returns a boolean value—<I>true</I> if the player’s move was valid and <I>false</I> otherwise. If the player’s move checks out, several things can happen. If the player has chosen to make a last stand (by pressing “L”), the repaint() procedure is called. This will be examined in a later section.</P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Analyzing Input</FONT></H4>
<P>The movePlayer(char) method shown in Listing 14-9 takes care of the gritty details of keyboard processing. The end result of this method is a boolean value indicating whether or not the player’s move is a valid one.
</P>
<P><B>Listing 14-9</B> The daleks14.movePlayer(…) method</P>
<!-- CODE //-->
<PRE>
public boolean movePlayer(char input) {
int newX = drX;
int newY = drY;
int newF = drF;
boolean pass = false;
// player may elect to not move (hit '5')
boolean teleport = false;
// player may teleport (hit 'T' or 't')
boolean validMove = true;
switch (input) {
// numeric keypad movement options
case '1': newX--; newY++; newF = faceLeft; break;
case '2': newY++; break;
case '3': newX++; newY++; newF = faceRight; break;
case '4': newX--; newF = faceLeft; break;
case '5': pass = true; break;
case '6': newX++; newF = faceRight; break;
case '7': newX--; newY--; newF = faceLeft; break;
case '8': newY--; break;
case '9': newX++; newY--; newF = faceRight; break;
// alternative keys for movement
case 'N': newX--; newY++; newF = faceLeft; break;
case 'n': newX--; newY++; newF = faceLeft; break;
case 'M': newY++; break;
case 'm': newY++; break;
case ',': newX++; newY++; newF = faceRight; break;
case 'H': newX--; newF = faceLeft; break;
case 'h': newX--; newF = faceLeft; break;
case 'J': pass = true; break;
case 'j': pass = true; break;
case 'K': newX++; newF = faceRight; break;
case 'k': newX++; newF = faceRight; break;
case 'Y': newX--; newY--; newF = faceLeft; break;
case 'y': newX--; newY--; newF = faceLeft; break;
case 'U': newY--; break;
case 'u': newY--; break;
case 'I': newX++; newY--; newF = faceRight; break;
case 'i': newX++; newY--; newF = faceRight; break;
// other valid commands
case 'T': teleport = true; break;
case 't': teleport = true; break;
case 'S': screwdriverUsed = true; break;
case 's': screwdriverUsed = true; break;
case 'L': lastStand = true; break;
case 'l': lastStand = true; break;
default: validMove = false;
}
// check if the move is out-of-bounds
if ((newX<0) || (newX>maxX-1) || (newY<0) || (newY>maxY-1))
validMove = false;
// find an empty location if teleporting
if (teleport) {
boolean okLoc = false;
// loop until valid coordinates are found
while (!okLoc) {
okLoc = true;
newX = (int)(Math.random()*maxX);
newY = (int)(Math.random()*maxY);
for (int j=0; j<numDaleks; j++)
if ( ((newX==dalX[j])&&(newY==dalY[j])) &&
((dalA[j])||(dalR[j])) )
okLoc = false;
}
animTele = true;
}
// check if the player is allowed to use the screwdriver
if ((screwdriverUsed) && (screwdrivers==0)) {
validMove = false;
screwdriverUsed = false;
}
// check if attempting to move to a location containing rubble
for (int j=0; j<numDaleks; j++)
if ((newX==dalX[j]) && (newY==dalY[j]) && (dalR[j]))
validMove = false;
// if everything is ok, record new position & orientation
if (validMove) {
drX = newX;
drY = newY;
drF = newF;
}
return validMove;
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="599-602.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="605-608.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -