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

📄 690-692.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:WordQuest</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=16//-->
<!--PAGES=690-692//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="686-689.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch17/693-698.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading47"></A><FONT COLOR="#000077">The syncSprite Class</FONT></H4>
<P>To achieve Thread synchronization among enemy Sprites, we are going to create a new subclass of Sprite, called syncSprite. Each syncSprite will not advance itself until all of the other syncSprites in its ThreadGroup have advanced. To do this, we use a counter variable. When each syncSprite finishes advancing, it increments this counter in every syncSprite in the Group. When the value of this counter reaches the number of syncSprites in the Group, it is reset and the process repeats. The syncSprite will not advance until this condition is met. The net result of all this inter-Thread communication is that no syncSprite can get more than one frame ahead or behind its siblings. The main drawback is that all syncSprites move as slowly as the one with the least amount of resources allocated to it. However, if all the syncSprites have the same priority level, this should not be much of a problem.
</P>
<P>The code for this class is not very complicated once you understand the concepts involved. We use a simple integer as a counter, and we use a boolean flag to ensure that each Sprite gets run for the first frame (without this, if one Sprite advanced ahead of the others, their counters would be set to 1, and they would never advance). We also take advantage of the yield() method to give up the Sprite&#146;s time slice to another waiting Thread (most likely another syncSprite in the same Group).</P>
<!-- CODE //-->
<PRE>
import Sprite;
import java.lang.*;

public class syncSprite extends Sprite &#123;
int count=0;
boolean flag=true;

syncSprite(ThreadGroup tg, String d) &#123;
       super(tg,d);
       count=0;
       flag=true;
       DELAY=50;
&#125;

public void run() &#123;
       while(true) &#123;
              try&#123; sleep(DELAY); &#125; catch(Exception e);
//            paintSprite( theG);
       if( flag || count==0 || count &gt;= getThreadGroup().activeCount()) &#123;
              =0;
              advance();
       flag = false;
       int i;
       syncSprite s[]= new syncSprite[i=getThreadGroup().activeCount()];
       enumerate(s);

       while(i-- &gt; 0)
              s[i].count&#43;&#43;;
       &#125; else
              yield();
              &#125;
&#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>To take advantage of this new class, we need to make only one change to WordQuest. In the nextQuestion() method, change the following line:
</P>
<!-- CODE SNIP //-->
<PRE>
s = new Sprite( group, dataArray[x]);
</PRE>
<!-- END CODE SNIP //-->
<P>to
</P>
<!-- CODE SNIP //-->
<PRE>
s = new syncSprite( group, dataArray[x]);
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading48"></A><FONT COLOR="#000077">That&#146;s All Folks!</FONT></H4>
<P>Congratulations! WordQuest is totally functional now. Go ahead and compile it. Of course, you will need some questions to be in a data file called data.txt, in the same directory as your compiled class files. If you want, you can borrow some that I created that are on the CD-ROM that accompanies this book. They are mainly taken from preparatory material for the Scholastic Aptitude Test (SAT-1). Note that although WordQuest was designed for vocabulary training, it could easily be used for math or other subjects.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading49"></A><FONT COLOR="#000077">Beautiful Friend, the End</FONT></H4>
<P>WordQuest is now ready to go! Compile it, play it, show it off to your friends. Who knows, you might even learn some new words!
</P>
<H3><A NAME="Heading50"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<P>There are many, many things you could do to make WordQuest even more fun. This list includes a few:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Like any other game, WordQuest would benefit from some dazzling graphics or sound effects. Inserting these into the game is very easy, since we have already established methods and structures for handling arrays of Images designed to animate Sprites. Sounds would require a few lines of new code, but nothing tricky.
<DD><B>&#149;</B>&nbsp;&nbsp;Now that our enemy Sprites are synchronized, you could have them fly in formation. This would require having a method that was aware of different patterns that the Sprites could be staggered in, and that set them up in cool patterns. Because the Sprites are synchronized, they will maintain this pattern almost exactly.
<DD><B>&#149;</B>&nbsp;&nbsp;Implement different levels. Although we have a variable in the status bar for &#147;level,&#148; we never actually use it in WordQuest. You could have each level use a different set of questions, so that the game gets progressively more difficult. Or, perhaps the Sprites could move faster as levels increase. You make the call.
<DD><B>&#149;</B>&nbsp;&nbsp;Write the EXPLOSION Sprite! This is simply an array of Images that animate an explosion. Write a special explosionSprite class that displays itself and then stops; then write up a method in either Sprite or WordQuest itself that calls for an explosion to replace dead Sprites.
</DL>
<H3><A NAME="Heading51"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>In this chapter, we created a nifty educational game while getting some practical experience with some Java concepts, like Thread synchronization. In addition, we combined many of the techniques learned throughout the book into an integrated gaming adventure. WordQuest is a formidable game in its own right, and it lays the foundation for many enjoyable action games to come. Education and action are both game genres with a considerable audience, and it is important for any game programmer to understand the underlying concepts that make both possible.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="686-689.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch17/693-698.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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