📄 667-669.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=667-669//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="664-667.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="669-673.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">Coding the StarField Class</FONT></H4>
<P>The StarField is quite simple, really. All we have to do is keep track of a big array of X and Y coordinates. At each coordinate, we draw a tiny little white line, on a black background, to represent a star. Because we will make StarField an extension of Sprite, we can use its “speed” to determine not only the amount each star will move, but also the length of each star. Thus, when the Sprite “warp” factor is set to a large number, the stars will appear to streak by at amazing warp speeds! Like the Sprite class, this first version of StarField will use simple onscreen graphics, but eventually we will convert it to double-buffered form.
</P>
<P>We begin with two integer arrays for the X and Y coordinates. We initialize the arrays so that they are full of -1. The method we will write for creating new stars will search the array for a star that is off the screen (that is, one with an X or Y coordinate that is less than zero).</P>
<P>Let’s get started:</P>
<!-- CODE //-->
<PRE>
import java.awt.*;
import java.lang.*;
public class StarField extends Sprite {
int x[],y[];
int NUM_STARS;
Graphics g;
StarField( int num, Rectangle r, Image im) {
this.im=im; // we’ll use this later for double-buffered graphics
DELAY = 300;
NUM_STARS = num;
bounds = r;
x = new int[num];
y = new int[num];
for(int i=0;i<NUM_STARS;i++) {
x[i]=-1;
y[i]=-1;
addStar(i);
}
}
</PRE>
<!-- END CODE //-->
<P>The initialization method makes a call to a method that adds a star to the array at position <I>i</I>, so let’s code that:</P>
<!-- CODE //-->
<PRE>
public void addStar(int min) {
int i,j;
for(i=0;i<NUM_STARS;i++)
if(x[i]==-1 && y[i]==-1) {
x[i] = bounds.x+min+(int)((bounds.width-min)*Math.[return]
random());
y[i] = bounds.y+(int)(bounds.height*Math.random());
}
}
</PRE>
<!-- END CODE //-->
<P>This picks a random Y position and a random X position (near the right edge) and adds the “star” to the coordinate arrays. Next let’s write a little method to “kill” a star that is no longer needed (i.e., one that has scrolled off the screen):
</P>
<!-- CODE SNIP //-->
<PRE>
public void killStar(int i) {
x[i]=-1;
y[i]=-1;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The default advance method for Sprite will obviously not work for StarField, so let’s code that next:
</P>
<!-- CODE //-->
<PRE>
public void advance() {
int i, spd = (int)(speedX+.5);
for(i=0;i<NUM_STARS-1;i++) {
x[i]-=spd;
if( !bounds.inside(x[i],y[i])) {
killStar(i);
addStar(bounds.width-50);
}
}
}
</PRE>
<!-- END CODE //-->
<P>Of primary importance is the painting method. We can safely assume that the applet will start with a nice black background, so all we have to do is erase the previous stars’ locations and then draw the new ones. As previously mentioned, we use Sprite’s default getSpeed() method to determine the length of each star (remember that getSpeed() takes the “warp” factor into account):
</P>
<!-- CODE //-->
<PRE>
public void paintSprite(Graphics g) {
int i;
Rectangle r=bounds;
g.setColor( Color.black);
g.fillRect( r.x,r.y,r.width,r.height);
for(i=0;i<NUM_STARS;i++)
if( r.inside(x[i],y[i])) {
g.setColor(Color.black);
g.drawLine(x[i]+getSpeed(),y[i], x[i]+2*(int)(speedX+.5), y[i]);
g.setColor(Color.white);
g.drawLine( x[i], y[i], x[i]+(int)(speedX+.5), y[i]);
}
}
</PRE>
<!-- END CODE //-->
<P>We’re done! This perhaps comes as a surprise, since we haven’t yet coded any of the Thread methods (run(), start(), stop). This is one of the great advantages of modular code design—the Sprite class (which is our superclass) takes care of all of that for us! Because our methods (like advance() and paintSprite()) use the same conventions as Sprite, the preexisting run() method is quite sufficient.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">Checking Out the Environment</FONT></H4>
<P>If you want to see what all that we have accomplished looks like put together, you should take the time now to code up a very simple applet that uses the classes as we’ve developed them so far. A simple applet that creates a StarField, Terrain, and Sprite is just fine. Although we won’t walk through the code for such a game right now (WordQuest should demonstrate how it all works), you can find the source code to a game called SpaceDeath on the CD-ROM that accompanies this book. That game shares many attributes with WordQuest (including some things we haven’t yet talked about), but isn’t nearly as refined. I would recommend it only as an example of how these classes can be used to create other games. If you want to really see them in action, move right along to the next section…
</P>
<H3><A NAME="Heading25"></A><FONT COLOR="#000077">On with the Quest</FONT></H3>
<P>It’s time to start talking about our primary goal for this chapter: WordQuest. There are many refinements that need to be made to the classes we just developed in order to get them ready for WordQuest. In addition, we need to create some new classes and the very lengthy WordQuest class itself. WordQuest is so long mainly because it has the responsibility of coordinating all of the various elements involved in the game: It must play referee, scorekeeper, and line judge. To accomplish all of this, we should start by creating some new classes to help with the task.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="664-667.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="669-673.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -