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

📄 599-602.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=599-602//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="596-599.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="602-605.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">The Animation Loop</FONT></H4>
<P>Now that two Thread objects are active, one can be concentrated on the animation loop while the other waits for keyboard input (a keypress indicates it is time for the game to begin). Nothing fancy is required here. The update() procedure will call the animatedIntro object whenever <I>animIntro</I> (a boolean variable) is set to <I>true</I>. Therefore, the animation loop will start whenever repaint() is called and <I>animIntro</I> is <I>true</I>, as demonstrated in Listing 14-5.</P>
<P><B>Listing 14-5</B> The daleks14.update(&#133;) method</P>
<!-- CODE //-->
<PRE>
public synchronized void update(Graphics g)  &#123;
        //   draw between-move animations and repaint the game board, or
        //   call the intro screen&#146;s drawing routine
        repaintDone = false;
        if (animIntro)  &#123;
            animateIntro.drawIntroScreen(g, doneLoading, currentPic);
            if (!doneLoading)  &#123;
                if (tracker.checkID(currentPic, true))
                    currentPic &#43;&#43;;
                if (currentPic &gt; 24)  &#123;
                    doneLoading = true;
                    prepareScreen();
                    &#125;
                &#125;
            repaint();
            &#125;
        else  &#123;
            ...
            &#125;
</PRE>
<!-- END CODE //-->
<P>This is the portion of the update() procedure that comprises the animation loop. Notice that the loop will only continue so long as <I>animIntro</I> remains <I>true</I>. When a key is pressed and image loading is complete, the keyDown() method sets <I>animIntro</I> to <I>false</I> and stops the second Thread object, effectively ending the animation loop.</P>
<P>Notice that the actual drawing routine is handled by the animateIntro object. Its drawIntroScreen() method is called with arguments for the current graphics context (telling it where to draw!), Image loading status, and the number of the picture currently being loaded.</P>
<P>The last important point of the animation loop is the use of the MediaTracker object <I>tracker</I>. As a picture is loaded, the value for <I>currentPic</I> is incremented. The value of <I>currentPic</I> is passed to the animateIntro() method and is used to determine the length of the progress bar on the bottom of the screen, as shown in Figure 14-3. When the last Image file is loaded, <I>doneLoading</I> becomes <I>true</I>, and the progress bar is replaced by the normal status bar.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/14-03.jpg',494,359 )"><IMG SRC="images/14-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-03.jpg',494,359)"><FONT COLOR="#000077"><B>Figure 14-3</B></FONT></A>&nbsp;&nbsp;The progress bar while an image is loading</P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">The Game Begins</FONT></H3>
<P>Once the game begins, nothing happens in the Daleks! applet until the player presses a key. In a real-time game this would be unacceptable, but it is perfect for turn-based games. A large number of boolean variables are used in the daleks14 object to help direct traffic, but we will only examine the important ones to get a feel for the structure of the game itself.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Starting a Turn</FONT></H4>
<P>A new turn begins when the player presses a key, which automatically calls the keyDown() method, shown in Listing 14-6.
</P>
<P><B>Listing 14-6</B> The daleks14.keyDown(&#133;) method</P>
<!-- CODE SNIP //-->
<PRE>
public synchronized boolean keyDown(Event evt, int x)  &#123;
       if (doneLoading)
              handleKeyPress(x);
       return false;
       &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that nothing will happen unless <I>doneLoading</I> is <I>true</I>. This variable will not become <I>true</I> until the <I>tracker</I> object is done loading all the Image files necessary to play the game. Ideally, the audio files would also be tracked, but Java&#146;s beta API does not currently implement this function. As a result, the game will pause to load audio files the first time they are played.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Directing Traffic</FONT></H4>
<P>Now that a new turn has started, what should be done? The game can be in many states&#150;we could be waiting to start a new level (if all the Daleks have been destroyed), in the middle of a level, or waiting to reanimate the title screen (if the player has been captured). Each of these possibilities requires an additional boolean variable to help the handleKeyPress(int) method decide what to do. Listing 14-7 gives the skeleton of this procedure.
</P>
<P><B>Listing 14-7</B> Basic logic/skeleton of handleKeyPres(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
               ...
               &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>The <I>if</I> statement checks the current status of the game and calls the appropriate methods. Let&#146;s examine what happens if we are in the middle of a level and the player has pressed a key. In this case, the boolean variables <I>animIntro</I>, <I>levelComplete</I>, and <I>playerDead</I> are <I>false</I>, and the code in the final <I>else</I> clause will be executed.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="596-599.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="602-605.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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