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

📄 801-805.html

📁 java game programming e-book
💻 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,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</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=801-805//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="798-801.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="806-809.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Controlling Input from the Keyboard</FONT></H4>
<P>Control from the keyboard is fairly straightforward: Choose a direction that corresponds to which arrow key was pressed. The code in Listing 20-6 includes a check for letter keys grouped together for keyboards that do not have arrow keys. This choice of keys is purely based on what seems to be the most ergonomic. A check is made to see if the key pressed was a directional key; if such a key was pressed, then the direction is altered only if the new direction is perpendicular to the current direction. The direction value must be one of -1, 0, or 1. Any other value will result in an incorrect depiction of the worm.
</P>
<P>One thing to watch out for: Because the keyboard events can arrive one right after the other, it is possible to rotate the head completely around between a change in the head&#146;s position by the SessionManager. The result of this grouping is to cause the head to collide with the body but without turning. The player will see this as a collision indication directly behind the head segment.</P>
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Controlling Input with the Mouse</FONT></H4>
<P>The direction is set differently if the player uses a mouse. The player can click anywhere on the playing surface in a location that does not correspond directly to the current direction of travel. A change in direction is made based on whether the player clicks to the right or left of an imaginary line parallel and through the current direction of motion of the worm&#146;s head. This can be determined by getting the position of the head segment and the current direction of travel and calculating the true position of the worm&#146;s head in the playing surface. This is the only code in the class Worm that does not strictly adhere to the separation of the display surface from the abstract playing surface. Eventually both functions could be moved to the SessionManager if this separation were desired.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading29"></A><FONT COLOR="#000077">Starting the SessionManager</FONT></H4>
<P>The SessionManager class&#146;s constructor has easier duties than the Worm class&#146;s constructor. It must create the player&#146;s worm and any additional worms. The constructor in Listing 20-7 shows an array of Worm objects created to support the player&#146;s worm and two additional autonomous worms. The player&#146;s worm must be assigned to the first element in the array for this implementation of Worm. The array of four Dimension objects is used for guiding the autonomous worms.
</P>
<P><B>Listing 20-7</B> SessionManager initialization and the game Restart() function</P>
<!-- CODE //-->
<PRE>
public class SessionManager &#123;
    static int SIZEX = 30;
    static int SIZEY = 30;
    static int WORMCOUNT = 3;

    boolean m_newtr = false;
    boolean m_showscore = false, m_dontdrop = false;
    boolean m_speedup = false;

    Dimension wMax = new Dimension(SIZEX,SIZEY);
    Dimension dTemp = new Dimension();
    Dimension treatpoint = new Dimension();
    Dimension collisionpoint = new Dimension();
    Dimension newpoint = new Dimension();
    Dimension autopoint = new Dimension();
    Dimension dpoint[] = new Dimension[4];

    Worm worm[];
    int currscore;
    int highscore;
    int lastscore;
    int treatcount;
    int forcedelay;
    int currautoworm;
    int adddropidx;
    int grow;
    long age;
    int m_getallidx;
    int dirchange;

    public SessionManager()
    &#123;
        currscore = lastscore = highscore = 0;

        for (int i = 0; i &lt; 4; i&#43;&#43;)
            dpoint[i] = new Dimension();

        worm = new Worm[WORMCOUNT];

        worm[0] = new Worm(2, 5, 10, Color.blue,
                                    SIZEX * SIZEY / 2, 1, 0);
        worm[1] = new Worm(2, 15, 20, Color.yellow,
                                    22, 0, -1);
        worm[2] = new Worm(2, 1, 20, Color.cyan,
                                    22, 0, 1);
        Restart();
    &#125;

    public void Restart()
    &#123;
        collisionpoint.width = -1;
        collisionpoint.height = -1;

        lastscore = currscore;
        currscore = 0;
        treatcount = 0;
        currautoworm = 1;
        grow = 0;
        age = 0;
        forcedelay = 0;
        dirchange = 0;

        for (int i = 0; i &lt; WORMCOUNT; i&#43;&#43;)
            worm[i].ReviveWorm();

        NewTreat(true);
    &#125;
</PRE>
<!-- END CODE //-->
<P>The playing surface is 30x30 units across. The maximum player worm size is these two numbers multiplied together and divided by two. This effectively means the player&#146;s worm can never occupy more than half the physical space of the playing surface (which already is quite a lot, due to the limited maneuverability available). Refer to Table 20-1 for information on the parameters passed to the worm constructor.
</P>
<P>The Restart() function as shown in Listing 20-7 is called to initialize per-game variables. This function is declared public and is called by the class WormBin to restart a game and initialize variables. Restart() iterates through each worm and calls the worm&#146;s ReviveWorm() member function to reinitialize each worm&#146;s variables. Restart() also clears the collision variables, resets the current score to zero, and calls NewTreat() to randomly place a new treat on the playing surface.</P>
<H4 ALIGN="LEFT"><A NAME="Heading30"></A><FONT COLOR="#000077">Using the Next() Function</FONT></H4>
<P>The SessionManager is entirely event-driven. Nothing happens unless the class WormBin calls it to perform some action or request some data. During game play, WormBin calls SessionManager once every 100 milliseconds to increment the player&#146;s worm position. No calculations take place between calls to the SessionManager, because WormBin calls the Java API Thread.sleep() in order to give the illusion of smooth, constant motion. This means the SessionManager must perform calculations quickly and return so the thread can go back to sleep. If the SessionManager were to spend long and different time periods calculating results, the appearance of smooth motion would be lost, because a greater percentage of time would be spent calculating than sleeping. Refer to Figure 20-9 for an illustration of the following sections on the Next() function as used by the SessionManager and the WormBin class.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/20-09.jpg',468,582 )"><IMG SRC="images/20-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/20-09.jpg',468,582)"><FONT COLOR="#000077"><B>Figure 20-9</B></FONT></A>&nbsp;&nbsp;The Next() function as it interacts across classes to make the worm appear to move<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="798-801.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="806-809.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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