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

📄 ch13.htm

📁 Java游戏开发
💻 HTM
📖 第 1 页 / 共 3 页
字号:
&nbsp;&nbsp;&nbsp;&nbsp;offGrfx = offImage.getGraphics();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;infoMetrics = offGrfx.getFontMetrics(infoFont);
<BR>
&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;// Draw the sprites<BR>
&nbsp;&nbsp;srv.draw(offGrfx);<BR>
<BR>
&nbsp;&nbsp;// Draw the game info<BR>
&nbsp;&nbsp;offGrfx.setFont(infoFont);<BR>
&nbsp;&nbsp;offGrfx.setColor(Color.white);<BR>
&nbsp;&nbsp;offGrfx.drawString(new String(&quot;Level: &quot;
+ level +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&quot;&nbsp;&nbsp;Caught: &quot; + caught
+ &quot;&nbsp;&nbsp;Lost: &quot; + lost), 10, 5 +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;infoMetrics.getAscent());<BR>
<BR>
&nbsp;&nbsp;// Is the game over?<BR>
&nbsp;&nbsp;if (lost &gt;= 5) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Font&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f
= new Font(&quot;Helvetica&quot;, Font.BOLD, 36);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;FontMetrics fm = offGrfx.getFontMetrics(f);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s
= new String(&quot;Game Over&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.setFont(f);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;offGrfx.drawString(s, (size().width -
fm.stringWidth(s)) / 2,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((size().height - fm.getHeight())
/ 2) + fm.getAscent());<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Stop the music<BR>
&nbsp;&nbsp;&nbsp;&nbsp;music.stop();<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Add a new scorpion?<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if ((rand.nextInt() % (20 - level / 2))
== 0)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv.add(new Scorpion(this,
1 -<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math.abs(rand.nextInt()
% 2), level));<BR>
<BR>
&nbsp;&nbsp;// Draw the image onto the screen<BR>
&nbsp;&nbsp;g.drawImage(offImage, 0, 0, null);<BR>
}</TT>
</BLOCKQUOTE>
<HR>
<P>
After drawing the sprites, the <TT>update</TT>
method draws the game information in the upper left corner of
the applet window. The game information includes the difficulty
level, the number of scorpions caught, and the number of scorpions
lost. It then checks the <TT>lost</TT>
member variable to see whether it is greater than or equal to
<TT>5</TT>. If so, the game has ended,
so <TT>update</TT> draws the <TT>Game
Over</TT> message and stops the music. If the game isn't
over, <TT>update</TT> determines whether
or not it should add a new scorpion. This determination is based
on the current level and a little randomness.
<P>
The mouse input in the game is handled by four different methods:
<TT>mouseEnter</TT>, <TT>mouseExit</TT>,
<TT>mouseMove</TT>, and <TT>mouseDown</TT>.
<TT>mouseEnter</TT> and <TT>mouseExit</TT>
show and hide the net sprite based on the mouse being inside or
outside the applet window:
<BLOCKQUOTE>
<TT>public boolean mouseEnter(Event evt,
int x, int y) {<BR>
&nbsp;&nbsp;if (net != null)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;net.show();<BR>
&nbsp;&nbsp;return true;<BR>
}<BR>
<BR>
public boolean mouseExit(Event evt, int x, int y) {<BR>
&nbsp;&nbsp;if (net != null)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;net.hide();<BR>
&nbsp;&nbsp;return true;<BR>
}</TT>
</BLOCKQUOTE>
<P>
Showing and hiding the net sprite based on the mouse being in
the applet window visually helps tie the net to the mouse pointer.
The <TT>mouseMove</TT> method simply
sets the position of the net to the position of the mouse, which
causes the net to follow the mouse around:
<BLOCKQUOTE>
<TT>public boolean mouseMove(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;if (net != null)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;net.setPosition(new Point(x - 10, y -
10));<BR>
&nbsp;&nbsp;return true;<BR>
}</TT>
</BLOCKQUOTE>
<P>
The last of the mouse input methods, <TT>mouseDown</TT>,
checks to see whether a scorpion has been caught by calling the
<TT>isPointInside</TT> method:
<BLOCKQUOTE>
<TT>public boolean mouseDown(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;if (lost &lt; 5) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Sprite s = srv.isPointInside(new Point(x
- 5, y - 5));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (s != null) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Remove the scorpion and
increase number caught<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv.removeElement(s);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((++caught % 15) == 0)
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Increase the
level and play applause sound<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level++;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;applause.play();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Play the net
hit sound<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;netHit.play();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Play the net miss sound
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;netMiss.play();<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return true;<BR>
}</TT>
</BLOCKQUOTE>
<P>
If no scorpion has been caught, the <TT>mouseDown</TT>
method plays the <TT>netMiss</TT>
audio clip. If a scorpion has been caught, the scorpion sprite
is removed from the list, and the <TT>caught</TT>
member variable is incremented. If <TT>caught</TT>
is divisible by <TT>15</TT>, <TT>level</TT>
is also incremented, and the <TT>applause</TT>
audio clip is played. This results in a new level being reached
for every 15 scorpions that are caught.
<P>
The keyboard input in Scorpion Roundup is only used to start a
new game or toggle the music on and off. The <TT>keyDown</TT>
method checks for these keys and takes the appropriate actions:
<BLOCKQUOTE>
<TT>public boolean keyDown(Event evt, int
key) {<BR>
&nbsp;&nbsp;if ((key == (int)'n') || (key == (int)'N'))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;newGame();<BR>
&nbsp;&nbsp;else if ((key == (int)'m') || (key == (int)'M')) {
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;musicOn = !musicOn;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (musicOn)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;music.loop();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;music.stop();<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return true;<BR>
}<BR>
Finally, you arrive at the newGame method:<BR>
void newGame() {<BR>
&nbsp;&nbsp;// Set up a new game<BR>
&nbsp;&nbsp;level = 1;<BR>
&nbsp;&nbsp;caught = lost = 0;<BR>
&nbsp;&nbsp;srv = new SRVector(new ImageBackground(this, back));
<BR>
&nbsp;&nbsp;net = new Sprite(this, netImage, new Point((size().width
-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;netImage.getWidth(this)) / 2, (size().height
-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;netImage.getHeight(this)) / 2), new Point(0,
0), 20,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Sprite.BA_WRAP);<BR>
&nbsp;&nbsp;srv.add(net);<BR>
&nbsp;&nbsp;if (musicOn)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;music.loop();<BR>
}</TT>
</BLOCKQUOTE>
<P>
The <TT>newGame</TT> method does everything
necessary to initialize and start a new game: The <TT>level</TT>,
<TT>caught</TT>, and <TT>lost</TT>
member variables are initialized, the sprite list is re-created,
and the net sprite is re-created and added back to the list. The
music is also restarted.
<P>
That wraps up the details of Scorpion Roundup, your second complete
Java game. You are fast becoming a Java game expert! However,
before you throw the book down and start hacking away at a game
of your own, make sure you fully understand how this game works.
I encourage you to try your hand at enhancing it and adding some
new features. For some enhancement ideas, check out the &quot;Exercises&quot;
section at the end of this lesson.
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
In today's lesson, you built your second complete Java game-Scorpion
Roundup. You began by learning a little background on the game,
followed by fleshing out the conceptual game design. With the
groundwork laid, you saw that it wasn't so bad moving on to the
actual game implementation. It was still a lot of work, but it
resulted in a pretty neat game that made use of mouse input, sound
effects, and music.
<P>
With another complete game under your belt, you're probably feeling
pretty invincible. It's a good thing too, because tomorrow you're
going to shift gears and tackle an often difficult and sobering
aspect of game programming-debugging. Tomorrow's lesson covers
all the big issues relating to hunting down and ridding your games
of bugs. But you don't need to worry about that now; go play a
few games of Scorpion Roundup and relax!
<H2><A NAME="QA"><B><FONT SIZE=5 COLOR=#FF0000>Q&amp;A</FONT></B></A>
<BR>
</H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Are scorpions really popular as pets?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Yes they are. If you're interested in adopting your own pet scorpion, the folks at Glades Herp, Inc. would be glad to help you out. They are on the Web and can be found at <TT><A 
HREF="http://www.tntonline.com/gherp/gherp.htm">http://www.tntonline.com/gherp/gherp.htm</A></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Why are the scorpions in the game colored green?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Because scorpion hunters use black lights to illuminate scorpions at night, thereby making them visible. The black light causes the scorpions to take on a greenish glow.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Why isn't the net sprite implemented as a new sprite class?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Because it doesn't require any new functionality beyond that provided by the <TT>Sprite</TT> class. You should make a strong effort to only derive new classes when you specifically need to add new functionality.

</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Why is the music in Scorpion Roundup so repetitive?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50>A</TD><TD>The music is implemented as a looped audio clip. Because audio clips tend to take up a lot of space and therefore take a while to transfer over an Internet connection, it is important to keep them as short as possible. 
Although it is short and repetitive, the music in Scorpion Roundup still manages to add an interesting dimension to the game without taking all day to transfer.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><B><FONT SIZE=5 COLOR=#FF0000>Workshop</FONT></B></A>
</H2>
<P>
The Workshop section provides questions and exercises to help
you get a better feel for the material you learned today. Try
to answer the questions and at least think about the exercises
before moving on to tomorrow's lesson. You'll find the answers
to the questions in appendix A, &quot;Quiz Answers.&quot;
<H3><A NAME="Quiz"><B>Quiz</B></A></H3>
<OL>
<LI>Which major U.S. airport is a good place to find scorpion
souvenirs?
<LI>Why do you need the <TT>SRVector</TT>
class?
<LI>Why do scorpions only come out at night?
<LI>Why is the music stopped in the <TT>stop</TT>
method of the <TT>ScorpionRoundup</TT>
class?
<LI>Why is the <TT>lost</TT> member
variable in the <TT>ScorpionRoundup</TT>
class declared as public static?
</OL>
<H3><A NAME="Exercises"><B>Exercises</B></A></H3>
<OL>
<LI>Order your own pet scorpion and give it a loving home.
<LI>Make the net a frame-animated sprite that gives the effect
of the net waving in the air.
<LI>Buy a black light and see whether your pet scorpion really
looks green.
<LI>Change the sound effects and music to use your own custom
audio clips.
<LI>Add some entirely new sound effects, such as a laughter sound
when a scorpion gets away.
<LI>Use the Scorpion Roundup code to create an entirely new game.
For example, you could change the graphics and sound, modify the
code a little, and turn the game into a target shooting game.
Just change the background to a picture of a sky, the net to cross-hairs,
and the scorpions to clay targets. Then modify the code so that
the clay targets arch through the air, and use a gunshot sound
instead of the swoosh sound used for the net. You'll have a whole
new game!
</OL>
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch12.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch14.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

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