📄 207-211.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:Extending Your Video 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=6//-->
<!--PAGES=207-211//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="203-207.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="211-217.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>When <I>energy</I> reaches 0, GunManager invokes the gameOver() method in GameManager, which you will define in the next section. Otherwise, it sets displayHit, which tells GunManager’s paint() to paint a red flash when a hit occurs. The paint() method also provides a status indicator of energy remaining by drawing a rectangle whose width is equal to <I>energy</I>. Thus, the rectangle shrinks as the energy level decreases (which is cooler than simply displaying a number!).</P>
<P>Here is GunManager’s paint() method:</P>
<!-- CODE //-->
<PRE>
String energyString = "Energy";
public void paint(Graphics g) {
// if gun is hit, flash a red rectangle
// instead of painting gun
if (displayHit) {
g.setColor(Color.red);
g.fillRect(0,gun_y,width,gun_height);
displayHit = false;
}
else {
gun.paint(g);
}
missile.paint(g);
// display energy left
g.setColor(Color.red);
g.drawString(energyString,3,13);
g.fillRect(0,17,energy,10);
}
</PRE>
<!-- END CODE //-->
<P>Compile and try out these changes. (For now, use an empty stub for the gameOver() method in GameManager.) As you will see, the game action is basically finished! But there are two steps remaining before Alien Landing is a complete presentation.
</P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Creating an Opening and Closing</FONT></H3>
<P>Think of a game as a movie: There are the opening credits, which display the name of the game and the instructions, the actual playing of the game, and the closing credits. The James Bond movies hook you, right from the opening action sequence (sometimes the best part of the movie!). In similar fashion, you can create fantastic opening animations for your games that grab the player.
</P>
<P>You won’t do that here, but you will see how to modify the GameManager to create openings and closings. Figure 6-6 shows the GameManager structure we will implement.</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/06-07.jpg',467,398 )"><IMG SRC="images/06-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-07.jpg',467,398)"><FONT COLOR="#000077"><B>Figure 6-6</B></FONT></A> GameManager structure</P>
<P>The idea is to introduce variables that represent the state of the game, and to paint the game, or the opening or closing sequences, based on these variables. The boolean <I>playing</I> records whether the user is actually playing. The <I>screen</I> variable can be set to either INTRO or GAME_OVER. GameManager’s paint() method will check these variables and draw what’s appropriate.</P>
<P>Let’s see how these variables are manipulated. The constructor of GameManager first sets the initial values for these variables: The game is not playing, and the introduction screen should be displayed:</P>
<!-- CODE SNIP //-->
<PRE>
playing = false; // not playing
screen = INTRO; // show intro screen
</PRE>
<!-- END CODE SNIP //-->
<P>The player will move from the introduction mode to the playing mode, for example, by clicking the mouse. By modifying mouseDown(), you enable this behavior:
</P>
<!-- CODE //-->
<PRE>
public boolean mouseDown(Event e,int x,int y) {
if (playing) {
gm.fireMissile(x);
}
else if (screen == INTRO) { // start game for mouse
// down on intro screen
playing = true;
newGame();
}
else if (screen == GAME_OVER) { // else go back to intro
if (e.shiftDown()) { // if shift-click
screen = INTRO;
}
}
return true;
}
</PRE>
<!-- END CODE //-->
<P>The other mouse methods will also check if the game is playing before passing input to the GunManager.
</P>
<P>Now we need newGame() methods in all the manager classes, which will reset the parameters of the game. For example, GameManager’s newGame() will set the <I>score</I> back to 0; GunManager’s newGame() replenishes the player’s energy supply.</P>
<!-- CODE //-->
<PRE>
// GameManager’s newGame():
// initialize params for new game
public void newGame() {
score = 0; // no score
numLanded = 0; // no aliens landed
gm.newGame(); // call newGame in
um.newGame(); // manager classes
offscreen.setFont(smallFont); // set font in game
}
// GunManager’s newGame():
public void newGame() {
gun.setPosition(width/2-gun_width,gun_y);
gun.restore();
displayHit = false;
energy = maxEnergy;
}
</PRE>
<!-- END CODE //-->
<P>The opposite of newGame() is gameOver(), which is a GameManager method that is called when the player runs out of energy or too many aliens have landed and gameOver() triggers the closing sequence by setting <I>playing</I> and <I>screen</I>:</P>
<!-- CODE SNIP //-->
<PRE>
// handle game over
public void gameOver() {
if (playing) {
playing = false;
screen = GAME_OVER;
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, let’s see how to paint the opening and the closing credits. For the opening screen, we will draw instructions for the game. Let’s provide an opening animation that underlies the instructions, by including a call to the UFOManager’s paint(). This way, the aliens will fly around while the instructions appear. The following excerpt from the GameManager’s paint() method draws the introductory screen. The <I>introString[]</I> contains the text of the instructions.</P>
<!-- CODE //-->
<PRE>
... if (screen == INTRO) {
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,width,height); // clear buffer
...
um.paint(offscreen); // draw UFOs
...
offscreen.setColor(Color.magenta);
offscreen.setFont(mediumFont);
// draw instructions
for (int i=0; i<introString.length-1; i++) {
offscreen.drawString(introString[i],13,(3+i)*height/12);
}
offscreen.setColor(Color.green);
offscreen.drawString(introString[7],
(width-stringWidth)/2,
height*11/12);
g.drawImage(image,0,0,this);
}
</PRE>
<!-- END CODE //-->
<P>The code for the closing sequence follows the same idea. The closing animation will be provided by the UFOManager, and we will overlay the score and a <I>gameOverString</I>. The complete code for GameManager’s paint() is in Listing 6-1.</P>
<P>Alien Landing is finished! Now you can show it to your friends.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="203-207.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="211-217.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -