📄 651-656.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:WordQuest</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=16//-->
<!--PAGES=651-656//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch15/647-650.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="656-659.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 16<BR>WordQuest
</FONT></H2>
<P><I>Eric Ries</I></P>
<P>No doubt you’ve noticed that the most popular computer games on the market are violent shoot-’em-up, testosterone-charged games. There is, however, a large niche market for nonviolent games. Board and strategy games fall in this category, as does a third kind of game we have not yet discussed: educational games. The computer has proved itself a powerful tool for learning as well as entertainment, and the best results are often achieved through a combination of the two. In this chapter, we will create a Java game specifically designed to teach vocabulary, but which could easily be extended to teach a plethora of other concepts.
</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">What Is WordQuest?</FONT></H3>
<P>WordQuest combines certain elements of an action-adventure game with an educational purpose. For educational games, a believable plot is often unnecessary, but can add to the fun. In WordQuest, the player is on a secret nighttime mission to penetrate the enemy’s defenses. To successfully save his or her country/planet/galaxy from certain destruction, the player must pass through an advanced defense system designed to thwart would-be infiltrators by testing their vocabulary skills. As the player flies in a high-speed hovercraft, the enemy defense system will ask multiple-choice questions. The defense system uses massive force fields to thwart the player’s aim, but luckily, friendly agents have infiltrated the enemy base. These agents have managed to weaken each section of the force field at a specific point to allow the player to succeed in penetration. As the player approaches the force field, he or she will find that it has been divided into sections, each with an answer written on it. The user must “shoot” the correct answer to find the weak point in the force field and penetrate it. Figure 16-1 shows what the final game looks like.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/16-01.jpg',656,368 )"><IMG SRC="images/16-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-01.jpg',656,368)"><FONT COLOR="#000077"><B>Figure 16-1</B></FONT></A> WordQuest final product</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Plan of Attack</FONT></H4>
<P>WordQuest will be written in several stages. First, we are going to create a generic scrolling-action game environment that could be used for any kind of action game. This will help get the fundamentals of this type of game out of the way. This will also provide a useful set of classes that can be used and reused—a nice bonus for any program. Once we’ve got this generic game engine running to our satisfaction, we will proceed to alter it to meet the needs of an educational game. This will entail adding, removing, and changing some parts of our original code, but if we do a good job with the first part, we won’t have to do too much work.
</P>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Building an Action Environment</FONT></H3>
<P>The game we are creating takes place in a mythical realm in which the programmer gets to specify the laws of physics. Our goal, as always, is to create a set of classes that we can reuse later for any similar type of game. To this end, we are going to create a special Sprite class that can handle any kind of onscreen entity. By modifying and extending this class, we can easily create spaceships, birds, or flying oysters—all without having to do much more that make some cute graphics.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Simulating 3D</FONT></H4>
<P>We’re not going to write a 3D game in this chapter. However, we do want to give players the illusion that they are traveling forward at a tremendous speed. The two ways to simulate motion in a game of this type are to either move the user’s Sprite very fast, or to leave the player stationary and scroll the background very fast behind him or her. For this environment, we are going to choose the latter option.
</P>
<P>Whenever you drive, you’ll notice that when you look to the side, things that are closer to you appear to move quickly, while things far away appear to move slowly. We will take advantage of this property to fool the user. In the “foreground” we will scroll a fast-moving terrain, while in the background we will scroll a slow-moving star field that will give the illusion of night. These two things combined will give users the feeling that they are hurtling forward toward certain death, even though their “ship” is not moving at all.</P>
<P>This technique is very common. Video games systems, like Nintendo, make extensive use of it to achieve the very same illusion we seek. However, most games like that scroll a bitmapped background/foreground over and over again. This is obviously repetitive, and while it works pretty well, is not the best method. Because of some nifty Java features, we can have a <I>dynamic</I> background/foreground that is not repetitive, and has greater realism. We’ll discuss this implementation a little later.</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Understanding Sprite Theory</FONT></H4>
<P>Each Sprite will be its own thread. Each Sprite will be responsible for keeping track of its current location, moving appropriately, and drawing itself when requested by the applet. Furthermore, each Sprite should know what to do when it collides with another Sprite, and it should take action depending on the type of Sprite with which it collides. In order to accomplish this, we are going to need to assign each Sprite an ID number that designates what sort of sprite it is. That way, when two Sprites collide, each one simply checks what it has collided with and acts appropriately. For instance, if a “rock” Sprite collides with a “window” Sprite, you would expect the rock to emerge unscathed but the window to be completely destroyed. Game Sprites must exhibit these same behaviors.
</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Writing the Code</FONT></H3>
<P>Let’s jump right in and create a Sprite class with the basic features we’ve discussed so far. Once we’ve done that, we can figure out how to alter it to meet the needs of our specific game. Note that we will not make use of double-buffered graphics yet, as adding that later often leads to more efficient code.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch15/647-650.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="656-659.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -