📄 608-611.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=608-611//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="605-608.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="611-615.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Redrawing the Board</FONT></H4>
<P>Once the player and Daleks have moved, and the sounds and animations are done, it is time to draw the updated board to the offscreen bitmap. The update() method calls prepareScreen() to take care of this, given in Listing 14-12.
</P>
<P><B>Listing 14-12</B> The daleks14.prepareScreen() method</P>
<!-- CODE //-->
<PRE>
public synchronized void prepareScreen() {
// draw the current game status on the off-screen bitmap
offScreenGC.setColor(Color.white);
offScreenGC.clipRect(0,0,maxX*imgW,maxY*imgH+25);
offScreenGC.fillRect(0,0,maxX*imgW,maxY*imgH+4);
// draw info bar on bottom
offScreenGC.setColor(Color.darkGray);
offScreenGC.drawLine(0, maxY*imgH+1, maxX*imgW, maxY*imgH+1);
offScreenGC.drawLine(0, maxY*imgH+3, maxX*imgW, maxY*imgH+3);
offScreenGC.setColor(Color.lightGray);
offScreenGC.drawLine(0, maxY*imgH+2, maxX*imgW, maxY*imgH+2);
offScreenGC.setColor(Color.black);
offScreenGC.fillRect(0, maxY*imgH+4, maxX*imgW, 21);
if (doneLoading) {
offScreenGC.setFont(new Font("TimesRoman", Font.PLAIN, 12));
if (score > highScore)
highScore = score;
int yPos = maxY*imgH + 19;
offScreenGC.setColor(Color.white);
offScreenGC.drawString("SCORE: ", 5, yPos);
paintNumber(score, 51, yPos);
offScreenGC.drawString("SCREWDRIVERS: ", 124, yPos);
paintNumber(screwdrivers, 221, yPos);
offScreenGC.drawString("LEVEL: ", 260, yPos);
paintNumber(level, 303, yPos);
offScreenGC.drawString("HIGHSCORE: ", 338, yPos);
paintNumber(highScore, 415, yPos);
// draw the daleks
for (int j=0; j<numDaleks; j++) {
if (dalA[j]) { // if the dalek is alive..
checkFace(j);
offScreenGC.drawImage(dalekPic[dalF[j]],
dalX[j]*imgW,dalY[j]*imgH,this);
}
else if (dalR[j])
// if dalek is rubble..
offScreenGC.drawImage(rubblePic[dalF[j]],
dalX[j]*imgW,dalY[j]*imgH,this);
}
// draw the doctor
if (!playerDead)
offScreenGC.drawImage(doctorPic[drF+whichDoc],
drX*imgW,drY*imgH,this);
else {
offScreenGC.setColor(Color.white);
offScreenGC.fillRect(drX*imgW, drY*imgH, imgW, imgH);
offScreenGC.drawImage(deadPic, drX*imgW, drY*imgH, ⇐
this);
offScreenGC.setColor(Color.black);
offScreenGC.setFont(new ⇐
Font("Helvetica",Font.PLAIN,36));
offScreenGC.drawString("GAME OVER",maxX*imgW/2-⇐
100,75);
}
}
}
</PRE>
<!-- END CODE //-->
<P>A lot happens in this method. First the offscreen Image is covered with a fresh coat of white pixels to erase the previous graphics. Then the words and graphic are placed in the information bar to update the player’s status (see Painting by Number, below). The appropriate Dalek graphics are drawn to the game board. If a Dalek is alive, the right- or left-facing image is drawn, depending on its position relative to the player. If the Dalek has been reduced to rubble, a right- or left-facing rubble pile is drawn at its location.
</P>
<P>Finally, an Image for the player is drawn. The Image object drawn is dependent on the current level and the direction the player last moved. For instance, above level 5, the graphics for the second actor to play the role of Dr. Who are drawn. All possible player Images are contained in the <I>doctorPic[]</I> array, which is set up as shown in Table 14-1.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14-1</B> The doctorPic[] array
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="40%" ALIGN="LEFT">Array Index
<TH WIDTH="60%" ALIGN="LEFT">Array Contents
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>0
<TD>Left-facing graphic, first Doctor
<TR>
<TD>1
<TD>Right-facing, first Doctor
<TR>
<TD>2
<TD>Left-facing graphic, second Doctor
<TR>
<TD>3
<TD>Right-facing graphic, second Doctor
<TR>
<TD>…
<TD>…
<TR>
<TD>9
<TD>Right-facing graphic, fifth Doctor
<TR>
<TH COLSPAN="2"><HR>
</TABLE>
<P>The variable <I>drF</I> is either 0 (the player is facing left) or 1 (facing right), and the value of <I>whichDoc</I> points to the left-facing position of the currently active Doctor. The correct Image index can be calculated by adding both values together. If the player is facing right, and on level 5 (where <I>doctorPic[]</I> will equal 2), the calculated index value would be 3, indicating the Image object for the right-facing, second Doctor should be displayed.</P>
<P>If the player is captured, the words “Game Over” are displayed in large, imposing letters, and the graphic of a skull is shown in place of the normal player picture, as shown in Figure 14-5. In psychology, this is known as positive reinforcement, though players tend to view it as a fairly unpleasant experience.</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/14-05.jpg',495,359 )"><IMG SRC="images/14-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-05.jpg',495,359)"><FONT COLOR="#000077"><B>Figure 14-5</B></FONT></A> Captured—the end of the road</P>
<P>When the offscreen Image is complete, it is drawn to the screen in update() to replace the previous board. The update() method is now finished, and control is returned to handleKeyPress().
</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Painting by Number</FONT></H4>
<P>Sometimes very simple additions can greatly enhance the visual appeal of an applet. For Daleks!, the paintNumber(int) routine (shown in Listing 14-13) is used to draw the player’s current status using counter-style digits instead of the traditional awt.Font objects (which are limited in variety and rather plain-looking).
</P>
<P><B>Listing 14-13</B> The daleks14.paintNumber(int) method</P>
<!-- CODE //-->
<PRE>
public void paintNumber(int num, int xPos, int yPos) {
// draw the number as a graphic at the specified position
Integer numObj = new Integer(num);
String stringNum = numObj.toString();
for (int j=0; j < stringNum.length(); j++) {
char charDigit = stringNum.charAt(j);
int intDigit = Character.digit(charDigit, 10);
offScreenGC.drawImage(digit[intDigit],xPos+j*12, yPos-⇐
13,this);
}
}
</PRE>
<!-- END CODE //-->
<P>The first step is to convert the number from an integer to a String. The <I>for</I> loop then examines each successive character in the String, converts it to its integer equivalent, and paints the corresponding counter-style Image object to the offscreen bitmap. Unlike the rest of the Daleks! methods, this routine assumes that the counter digits will be twelve pixels in width. It would be a nice touch to make this routine independent of the size of the graphic digits used.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="605-608.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="611-615.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -