📄 790-794.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:The Game of Worm</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=20//-->
<!--PAGES=790-794//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="787-790.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="794-798.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Multithreading Issues</FONT></H4>
<P>Since Java code can be event-driven while supporting multiple threads of execution, we must be careful to design with functions that return data that is valid over the scope of the query. This usually means not storing state information that is dependent on event triggers, such as direction changes. The consequence could be lost segments and unexpected behavior.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Java Programming Gotchas</FONT></H4>
<P>Java is an object-oriented language based on C++. As any C++ programmer discovers when programming Java, pointers are not supported, and returning values from functions has limitations. Objects cannot be used without being created using the <I>new</I> operator. Since C++ also has a <I>new</I> operator for pointers, the programmer has to remember that these are not pointer manipulations, but object manipulations that can extend across object boundaries.</P>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Returning Values</FONT></H4>
<P>Values can only be returned by either changing a passed-in object’s attributes or by a return value. Passing in an object by reference and assigning it a new object will not change the original object.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Object Instance Assignments</FONT></H4>
<P>Be careful how you return objects from functions. If your return value is a global object (it will not go out of scope) and it is assigned to another object, and that object is assigned to a third object, the original object is deleted and now points to the third object. Assigning objects in Java is common, but care should be exercised to prevent unexpected game behavior.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Optimizing Performance Under Java</FONT></H4>
<P>Worm will be implemented with a minimum of allocations during game play to minimize garbage collection effects impacting the performance of the game. Each allocation requires a call to allocate storage and another later to free the storage by Java’s garbage collection subsystem. If we can do this once at game startup, then these hidden effects are minimized.
</P>
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Worm Game Class Components</FONT></H3>
<P>The game of Worm is comprised of three classes: Worm, SessionManager, and WormBin. The Worm class keeps track of attributes, segment locations, and head direction. We call its functions to change direction and let it update its current position. We can query it to get its current length or get the coordinates to all of its points. SessionManager keeps track of the contents of the playing surface. It interfaces with the player, the worm, and the treat and delivers information to be displayed. WormBin interfaces to the Java APIs for player input and display output and implements the worker thread for real-time gaming. WormBin does not interface directly with the Worm class but goes through the SessionManager class if it needs to do a repaint on all worm segments. WormBin is a class derived from Java’s Applet class. This relationship is diagrammed in Figure 20-7.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/20-07.jpg',465,401 )"><IMG SRC="images/20-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/20-07.jpg',465,401)"><FONT COLOR="#000077"><B>Figure 20-7</B></FONT></A> Worm class relationships</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Constructing a Worm</FONT></H4>
<P>The worm is defined by the variables passed into the constructor for the class Worm. Listing 20-1 shows the constructor for the class Worm; the parameters are defined in Table 20-1.
</P>
<P><B>Listing 20-1</B> Worm class construction</P>
<!-- CODE //-->
<PRE>
public class Worm {
Dimension m_points[];
int m_startidx, m_endidx;
int m_curridx;
int m_startx, m_starty;
int m_wormsize = 4;
int m_wormlen = 5;
int m_startxdir, m_startydir;
int m_regrow = 0;
boolean m_nextstop = false, m_lastdropped = false;
boolean m_nextadded = false;
Dimension dDir;
Dimension dCurr;
Dimension dNext;
Dimension dNewpoint;
Dimension dOldpoint;
Color wormcolor;
// first time initialization
//
public Worm ( int startx,
int starty,
int wormlen,
Color wclr,
int wormsize,
int initx,
int inity)
{
wormcolor = wclr;
m_wormsize = wormsize;
m_wormlen = wormlen;
m_startxdir = initx;
m_startydir = inity;
// set up storage array for worm's description
m_points = new Dimension[m_wormsize];
for (int i = 0; i < m_points.length; i++)
m_points[i] = new Dimension();
// save start location for worm
m_startx = startx;
m_starty = starty;
// helper dimensions
dDir = new Dimension();
dCurr = new Dimension();
dNext = new Dimension();
dNewpoint = new Dimension();
dOldpoint = new Dimension();
}
</PRE>
<!-- END CODE //-->
<TABLE WIDTH="100%">
<CAPTION ALIGN=LEFT><B>Table 20-1</B> Worm constructor parameters
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="25%" ALIGN="LEFT">Variable
<TH WIDTH="75%" ALIGN="LEFT">NameMeaning
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>startx
<TD>X position to place tail of worm
<TR>
<TD>starty
<TD>Y position to place tail of worm
<TR>
<TD>wormlen
<TD>Initial length of worm in number of segments
<TR>
<TD>wclr
<TD>Color of worm
<TR>
<TD>wormsize
<TD>Maximum growth of worm in number of segments
<TR>
<TD>initx
<TD>Initial x direction that worm is pointing
<TR>
<TD>inity
<TD>Initial Y direction that worm is pointing
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Adding a Queue</FONT></H4>
<P>The worm’s queue, or ring buffer, represents an ordered set of body segment coordinate locations. We can set the maximum size of the worm to any value we wish during construction; however, creating a large buffer may needlessly allocate memory if the final worm size is significantly less. The Java class Dimension, although intended as a size object, can also be used as a position object providing x and y coordinates using the width and height member variables, respectively. Referring again to Listing 20-1, you will see a variable for an array of Dimension objects declared as <I>Dimension m_points[]</I>. This variable must first be initialized with a call to <I>new Dimension[m_wormsize]</I> indicating we want to create an empty array of size <I>m_wormsize</I>. Next, each element of this new array must be initialized by calling <I>new</I> again for the desired number of Dimension objects. This is done in the <I>for</I> loop.</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD>
<B>A Note About Variable Names</B>
<P>The Worm code uses a common convention of prefacing member variables with an <I>m_</I> to help distinguish them from local variables. You might also see Java code with member variables prefaced with only an underscore. Whichever method you choose is up to you.</TABLE>
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="787-790.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="794-798.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -