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

📄 813-814.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=813-814//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="809-812.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="814-821.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading36"></A><FONT COLOR="#000077">Modifying SessionManager</FONT></H4>
<P>What code needs to be modified and added in order to create one or more worms that guide themselves? Based on the current Worm design, the primary code that needs to change is the SessionManager object. The Worm object needs fewer revisions, because it does not directly participate in the control of the playing surface.
</P>
<P>When a player controls the worm through the keyboard, the SetNewDirectionFromKeyboard() function is called. An equivalent function, SetDirection(), will be added, which directly sets the worm direction. A complementary function, GetDirection(), will be added for use during object detection. These functions are given in Listing 20-12.</P>
<P><B>Listing 20-12</B> Setting the worm direction from System Manager using helper functions</P>
<!-- CODE //-->
<PRE>
public void SetDirection(int x, int y)
&#123;
    dDir.width = x;
    dDir.height = y;
&#125;

public Dimension GetDirection()
&#123;
    return dDir;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading37"></A><FONT COLOR="#000077">The DoAutoWorms() Function</FONT></H4>
<P>In much the same way as the player&#146;s worm is advanced one segment for every call of the Next() function, each autonomous worm advances one segment each time DoAutoWorms() is called. DoAutoWorms() is called at the end of the Next() function by the SessionManager. Essentially all autonomous control is handled by DoAutoWorms(), which is given in Listing 20-13. Since DoAutoWorms() is called once for each call of Next(), two implementations suggest themselves: either increment the position of every autonomous worm for each call, or increment the position of one worm per call. DoAutoWorms() takes the latter approach, which results in progressively slower autonomous worm movement for every autonomous worm added.
</P>
<P><B>Listing 20-13</B> Controlling the autonomous worms with the SessionManager&#146;s DoAutoWorms() function</P>
<!-- CODE //-->
<PRE>
    private void DoAutoWorms()
    &#123;
        int direction, dx, dy;

        currautoworm&#43;&#43;;
        if (currautoworm &gt;= WORMCOUNT)
            currautoworm = 1;

        // save current direction of motion
        dpoint[0] = worm[currautoworm].GetDirection();

        autopoint = worm[currautoworm].GetNextPoint();

        if (dirchange &gt; 0)
            dirchange--;

        // do different things depending on what we hit
        // if hit a wall, go randomly right or left
        if (HitWall(autopoint) == true || dirchange == 1) &#123;

            dirchange = (int)((Math.random() &#43; 1) * 5 &#43; 10);

            // pick a direction at random
            if (Math.random() &lt; 0)
                direction = -1;
            else
                direction = 1;

            dpoint[1] = GetNewDirection(dpoint[0], direction);
        &#125;
        else if (HaveSeg(autopoint) == true ||
                  HitTreat(autopoint) == true) &#123;

            // always try to go left if hit an object
            dpoint[1] = GetNewDirection(dpoint[0], -1);
        &#125;
        else &#123;
            // no collision, all done
            worm[currautoworm].SetNextPoint(autopoint);
            // extend worm if it shrunk
            if (worm[currautoworm].NotRegrowing())
                worm[currautoworm].DropLastPoint();
            return;
        &#125;
        // create remaining directions
        if (dpoint[0].width == 1) &#123;
            dpoint[2].width = -1;
            dpoint[2].height = 0;
        &#125;
        else if (dpoint[0].width == -1) &#123;
            dpoint[2].width = 1;
            dpoint[2].height = 0;
        &#125;
        else if (dpoint[0].height == 1) &#123;
            dpoint[2].width = 0;
            dpoint[2].height = -1;
        &#125;
        else &#123;
            dpoint[2].width = 0;
            dpoint[2].height = 1;
        &#125;

        if (dpoint[1].width == 1) &#123;
            dpoint[3].width = -1;
            dpoint[3].height = 0;
        &#125;
        else if (dpoint[1].width == -1) &#123;
            dpoint[3].width = 1;
            dpoint[3].height = 0;
        &#125;
        else if (dpoint[1].height == 1) &#123;
            dpoint[3].width = 0;
            dpoint[3].height = -1;
        &#125;
        else &#123;
            dpoint[3].width = 0;
            dpoint[3].height = 1;
        &#125;

        // skip this first since it is a known collision
        for (int i = 1; i &lt; 4; i&#43;&#43;) &#123;

            worm[currautoworm].SetDirection(dpoint[i].width,
                                          dpoint[i].height);
            autopoint = worm[currautoworm].GetNextPoint();

            if (HitWall(autopoint) == false &#38;&#38;
                HaveSeg(autopoint) == false &#38;&#38;
                HitTreat(autopoint) == false) &#123;

                // no collision, all done
                worm[currautoworm].SetNextPoint(autopoint);
                // extend worm if it shrunk
                if (worm[currautoworm].NotRegrowing())
                    worm[currautoworm].DropLastPoint();
                return;
            &#125;
        &#125;
        // no places left to go!

        if (worm[currautoworm].NotRegrowing())
            worm[currautoworm].DropLastPoint();
    &#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading38"></A><FONT COLOR="#000077">Calculating Increments</FONT></H4>
<P>The variable <I>currautoworm</I> increments over the number of autonomous worms for each call of DoAutoWorms() and provides an index into SessionManager&#146;s array of worms. The array <I>dpoint[]</I> provides four direction variables that correspond to the four available directions of motion. This array is dynamically filled with directions in decreasing order of desirability.</P>
<P>The function GetNextPoint() is called to retrieve the calculated x and y locations of the next head position into the variable <I>autopoint</I>, and GetDirection() returns the current head travel direction into <I>dpoint[0]</I>. The variable <I>dirchange</I> is set at a random value around 10 and decremented for each call to DoAutoWorms(). Once it reaches 0, it is reset to a new random value. When it reaches a value of 1, the current autonomous worm&#146;s direction is changed irrespective of whether it touched an object. This feature adds some dynamics to the game, partly randomizing the motion of the autonomous worms.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="809-812.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="814-821.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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