📄 199-203.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=199-203//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="193-199.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="203-207.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Modifying GameManager and UFOManager</FONT></H4>
<P>The GameManager needs to register the attacking and exploding animations with the MediaTracker so that they are loaded prior to the start of the game. This is done by declaring new variables and modifying the loadImages() method in GameManager. In the following code, all the GIFs are assumed to be in a directory image/, which is in the same location as the GameManager applet class.
</P>
<!-- CODE //-->
<PRE>
Image ufoImages[] = new Image[6]; // 6 ufo Images
Image attackImages[] = new Image[6]; // 6 attack Images
Image explodeImages[] = new Image[4];// 4 explode Images
// load all images used in game
public void loadImages() {
MediaTracker t = new MediaTracker(this);
gunImage = getImage(getCodeBase(),"image/gun.gif");
t.addImage(gunImage,0);
for (int i=0; i<ufoImages.length; i++) {
ufoImages[i] = getImage(getCodeBase(),
"image/ufo" + i + ".gif");
t.addImage(ufoImages[i],0);
attackImages[i] = getImage(getCodeBase(),
"image/attack" + i + ".gif");
t.addImage(attackImages[i],0);
}
for (int i=0; i<explodeImages.length; i++) {
explodeImages[i] = getImage(getCodeBase(),
"image/explode" + i + ".gif");
t.addImage(explodeImages[i],0);
}
...
}
</PRE>
<!-- END CODE //-->
<P>The GameManager also loads the explosion sound, which plays when a UFO blows up:
</P>
<!-- CODE SNIP //-->
<PRE>
try {
expsound = getAudioClip(getCodeBase(),"Explosion.au");
}
catch (Exception e) { }
</PRE>
<!-- END CODE SNIP //-->
<P>Now modify the GameManager’s init() and UFOManager’s constructor to pass the animations and explosion audio clip to the UFO constructor. You’ll find these changes in Listings 6-1 and 6-3.
</P>
<P>Compile and run these modifications. You can immediately see how the attacking and exploding animations improve the game simulation. Now, let’s add increasing levels of difficulty.</P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Adding Levels of Difficulty</FONT></H3>
<P>In video games that pit human players against computer opponents, the strength of the opponents increases as play continues. Usually, the game starts at a level that the novice player can handle, but grows more difficult so that an accomplished player will still feel challenged. And if you’re charging money each time the game gets played, as with arcade machines, you’ll want to make it almost impossible to play for a very long time!
</P>
<P>There are lots of ways to introduce skill levels into a game, and you’re limited only by your creativity. For a game such as Alien Landing, in which state machines try to destroy the player, here are four ways you can increase the difficulty:</P>
<DL>
<DD><B>•</B> Increase the number of UFOs. More aliens will appear on the screen as the game progresses.
<DD><B>•</B> Increase the velocity of the UFOs. This will make them harder to shoot.
<DD><B>•</B> Make the UFOs more intelligent. For example, the attacking aliens could aim for the player’s current location, instead of moving randomly.
<DD><B>•</B> Allow the UFOs to gain extra powers. They could start firing back at the player, or perhaps they might gain resistance to the player’s missiles.
</DL>
<P>The possibilities are endless, and they are not difficult to add to the Alien Landing simulation. For example, let’s see how you might implement the first option, increasing the number of aliens as the game continues. This change will require communication between the UFO sprite and the UFOManager. Figure 6-3 diagrams the communication channel we’ll add between these two classes. First, let’s see what changes are needed in the UFOManager.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/06-04.jpg',462,315 )"><IMG SRC="images/06-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-04.jpg',462,315)"><FONT COLOR="#000077"><B>Figure 6-3</B></FONT></A> Communication between UFO sprite and UFOManager</P>
<P>Define a new variable <I>levels</I>, which represents the number of UFOs active at a given moment. The player will progress from level to level, after killing a certain number of aliens, defined by the constant KILL_FOR_NEXT_LEVEL. The variable <I>ufosKilled</I> tracks the number of UFOs that the player has destroyed during the game. These variables are initialized in the UFOManager method newGame(), which also suspends the UFO sprites that are not needed for the starting level.</P>
<!-- CODE //-->
<PRE>
int ufosKilled; // count ufos killed
int level; // the level
// (i.e. #ufos on screen)
// kill 13 ufos until next level
static final int KILL_FOR_NEXT_LEVEL = 13;
// initialize parameters of new Game
public void newGame() {
ufosKilled = 0;
level = 2; // start with 2 ufos
// on the screen
for (int i=0; i<ufo.length; i++) {
initializePosition(ufo[i]);
if (i >= level) { // suspend ufos
// above start level
ufo[i].suspend();
}
}
}
</PRE>
<!-- END CODE //-->
<P>To move from level to level, the individual UFO sprites need a way of telling the UFOManager that they’ve been killed. We will add a public method to the UFOManager called killed() that the sprites can call. The killed() method updates <I>ufosKilled</I>; it also increases <I>level</I> every time <I>num_killed</I> is divisible by KILL_FOR_NEXT_LEVEL:</P>
<!-- CODE //-->
<PRE>
// tracks the number of ufos killed. If the
// num_killed is divisible by KILL_FOR_NEXT_LEVEL
// increment the level
public void killed() {
ufosKilled++;
if (ufosKilled % KILL_FOR_NEXT_LEVEL == 0) {
level = (level == NUM_UFOS) ? NUM_UFOS : level+1;
}
}
</PRE>
<!-- END CODE //-->
<P>When a UFO sprite is destroyed, it sends the killed() message to the UFOManager. The UFO class stores a reference to the UFOManager in variable <I>um</I>, so it can call killed() after it has been hit(). Here is the change to the UFO’s hit() method:</P>
<!-- CODE //-->
<PRE>
// this is called if a missile hits the alien
public void hit() {
// alien is invulnerable when it's attacking
// but it gets "pushed back"
if (state == ATTACK) {
locy -= 17;
}
// otherwise explode!
else if (state != EXPLODE) {
startExplode(); // start explode state
um.killed(); // tell UFOManager
// another UFO's dead
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="193-199.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="203-207.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -