⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 21天学会用JAVA开发网络游戏,这是一本英文书
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML>

<HEAD>
   <TITLE>Chapter 10 -- Traveling Gecko: Blistering Desert Fun</TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 10</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Travelin Gecko: Blistering Desert Fun</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="#DesigningTravelingGecko" >Designing Traveling Gecko</A>
<UL>
<LI><A HREF="#Sprites" >Sprites</A>
<LI><A HREF="#GamePlay" >Game Play</A>
</UL>
<LI><A HREF="#SampleAppletTravelingGecko" >Sample Applet: Traveling Gecko</A>
<UL>
<LI><A HREF="#TheSpriteClasses" >The Sprite Classes</A>
<LI><A HREF="#TheTravelingGeckoClass" >The TravelingGecko Class</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&amp;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>
On <A HREF="ch9.htm" >Day 9</A>, you learned all about handling
user input in Java. In this lesson, you combine what you learned
about user input with your knowledge of the sprite classes to
create your first complete Java game, Traveling Gecko. In doing
so, you learn and apply new techniques for extending the sprite
classes.
<P>
Today's lesson presents and solves the major technical issues
involved in putting together a complete Java game. By developing
a complete game, your core Java game programming skills come full
circle. You'll see that putting together a complete game isn't
really that much more complex than the sample sprite applets you've
already worked on. After you finish this lesson, you'll have the
fundamental Java game programming constructs firmly in place so
that you can move on to more advanced topics.
<H2><A NAME="DesigningTravelingGecko"><B><FONT SIZE=5 COLOR=#FF0000>Designing
Traveling Gecko</FONT></B></A></H2>
<P>
As you learned back in <A HREF="ch7.htm" >Day 7</A>, &quot;Sim
Tarantula: Creepy Crawly Sprites,&quot; it's very important to
think through a game design as thoroughly as possible before writing
any code. So, before you even consider editing a Java source file,
be sure to think about the game design in both general and specific
terms. With that in mind, let's break the Traveling Gecko sample
game into its logical components.
<P>
The Traveling Gecko game is modeled roughly on the classic Atari
2600 Frogger game. In the original Frogger game, you guide a frog
through traffic and then across a river using floating logs to
get across. Traveling Gecko takes a similar approach, in that
the goal is to maneuver an animal from one place to another while
dodging dangers along the way. However, the setting for Traveling
Gecko is the desert southwest, and your character is a gecko on
the move. Your journeying little gecko only wants to get across
a particularly small stretch of desert, but he has to contend
with a variety of predators to do so. The predators include Gila
monsters, scorpions, rattlesnakes, and tarantulas.
<H3><A NAME="Sprites"><B>Sprites</B></A></H3>
<P>
Based on the game description thus far, you probably already have
in mind some sprites that the game will need. Let's go ahead and
break the game down into sprites, because that's where most of
the substance of the game is located. Obviously, the most important
sprite is the gecko sprite itself, which needs to be able to move
based on user input. The gecko sprite is the heart of the game
and must be designed with care.
<P>
Because the gecko is capable of being killed by the predators,
you'll also need an animation of the gecko dying-a geckocide sprite.
If you recall, you used a similar approach (spidercide sprite)
when developing the Sim Tarantula applet on <A HREF="ch7.htm" >Day 7</A>.
The geckocide sprite simply shows an animation of the gecko dying
so that it doesn't just disappear when it dies.
<P>
Moving along, it's fairly obvious that you'll also need sprite
objects for the predators. Although each one has basically the
same functionality, let's go ahead and think of them as different
sprite objects, because you might decide to add unique behavior
to one of them later. You should have some special logic for handling
a collision between the predators and the gecko, because this
contact results in the gecko's death.
<P>
Before you finish itemizing the sprites, take a moment to think
about the specifics surrounding the gecko's path across the desert.
Taking an approach similar to Frogger's, the gecko must travel
from the bottom of the screen to safety at the top. However, it
seems too easy to simply have him go from the bottom of the screen
to the top with no other obstacles than the predators. Frogger
has specific locations at the top of the screen where the frog
must go. Let's take a similar approach here. By placing large
rocks at the top and bottom of the screen, you can provide openings
at which the gecko can start and finish. This makes sense too,
because the openings in the rocks make good hiding places for
the gecko.
<P>
If you're now thinking that the rocks would make good additions
to the sprite inventory for Traveling Gecko, then pat yourself
on the back! If not, don't feel too bad; it might be because you
think they could just be made part of the background. That's true,
but there would be a big problem in detecting collisions between
the gecko and the rocks. The rocks are there for a reason-to limit
the gecko's movement. And the only way to limit the gecko's movement
is to detect a collision between him and a rock and not let him
move if he's colliding with a rock. Without making the rocks sprites,
you would have to add a bunch of special case code to a derived
<TT><FONT FACE="Courier">SpriteVector</FONT></TT> class to see
whether the gecko is colliding with them. Adding code to a derived
<TT><FONT FACE="Courier">SpriteVector</FONT></TT> class isn't
the problem, though; the problem is duplicating the collision
detection functionality you've already written.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The discussion about the rock sprite brings up a good point in regard to game objects: Practically any graphical object in a game that can be interacted with or handled independently of the background should be implemented as a sprite. Remember that 
sprites are roughly analogous to cast members in a theatrical play. To carry things a bit further, you can extend the usage of sprites to also include the props used in a play. This is essentially the role rocks play in the Traveling Gecko game: 
props!</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<P>
The rocks are the last sprites you'll need to write for the game.
To summarize what you have thus far, Traveling Gecko requires
sprites modeling the following objects:
<UL>
<LI>Gecko
<LI>Geckocide
<LI>Gila monster
<LI>Scorpion
<LI>Rattlesnake
<LI>Tarantula
<LI>Rock
</UL>
<P>
The gecko sprite models the player and is controlled by the player's
user input responses. The geckocide sprite is used to show a dying
gecko and comes in the form of a simple frame animation. The Gila
monster, scorpion, rattlesnake, and tarantula sprites model the
predators who are trying to ruin the gecko's trip. Remember that
there has to be some method of killing the gecko based on a collision
with these predators. This is an issue you'll deal with later
in this lesson, when you get into writing the Java code. Finally,
the rock sprite models rocks that block the gecko's movement,
thereby making it more difficult for him to get across the desert
safely.
<H3><A NAME="GamePlay"><B>Game Play</B></A></H3>
<P>
Now that you have an idea of what sprite classes you need to write,
let's take a look at the game itself and how it will play. First,
it wouldn't be much fun if the game ended as soon as you were
killed by a predator. So let's give the player four geckos (lives)
to play with; the game isn't over until all four are killed.
<P>
Although it is certainly fulfilling to help out a gecko in need,
it would also be nice to reward the player with some type of point
system. Let's give the player 25 points each time the gecko makes
it safely across the desert. Then the player's good will for saving
a gecko's life is given a numeric value that can be viewed with
pride!
<P>
Because every game ultimately ends when all four geckos are killed,
you also need to provide the player with a way to start a new
game. This is an ideal situation for a button; the player simply
clicks the button to start a new game.
<P>
This finishes the game design for Traveling Gecko. You now have
all the information you need to get into the specifics surrounding
the applet and support classes. What are you waiting for?
<H2><A NAME="SampleAppletTravelingGecko"><B><FONT SIZE=5 COLOR=#FF0000>Sample
Applet: Traveling Gecko</FONT></B></A></H2>
<P>
The Traveling Gecko applet is your first complete Java game and
makes the most of the indispensable sprite classes you've come
to know so well. Figure 10.1 shows the Traveling Gecko applet
in the middle of a heated game.
<P>
<A HREF="f10-1.gif" ><B>Figure 10.1 : </B><I>The Traveling Gecko sample applet.</I></A>
<P>
Traveling Gecko begins by creating the gecko, rocks, and predators.
You then use the keyboard to control the gecko and attempt to
guide him safely into the rock opening at the top right of the
screen. The score is displayed in the upper left corner of the
screen. Immediately to the right of the score is the number of
remaining gecko lives, which are displayed graphically as tiny
geckos.
<P>
The different predators all roam around the desert background
at different speeds hoping to make a quick meal out of your trusting
gecko. If one of them gets lucky, a geckocide object is created
to show the dying gecko. The number of remaining lives is then
decremented.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Warning</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Watch out for those pesky scorpions; they're quite fast!</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<P>
If you guide the gecko safely across, you receive 25 points and
the chance to help him across again. I know, one would think that
the gecko would be thankful for making it across once and not
want to try again, but that's not the case! If you're able to
help him across a few times, you'll notice that the predators
start calling in reinforcements to make things more difficult.
<P>
If you manage to lose all four of your geckos to the predators,
the game ends and you see a message indicating that the game is
over. Figure 10.2 shows Traveling Gecko when a game has just ended.
<P>
<A HREF="f10-2.gif" ><B>Figure 10.2 : </B><I>A Traveling Gecko game that has come to an end.</I></A>
<P>
At this point, all you have to do is click the New Game button
with the mouse, and everything starts over. If you haven't checked
it out yet, now might be a good time to grab the accompanying
CD-ROM and try the game out for yourself. The complete source
code, executable, and images for the Traveling Gecko game are
included on the CD-ROM. If you just can't wait to find out all
the gory details, then by all means skip the CD-ROM and read on!
<H3><A NAME="TheSpriteClasses"><B>The Sprite Classes</B></A></H3>
<P>
As you probably guessed, the heart of the Traveling Gecko applet
is the extended sprite classes. The first of these classes is
the <TT><FONT FACE="Courier">Gecko</FONT></TT> class, which models
the gecko that is controlled by the player. The <TT><FONT FACE="Courier">Gecko</FONT></TT>
class is derived straight from <TT><FONT FACE="Courier">Sprite</FONT></TT>.
You might think that it would make more sense to derive <TT><FONT FACE="Courier">Gecko</FONT></TT>
from <TT><FONT FACE="Courier">DirectionalSprite</FONT></TT> (<A HREF="ch7.htm" >see Day 7</A>),
because a gecko clearly should face and move in different directions.
This is logical thinking, but the gecko's movement is limited
to up, down, left, and right. The <TT><FONT FACE="Courier">DirectionalSprite</FONT></TT>
class is geared more toward objects that can spin around and move
in different directions, including diagonal directions.
<P>
However, there is a drawback to not deriving the <TT><FONT FACE="Courier">Gecko</FONT></TT>
class from <TT><FONT FACE="Courier">DirectionalSprite</FONT></TT>:
The gecko can't face in the direction it is moving. That is why
the gecko is always facing upward, regardless of its movement.
This is a little unrealistic because most geckos probably don't
sidestep, but it makes things easier to implement. This is one
of those cases in which you sometimes have to make sacrifices
in detail for the sake of making the code simpler.
<P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -