📄 605-608.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=605-608//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="602-605.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="608-611.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The heart of this method is the <I>switch</I> command, which tests all the valid possibilities and sets the game variables accordingly. The various logical tests that follow the <I>switch</I> statement check the validity of the player’s proposed move (e.g., whether the player actually has any screwdrivers to use). If the player’s move is valid, the temporary values are copied into the actual variables and the method returns the value <I>true</I>. If the method returns <I>false</I>, the turn is aborted and the applet resumes waiting for keyboard input.</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">The Daleks Pursue</FONT></H4>
<P>Assuming the player has entered a valid command, the applet must move the Daleks toward the player, and check whether the player has been captured or all the Daleks have been destroyed. The handleKeyPress() method calls moveDaleks() and checkDaleks() to handle these functions, demonstrated in Listing 14-10.
</P>
<P><B>Listing 14-10</B> The daleks14.moveDaleks() method</P>
<!-- CODE //-->
<PRE>
public void moveDaleks() {
// move each dalek toward player
for (int j=0; j<numDaleks; j++)
// only move if dalek is alive
if (dalA[j]) {
if (dalX[j] < drX) dalX[j]++;
if (dalY[j] < drY) dalY[j]++;
if (dalX[j] > drX) dalX[j]--;
if (dalY[j] > drY) dalY[j]--;
if ((dalX[j]==drX) && (dalY[j]==drY))
// player has died
drA = false;
}
// check for collisions between daleks
newRubble = false;
for (int j=0; j<numDaleks; j++) {
for (int k=j+1; k<numDaleks; k++)
if ( (dalX[j]==dalX[k]) && (dalY[j]==dalY[k]) &&
((dalA[j]) || (dalR[j])) && ((dalA[k] || dalR[k])) )
{
if (dalA[j]) {
score += level * 2; newRubble = true; }
if (dalA[k]) {
score += level * 2; newRubble = true; }
dalA[j] = dalA[k] = false; // both daleks are dead
dalR[j] = dalR[k] = true; // both daleks are rubble
}
}
</PRE>
<!-- END CODE //-->
<P>The first loop evaluates every Dalek on the board. If the Dalek is alive (the value of the boolean variable <I>dalA[x]</I> is <I>true</I>), its position will be evaluated relative to the player’s current position. Each surviving Dalek is allowed to move to one adjacent square. Daleks destroyed with the sonic screwdriver or turned into rubble will not be moved, because the corresponding <I>dalA[x]</I> variable will be <I>false</I>. If a Dalek has moved onto the same square as the current player, the boolean variable <I>drA</I> is set to <I>false</I>, indicating the player has been captured.</P>
<P>The block of code evaluates the updated Dalek positions. Any Daleks that have attempted to move onto the same square must be turned into rubble. The outside <I>for</I> loop cycles through the entire array of active Daleks (numbered 0 to <I>numDaleks</I>). The inside loop checks the position of the Dalek pointed to by the outside loop against all higher-numbered Daleks. There is no need for both loops to cycle through the entire array, and doing so would have a number of negative effects. First, performance would be decreased. When many Daleks are on the current board, these nested loops take up a large proportion of processor time each turn. Second, if the inner loop cycled through the entire array, each pair of Daleks would be compared more than once. And Daleks would also be compared to themselves! Try tracing through the code by hand to get a feel for the logic behind the way the code is written.</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Programming Tip</B></FONT>
<P>The performance of a game can often be dramatically improved by paying close attention to the algorithms it uses. An algorithm is a set of logic that evaluates the status of the game and makes decisions. It is common for programmers to realize large performance increases simply by rewriting small portions of code.
</P>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Between-Move Animations</FONT></H4>
<P>Once a valid move is entered and the Daleks have moved, the handleKeyPress() method plays any appropriate sounds. The method plays audio files when Daleks crash into each other or the player uses the sonic screwdriver. Then the update() method is called with repaint(). Animations are drawn directly to the old game board for teleportation, the sonic screwdriver, or a level change. For example, the animateTele() method (shown in Listing 14-11) is called when the player presses “T” to teleport to a new location, as seen in Figure 14-4.
</P>
<P><B>Listing 14-11</B> The daleks14.animateTele(…) method</P>
<!-- CODE //-->
<PRE>
public void animateTele(Graphics g) {
// animate pairs of gray lines when teleporting
g.clipRect(0,0,maxX*imgW,maxY*imgH);
g.setColor(Color.lightGray);
for (int j=maxX*imgW+8; j>8; j-=8) {
g.drawLine(drX*imgW-j,drY*imgH-j,drX*imgW+j,drY*imgH-j);
g.drawLine(drX*imgW-j,drY*imgH+j,drX*imgW+j,drY*imgH+j);
pause(800);
}
animTele = false;
}
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/14-04.jpg',493,358 )"><IMG SRC="images/14-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-04.jpg',493,358)"><FONT COLOR="#000077"><B>Figure 14-4</B></FONT></A> Teleporting to a new location</P>
<P>The code clips the screen to the actual game board so that the information bar on the bottom is not drawn on. Then the <I>for</I> loop draws a series of horizontal lines starting from the edges of the board and moving toward the player. The routine was specifically written to be independent of the size of the current board. Instead of using absolute coordinates, the functions use <I>maxX</I>, <I>maxY</I>, <I>imgH</I>, and <I>imgW</I>, which can be easily changed. This is common throughout the graphics routines in the Daleks! classes, allowing the game board to be made larger or smaller by adjusting only a couple values instead of every graphics function in the applet.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="602-605.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="608-611.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -