📄 611-615.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=611-615//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="608-611.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="615-616.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Better Housekeeping</FONT></H4>
<P>A bit of tidying up remains to be done before the turn can end. In particular, we need to evaluate whether the level has been completed. The handleKeyPress() method calls checkDaleks() to perform this function, shown in Listing 14-14.
</P>
<P><B>Listing 14-14</B> The daleks14.checkDaleks() method</P>
<!-- CODE SNIP //-->
<PRE>
public void checkDaleks() {
// evaluate whether all the daleks are dead
// and set the 'allDead' variable accordingly.
allDead = true;
for (int j=0; j<numDaleks; j++)
if (dalA[j])
allDead = false;
}
</PRE>
<!-- END CODE SNIP //-->
<P>This method begins by assuming that all the Daleks have been destroyed. It then checks each Dalek’s <I>dalA[x]</I> boolean variable. If the value of that variable is <I>true</I>, indicating the Dalek is still alive, we can no longer say all the Daleks have been destroyed, and the <I>allDead</I> variable is set to <I>false</I>.</P>
<P>Meanwhile, back at the handleKeyPress() method, the last few chores are completed, as given in Listing 14-15.</P>
<P><B>Listing 14-15</B> Status update portion of 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
...
checkDaleks();
if ((drA) && (allDead))
levelComplete = true;
else if (!drA)
playerDead = true;
}
}
</PRE>
<!-- END CODE //-->
<P>After the call to checkDaleks(), handleKeyPress() checks to see if the player is still alive and all of the Daleks have been destroyed. If so, the level is complete, and <I>levelComplete</I> is set to <I>true</I>. The next call to handleKeyPress() will now give control to the routines under the comment “set up for the next level”.</P>
<P>Another possibility is that the player has been captured. If this is the case, the boolean variable <I>playerDead</I> is set to <I>true</I>, and the next call to handleKeyPress() will resume animation of the title screen.</P>
<H3><A NAME="Heading18"></A><FONT COLOR="#000077">Customizing the Game</FONT></H3>
<P>The graphics routines in Daleks! were written to be flexible. In addition, users can adjust aspects of the game without having to return the source code, change variables, and recompile the classes. Allowing users to customize the applet means that different challenges and game themes can be created with ease.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Changing the Title Screen</FONT></H4>
<P>Several aspects of the animated title screen can be customized from HTML. The following HTML code includes tags that change various variables in the applet, as shown in Listing 14-16 and Figure 14-6.
</P>
<P><B>Listing 14-16</B> Modifying the intro screen</P>
<!-- CODE SNIP //-->
<PRE>
<applet code="daleks14.class" width=576 height=313>
...
<param name="title" value="Skeletons!">
<param name="flyingWidget" value="*">
<param name="numWidgets" value=300>
...
</applet>
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/14-06.jpg',589,326 )"><IMG SRC="images/14-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-06.jpg',589,326)"><FONT COLOR="#000077"><B>Figure 14-6</B></FONT></A> Giving the intro screen a custom look</P>
<P>The “Skeletons!” String replaces the usual “Daleks!” title. In addition, instead of animating 100 question marks, the animatedIntroClass object will display 300 asterisks. Currently the color and size of these Fonts cannot be changed from HTML.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Changing the Board Size</FONT></H4>
<P>In a similar way, the size of the board and the graphics displayed on the board can be changed. The code shown in Listing 14-17 sets new dimensions for the game board and the graphics in the game. By replacing the GIF files loaded by the applet, users can create new themes for the game, like the one shown in Figure 14-7.
</P>
<P><B>Listing 14-17</B> Adjusting board size through HTML</P>
<!-- CODE SNIP //-->
<PRE>
<applet code="daleks14.class" width=576 height=313>
...
<param name="maxX" value=18>
<param name="maxY" value=9>
<param name="imgH" value=32>
<param name="imgW" value=32>
...
</applet>
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/14-07.jpg',589,326 )"><IMG SRC="images/14-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-07.jpg',589,326)"><FONT COLOR="#000077"><B>Figure 14-7</B></FONT></A> Creating new themes with new GIFs</P>
<P>The values of the <I>maxX</I> and <I>maxY</I> parameters will become the new dimensions for the size of the game board in squares. The <I>imgH</I> and <I>imgW</I> parameters specify the size of the graphics to be displayed in those squares, in this case standard 32x32 Images. Note that the <I>height=</I> modifier in the <applet> tag is 25 pixels taller than the value of <I>imgH</I> * <I>maxY</I>. The extra height is necessary to create enough room for the information bar at the bottom of the screen.</P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Balancing Play</FONT></H4>
<P>The most important customizing options are those that change the nature of the game itself. While limited, Daleks! does allow several options to fine-tune the actual play of the game. Listing 14-18 shows one such possibility.
</P>
<P><B>Listing 14-18</B> Play-balancing through HTML</P>
<!-- CODE //-->
<PRE>
<applet code="daleks14.class" width=576 height=313>
...
<param name="maxX" value=18>
<param name="maxY" value=9>
<param name="levelMultiple" value=3>
<param name="baseNum" value=2>
<param name="maxEnemies" value=100>
...
</applet>
</PRE>
<!-- END CODE //-->
<P>As seen before, <I>maxX</I> and <I>maxY</I> can be changed to adjust the overall size of the board. The value of <I>levelMultiple</I> becomes the number of Daleks added each level, while <I>baseNum</I> is an unchanging number of Daleks. The formula used in the game is</P>
<!-- CODE SNIP //-->
<PRE>
numDaleks = level * levelMultiple + baseNum
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, <I>maxEnemies</I> puts an upper limit on the number of Daleks the player must contend with. This parameter can be used to keep the game from quickly getting out of hand (or to consciously let it get out of hand!).</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="608-611.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="615-616.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -