📄 578-588.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:Building the JAVAroids 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=13//-->
<!--PAGES=578-588//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="575-577.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="588-590.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">The Game Manager</FONT></H4>
<P>The GameManager class contains the top-level structure of JAVAroids. The game opens with an introductory screen, which lists the point values of the objects in the game. After this, the player can test drive his ship, and learn the controls. Then, the player can start the game. The transition between these screens is triggered by a mouseUp event.
</P>
<P>GameManager implements the Runnable interface by defining a method called run(). The heart of the run() method is the Video Game Loop, which we covered back in Chapter 5, Building a Video Game. This loop coordinates the sequence of actions that take place in Figure 13-17. GameManager also defines handlers for key events, which it passes along to the ShipManager.</P>
<P>Finally, the GameManager source is presented in Listing 13-18. JAVAroids is finished!</P>
<P><B>Listing 13-18</B> GameManager class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// GameManager:
// responsible for tracking status of game and keeping score
//
/////////////////////////////////////////////////////////////////
public class GameManager extends Applet implements Runnable {
// variables for double buffering and animation
Thread animation;
Graphics gbuf;
Image im;
static final int REFRESH_RATE = 72; // in ms
// size of the game applet: change as needed
static final int MAX_HEIGHT = 400;
static final int MAX_WIDTH = 400;
// game parameters
private int score; // score
private boolean playing; // playing or not
private boolean gameOver; // is game over
private int intro_screen; // which screen
static final int EXTRA_SHIP = 10000; // #pts for extra ship
private int current_extra = 0; // counts extras
// constants for the introduction part of the game
static final int INSTRUCTIONS = 1;
static final int TEST_DRIVE = 2;
static final int INTRO_OVER = 3;
// references to manager classes
AstManager am; // asteroids
ShipManager sm; // ship
EnemyManager em; // enemy ships
EffManager efm; // effects
// sound
URL codebase;
AudioClip expsound = null;
// variables to monitor performance during game
Date time;
long t1,t2,dt;
// fonts
static Font bigfont = new Font("TimesRoman", Font.BOLD,24);
static Font medfont = new Font("TimesRoman", Font.PLAIN,18);
static Font smallfont = new Font("TimesRoman", Font.PLAIN,12);
/////////////////////////////////////////////////////////////////
// initialize applet
/////////////////////////////////////////////////////////////////
public void init() {
// initialize screen
setBackground(Color.black);
setFont(smallfont);
// initialize double buffer
im = createImage(MAX_WIDTH,MAX_HEIGHT);
gbuf = im.getGraphics();
// initialize sound
codebase = getCodeBase();
// System.out.println(codebase.toString());
resize(MAX_WIDTH,MAX_HEIGHT);
// initialize game state
gameOver = true;
intro_screen = 1;
playing = false;
}
/////////////////////////////////////////////////////////////////
// start applet
/////////////////////////////////////////////////////////////////
public void start() {
// load sound
try {
expsound = getAudioClip(getCodeBase(),"Explosion.au");
}
catch (Exception exc) {
System.out.println("Sound not loaded");
};
// initialize manager classes
efm = new EffManager(expsound);
sm = new ShipManager(MAX_WIDTH,MAX_HEIGHT,efm,this);
em = new EnemyManager(MAX_WIDTH,MAX_HEIGHT,sm,efm,this);
am = new AstManager(MAX_WIDTH,MAX_HEIGHT,sm,em,efm,this);
// start animation
if (animation == null) {
animation = new Thread(this);
if (animation != null) {
animation.start();
}
else
System.out.println("Insufficient memory to fork thread!");
}
}
/////////////////////////////////////////////////////////////////
// stop applet
/////////////////////////////////////////////////////////////////
public void stop() {
if (animation != null) {
animation.stop();
animation = null;
}
}
/////////////////////////////////////////////////////////////////
// set gameOver flag
/////////////////////////////////////////////////////////////////
public void setGameOver() {
gameOver = true;
}
/////////////////////////////////////////////////////////////////
// start a new game
/////////////////////////////////////////////////////////////////
private void newGame() {
// tell managers to start a new game
em.newGame();
am.newGame();
sm.newGame();
// set font for game
gbuf.setFont(smallfont);
// initialize parameters
current_extra = 0;
score = 0;
gameOver = false;
playing = true;
}
/////////////////////////////////////////////////////////////////
//
// Event Handlers
//
/////////////////////////////////////////////////////////////////
// if mouseUp occurs, proceed to next screen of introduction.
// if game's being played, restart the game.
public boolean mouseUp(Event e,int x,int y) {
if (intro_screen == INSTRUCTIONS) {
intro_screen = TEST_DRIVE;
sm.getShip().setPosition(385,75);
sm.getShip().restore();
}
// else restart the game
else if (intro_screen == TEST_DRIVE) {
intro_screen = INTRO_OVER;
newGame();
}
else
newGame();
return true;
}
// pass key events to the ShipManager
public boolean keyDown(Event e, int k) {
sm.keyDown(e,k);
return true;
}
// pass key events to the ShipManager
public boolean keyUp(Event e, int k) {
sm.keyUp(e,k);
return true;
}
/////////////////////////////////////////////////////////////////
// the game driver
/////////////////////////////////////////////////////////////////
public void run() {
startInstructions();
// the Video Game Loop
while (true) {
if (playing) {
time = new Date(); // track time
t1 = time.getTime();
if (!gameOver) {
sm.update(); // update ship
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -