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

📄 602-605.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:Daleks!</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=14//-->
<!--PAGES=602-605//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="599-602.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="605-608.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Making the Right Moves</FONT></H4>
<P>We can only hope for the player&#146;s sake that he or she has made a wise move. Fortunately, the applet&#146;s only job is to analyze the player&#146;s input and update the game board according to the rules of the game. The final <I>else</I> clause (with the comment &#147;analyze keypress and execute the next turn&#148;) is where the entire turn unfolds, shown in Listing 14-8.</P>
<P><B>Listing 14-8</B> Executing player&#146;s move in handleKeyPress(int whichKey); executing next turn in handleKeyPress(int whichKey)</P>
<!-- CODE //-->
<PRE>
public void handleKeyPress(int whichKey)  &#123;
       if (animIntro)  &#123;
               // stop the intro screen and begin the game
               ...
               &#125;
       else if (levelComplete)  &#123;
               // set up for the next level
               ...
               &#125;
       else if (playerDead)  &#123;
               // animate the intro screen
               ...
               &#125;
       else &#123;
              // analyze keypress and execute the next turn
              char ASCIImove = (char)whichKey;
              boolean validMove = movePlayer(ASCIImove);

              if (validMove)  &#123;
                            if (lastStand)
                            repaint();
                     else  &#123;
                                   if (screwdriverUsed)  &#123;
                                   calcSonic();
                                   play(getCodeBase(), "sonic.au");
                                   &#125;
                            moveDaleks()
                     if (newRubble)
                                   play(getCodeBase(), "crash.au");
                     &#125;
            &#125;
            repaint();
           checkDaleks();
               if ((drA) &#38;&#38; (allDead))
                    levelComplete = true;
               else if (!drA)  &#123;
                    prepareScreen();
                    repaint();
                    playerDead = true;
                    &#125;
               &#125;
         &#125;
</PRE>
<!-- END CODE //-->
<P>First, the player&#146;s move is converted from an integer to an ASCII character for simplification. Then the movePlayer(char) method is called. This method returns a boolean value&#151;<I>true</I> if the player&#146;s move was valid and <I>false</I> otherwise. If the player&#146;s move checks out, several things can happen. If the player has chosen to make a last stand (by pressing &#147;L&#148;), the repaint() procedure is called. This will be examined in a later section.</P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Analyzing Input</FONT></H4>
<P>The movePlayer(char) method shown in Listing 14-9 takes care of the gritty details of keyboard processing. The end result of this method is a boolean value indicating whether or not the player&#146;s move is a valid one.
</P>
<P><B>Listing 14-9</B> The daleks14.movePlayer(&#133;) method</P>
<!-- CODE //-->
<PRE>
public boolean movePlayer(char input)  &#123;
       int newX = drX;
       int newY = drY;
       int newF = drF;
       boolean pass = false;
       // player may elect to not move (hit '5')
       boolean teleport = false;
       // player may teleport (hit 'T' or 't')
       boolean validMove = true;
       switch (input)  &#123;
              // numeric keypad movement options
              case '1': newX--;    newY&#43;&#43;;    newF = faceLeft;    break;
              case '2':            newY&#43;&#43;;                        break;
              case '3': newX&#43;&#43;;    newY&#43;&#43;;    newF = faceRight;   break;
              case '4': newX--;               newF = faceLeft;    break;
              case '5': pass = true;                              break;
              case '6': newX&#43;&#43;;               newF = faceRight;   break;
              case '7': newX--;     newY--;   newF = faceLeft;    break;
              case '8':             newY--;                       break;
              case '9': newX&#43;&#43;;     newY--;   newF = faceRight;   break;

              // alternative keys for movement
             case 'N': newX--;     newY&#43;&#43;;   newF = faceLeft;    break;
             case 'n': newX--;     newY&#43;&#43;;   newF = faceLeft;    break;
             case 'M':             newY&#43;&#43;;                       break;
             case 'm':             newY&#43;&#43;;                       break;
             case ',': newX&#43;&#43;;     newY&#43;&#43;;   newF = faceRight;   break;
             case 'H': newX--;               newF = faceLeft;    break;
             case 'h': newX--;               newF = faceLeft;    break;
             case 'J': pass = true;                              break;
             case 'j': pass = true;                              break;
             case 'K': newX&#43;&#43;;               newF = faceRight;   break;
             case 'k': newX&#43;&#43;;               newF = faceRight;   break;
             case 'Y': newX--;     newY--;   newF = faceLeft;    break;
             case 'y': newX--;     newY--;   newF = faceLeft;    break;
             case 'U':             newY--;                       break;
             case 'u':             newY--;                       break;
             case 'I': newX&#43;&#43;;     newY--;   newF = faceRight;   break;
             case 'i': newX&#43;&#43;;     newY--;   newF = faceRight;   break;

              // other valid commands
case 'T': teleport = true;                          break;
case 't': teleport = true;                          break;
case 'S': screwdriverUsed = true;                   break;
case 's': screwdriverUsed = true;                   break;
case 'L': lastStand = true;                         break;
case 'l': lastStand = true;                         break;
default:  validMove = false;
&#125;

// check if the move is out-of-bounds
       if ((newX&lt;0) || (newX&gt;maxX-1) || (newY&lt;0) || (newY&gt;maxY-1))
              validMove = false;

// find an empty location if teleporting
          if (teleport)  &#123;
boolean okLoc = false;
// loop until valid coordinates are found
while (!okLoc)  &#123;
okLoc = true;
                      newX = (int)(Math.random()*maxX);
                      newY = (int)(Math.random()*maxY);
                      for (int j=0; j&lt;numDaleks; j&#43;&#43;)
                      if ( ((newX==dalX[j])&#38;&#38;(newY==dalY[j])) &#38;&#38;
     ((dalA[j])||(dalR[j])) )
                      okLoc = false;
                      &#125;
              animTele = true;
              &#125;

       // check if the player is allowed to use the screwdriver
       if ((screwdriverUsed) &#38;&#38; (screwdrivers==0))  &#123;
              validMove = false;
              screwdriverUsed = false;
              &#125;

       // check if attempting to move to a location containing rubble
       for (int j=0; j&lt;numDaleks; j&#43;&#43;)
              if ((newX==dalX[j]) &#38;&#38; (newY==dalY[j]) &#38;&#38; (dalR[j]))
                     validMove = false;

       // if everything is ok, record new position &#38; orientation
       if (validMove)  &#123;
              drX = newX;
              drY = newY;
              drF = newF;
              &#125;

       return validMove;
       &#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="599-602.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="605-608.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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