📄 ch13.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 13 -- Scorpion Roundup: Action By Moonlight</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 13</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Scorpion Roundup: Action By Moonlight</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#DesigningScorpionRoundup" >Designing Scorpion Roundup</A>
<UL>
<LI><A HREF="#Sprites" >Sprites</A>
<LI><A HREF="#GamePlay" >Game Play</A>
<LI><A HREF="#Options" >Options</A>
</UL>
<LI><A HREF="#SampleAppletScorpionRoundup" >Sample Applet: Scorpion Roundup</A>
<UL>
<LI><A HREF="#TheSpriteClasses" >The Sprite Classes</A>
<LI><A HREF="#TheScorpionRoundupClass" >The ScorpionRoundup Class</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
Yesterday you saw how sound is supported in Java. You also wrote
a fairly simple applet to demonstrate how sound can be used in
a creative way. Today you go a step further by creating your second
complete game, including sound effects and music. The entire lesson
today is devoted to the design and development of this game, which
will provide you with another invaluable Java game creation experience.
<P>
The game you develop today is called Scorpion Roundup, and it
uses the all too familiar sprite classes to implement its animation
and sprite interactions. It uses the <TT>AudioClip</TT>
class you learned about in yesterday's lesson to represent both
sound effects and music. Unlike the Traveling Gecko game you developed
a few days ago, which used the keyboard exclusively for user input,
Scorpion Roundup uses the mouse as the user input device. You'll
see why when you get into the game.
<P>
The following topics are covered in today's lesson:
<UL>
<LI>Designing Scorpion Roundup
<LI>Sample applet: Scorpion Roundup
</UL>
<H2><A NAME="DesigningScorpionRoundup"><B><FONT SIZE=5 COLOR=#FF0000>Designing
Scorpion Roundup</FONT></B></A></H2>
<P>
Unlike Traveling Gecko, Scorpion Roundup isn't directly modeled
after any other game. Before getting into how the game plays,
you need some background on the premise surrounding the game,
because it is based on a very real concept that is pretty interesting.
<P>
Scorpions are fairly popular as pets. Not quite as popular as
dogs or cats, but what do you expect? They are also useful in
captivity for retrieving their poison, which is used in developing
antivenin medicine for people and pets stung by them. They are
also used to make souvenirs; just visit Sky Harbor Airport in
Phoenix, Arizona, and you'll see plenty of scorpions frozen in
plastic in the gift shops. The point here is that there are a
variety of human uses for scorpions. And where there's a demand,
there's a supply. This means that someone has to take on the job
of heading out into the desert and catching the rascals.
<P>
Catching a scorpion isn't as easy as you might think, though.
They are nocturnal creatures, so they only come out at night.
The thought of taking off into the desert at night might not appeal
to everyone, but conquering your fear of the dark desert isn't
the only hurdle when hunting for scorpions: It's hard to find
them at night! One of those brave souls that head into the darkness
in search of scorpions figured out a neat approach to finding
scorpions in the dark-using a black light. Black lights give off
a greenish glow that illuminates certain objects, including scorpions.
Therefore, to catch scorpions you simply head out into the darkness
with a black light and a net. That's the premise of the Scorpion
Roundup game. You're a scorpion hunter armed with a net, working
within a landscape illuminated by a black light that shows the
scorpions with their greenish glow.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Warning</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Real scorpions are caught by real professionals. For your own safety, I suggest only trying to catch the Java scorpions you meet in today's lesson!</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Based on this description of the game, you might already see why
the mouse is the ideal input device for the game. This is because
you are controlling a net, which is a hand-held object in real
life. The best way to handle controlling a net with the mouse
is to make the net a sprite. You also need a scorpion sprite to
model the scorpions that you are trying to catch.
<P>
At this point, the game is defined enough to move into more specifics.
Let's start by taking a look at the sprites in more detail.
<H3><A NAME="Sprites"><B>Sprites</B></A></H3>
<P>
You've established that the game requires two types of sprites
for modeling the net and the scorpions. You also know that the
net sprite is to be controlled by the mouse. Basically, all you
need the net sprite to do is follow the mouse around, which requires
very basic sprite movement. Based on this requirement of the net
sprite, it doesn't sound like you need to use a derived class
for it; you don't need any functionality beyond that provided
in the base <TT>Sprite</TT> class.
This is a correct assumption because deriving a new class is usually
only necessary when you need to add new functionality.
<P>
This means that Scorpion Roundup really only needs one derived
sprite class: the <TT>Scorpion</TT>
sprite class. What should this sprite do? To make the game a little
more simple to implement, let's limit the scorpion sprites to
traveling in horizontal directions only. The scorpions can run
in either the left or right direction, which means that they can
also face in either the left or right direction. You're probably
thinking that the directional sprite you developed last week might
work well here. Unfortunately, it won't work in this case because
it was specifically designed for sprites having exactly eight
directions; the scorpions in Scorpion Roundup only have two directions
(left and right).
<P>
The scorpion sprite needs to be frame animated so that the scorpions
look like they are moving their legs and running. You've also
established that the sprite needs two directions. Anything else?
Actually, there is one other thing. The goal of the game is to
catch as many scorpions as possible. However, you haven't established
how a game is lost; the game has no negative result when you don't
catch any scorpions. One solution is to have the scorpions get
away when they reach the other side of the screen, rather than
wrapping around. Furthermore, you could track how many scorpions
get away and end the game when a certain number of them escape.
The only place to determine when a scorpion has made it across
is within the <TT>Scorpion</TT> class.
Therefore, the <TT>Scorpion</TT> class
needs some method of determining when a scorpion has escaped and
modifying a value accordingly.
<H3><A NAME="GamePlay"><B>Game Play</B></A></H3>
<P>
Now that you understand how the sprites work in the game, let's
move on to the specifics of the game itself. You've established
that the goal of the game is to use a net to catch scorpions that
are running across the screen. You lose the game when you miss
a certain number of scorpions. You never really win; you just
try to catch as many scorpions as possible.
<P>
One thing you haven't covered is how the player is to be challenged
as the game goes on. Few games remain fun without increasing the
challenge as the play progresses, and Scorpion Roundup is no different.
The easiest way to make the game harder is to speed up the scorpions
themselves. You could also increase the speed at which the scorpions
are created; more scorpions on the screen at a time mean more
work for the player.
<P>
How do you establish when to increase the difficulty of the game?
Well, you could just do it behind the scenes based on time or
on how many scorpions have been caught. I like the latter approach
because it directly increases the difficulty based on the performance
of the player. The only catch is that most game players like to
know when they have progressed to another difficulty level. This
is easy enough to accommodate; just display the current level
along with the number of scorpions caught and lost. You then increment
the difficulty level when a certain number of scorpions have been
caught.
<H3><A NAME="Options"><B>Options</B></A></H3>
<P>
That wraps up the play aspects of the game, but you should consider
a few other small issues related to how the player controls the
game. The first one is how to start a new game. You might recall
that the Traveling Gecko game you developed a few days ago used
a New Game button to start new games. That approach was fine in
Traveling Gecko because the button was drawn on top of one of
the rocks. In Scorpion Roundup, the game appearance would suffer
more by having a button drawn on top of everything. The easy way
around starting a new game without using a button is to simply
allow the player to start a game with a certain key press, such
as the N key.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You actually could use buttons without covering up any of the game area by using an awt <TT>Canvas</TT> object as the game area, rather than the applet window. This is a good way to handle sharing the applet window between user interface controls and the
game area, but it involves more complexity. I didn't want this added complexity to make the game implementation more difficult to understand.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The only other issue in Scorpion Roundup that needs to be addressed
is music. Because the game uses looped music, it would be nice
for the player to be able to easily turn it on and off. Using
the keyboard approach again, the M key makes perfect sense as
a music toggle key.
<P>
That wraps up the design phase for Scorpion Roundup. Hopefully,
you're now anxious to dive into the details of implementing all
these cool ideas to build a real game!
<H2><A NAME="SampleAppletScorpionRoundup"><B><FONT SIZE=5 COLOR=#FF0000>Sample
Applet: Scorpion Roundup</FONT></B></A></H2>
<P>
The Scorpion Roundup applet is your second fully functional Java
game and shows off the sound skills you developed in yesterday's
lesson. Figure 13.1 shows a screen shot of a fast and furious
game of Scorpion Roundup. The complete source code, executable,
images, and sounds for Scorpion Roundup are located on the accompanying
CD-ROM.
<P>
<A HREF="f13-1.gif" ><B>Figure 13.1 :</B> <I>The Scopion Roundup sample applet in action.</I></A>
<P>
Scorpion Roundup begins by creating the net sprite, which you
can move around the play area with the mouse. Scorpions then begin
to run across the screen. If you click on a scorpion with the
net, you hear a sound indicating that you got him. If you miss,
you hear a sound of the net swishing through the air. All the
while, the music is playing in the background.
<P>
The current difficulty level is displayed in the upper left corner
of the screen, along with the number of scorpions caught and lost.
The level is incremented and a cheering sound is played each time
you catch 15 new scorpions. The scorpions start running faster
and appearing quicker with each increasing difficulty level.
<P>
If you let five scorpions get away, the game ends and you see
a <TT>Game Over</TT> message. Figure
13.2 shows Scorpion Roundup right after the game ends.
<P>
<A HREF="f13-2.gif" ><B>Figure 13.2 : </B><I>A Scorpion Roundup game that has come to an end.</I></A>
<P>
At this point, you can simply press the N key to start a new game.
If you haven't run Scorpion Roundup yet, please load the CD-ROM
and try it out. If you're not the type that responds well to the
word "please," then by all means skip playing the game
and read on!
<H3><A NAME="TheSpriteClasses"><B>The Sprite Classes</B></A></H3>
<P>
The only derived <TT>Sprite</TT> class
used in Scorpion Roundup is the <TT>Scorpion</TT>
class, which models a horizontally running scorpion. Listing 13.1
shows the source code for the <TT>Scorpion</TT>
class.
<HR>
<BLOCKQUOTE>
<B>Listing 13.1. The </B><TT><B><FONT FACE="Courier">Scorpion</FONT></B></TT><B>
class.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>public class Scorpion extends Sprite
{<BR>
public static Image[][] image;<BR>
private static Random rand = new Random(System.currentTimeMillis());
<BR>
<BR>
public Scorpion(Component comp, int dir, int speedInc)
{<BR>
super(comp, image[dir], 0, 1, 1, new Point((dir
== 0) ?<BR>
(comp.size().width - image[dir][0].getWidth(comp))
: 0,<BR>
60 + Math.abs(rand.nextInt()
% 5) * 44), new Point((dir == 0)<BR>
? (-5 - speedInc) : (5 + speedInc),
0), 10,<BR>
Sprite.BA_DIE);<BR>
}<BR>
<BR>
public static void initResources(Applet app, MediaTracker
<BR>
tracker, int id) {<BR>
image = new Image[2][2];<BR>
for (int i = 0; i < 2; i++)<BR>
for (int j = 0; j < 2;
j++) {<BR>
image[i][j] =
app.getImage(app.getCodeBase(),<BR>
"Res/Scorp"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -