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

📄 683-686.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=683-686//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="679-683.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="686-689.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading41"></A><FONT COLOR="#000077">Handling the Rules</FONT></H4>
<P>Arguably the most crucial method in WordQuest is the method that does all of the game-related checking. This implements the &#147;rules&#148; of WordQuest. This method decides the fate of every Sprite and imposes the rules of the game on everyone. First, it turns all active enemies and bullets into arrays of Sprite:
</P>
<!-- CODE SNIP //-->
<PRE>
public void doCheck() &#123;
int i,j;
Sprite sprites[]=new Sprite[group.activeCount()],bul[]=new Sprite&#8658;
[bullets.activeCount()];

group.enumerate( sprites);
bullets.enumerate( bul);
int x;
</PRE>
<!-- END CODE SNIP //-->
<P>Next, it checks to see whether the user&#146;s Sprite is alive. If it is not, the user has suffered an ignominious defeat, and his or her score must be adjusted, and the high scores must be displayed:
</P>
<!-- CODE //-->
<PRE>
if( !user.isAlive()) &#123;
       System.out.println("You lose!\n");
       bar.addScore(-10);
       bar.addLives(-1);
       bullets.stop();
       T.suspend();
       if( bar.lives==0) &#123;
              playing=false;
              PF.HS.addScore(name,bar.score,null,null);
              PF.show();
              return;
              &#125;
       initUser();
       nextQuestion();
       return;
&#125;
</PRE>
<!-- END CODE //-->
<P>Undoubtedly you would want to replace &#147;You lose&#148; with a friendlier message (maybe even a graphic or pop-up window); this is left as an exercise. Next, we do all of the Sprite checking. Whenever a collision occurs, we call the collision() method in the affected Sprites. In order to save processing time, we only check those Sprites that have an effect on each other (i.e., user vs. Enemy). However, if we wanted to, we could just check every Thread in the &#147;everything&#148; group against every other Thread, and call the appropriate collision methods. Also, notice that we suspend a Sprite while we are checking it, in order to ensure that it does not move while it is being checked (that is cheating!). Another nifty feature is that when a bullet hits an incorrect answer, it is deflected back at the user. This adds a bit more action to the game, although some players might find it a bit too difficult to handle. If you find this to be the case, the feature can easily be disabled.
</P>
<!-- CODE //-->
<PRE>
for( x=0;x&lt;group.activeCount();x&#43;&#43;) &#123;
sprites[x].suspend();
       if( !sprites[x].intersects( SFrect))
              sprites[x].stop();
       else
       if( user.intersects( sprites[x].bounds)) &#123;
              user.collision( user.id * sprites[x].id);
              sprites[x].collision( user.id * sprites[x].id);
              &#125;
       for(int y=0; y &lt; bullets.activeCount(); y&#43;&#43;) &#123;
       Sprite bullet = bul[y];
       bullet.suspend();
               if( ! bullet.intersects( SFrect) || bullet.getSpeed() == &#8658;
0)
                     bullet.stop();
               else
               if( bullet.intersects(user.bounds)) &#123;
                     user.stop();
                     bullet.stop();
               &#125; else
               if( sprites[x].intersects( bullet.bounds))
                     if( currentQ.correct.equals( sprites[x].data)) &#123;
                             sprites[x].stop();
                             bullet.stop();
                             bar.addScore(50);
                     &#125; else
                             bullet.setSpeed( - bullet.getSpeed(),0);
               bullet.resume();
               &#125;
               sprites[x].resume();
       &#125;

if( group.activeCount() &lt;= 2)
       nextQuestion();

&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading42"></A><FONT COLOR="#000077">Selecting a Question</FONT></H4>
<P>The next method we need to write selects a question from the list that the dataThread we wrote earlier gets from the data file. The method must also take the information contained in the Question object and create the appropriate enemy groups.
</P>
<P>We start by stopping any currently active enemy Sprites. Then, we check to see if there are any questions available from which to choose. If not, we spawn a new dataThread to read some in from the data file:</P>
<!-- CODE //-->
<PRE>
public void nextQuestion() &#123;

group.stop();

if( questions.isEmpty())
       try&#123;
              URL theURL = new URL(getCodeBase(), "data.txt");
              new dataThread( questions, theURL).start();
       &#125; catch (Exception e);
</PRE>
<!-- END CODE //-->
<P>Next, we wait until there is at least one Question in the Vector that the dataThread is supposed to fill:
</P>
<!-- CODE SNIP //-->
<PRE>
currentQ=null;
do &#123;
       try &#123;
       currentQ = (Question)questions.elementAt((int)(Math.random()&#8658;
*questions.size()));
       questions.removeElement(currentQ);
       &#125; catch(Exception e);
} while( currentQ == null );
</PRE>
<!-- END CODE SNIP //-->
<P>Once that is complete, we assign the new question to the status bar, and then attempt to find a list of five possible answers to the question. We want to be especially sure that the correct answer appears once and only once in every set of five:
</P>
<!-- CODE //-->
<PRE>
bar.setQuestion( currentQ.question);

String dataArray[],temp;

dataArray = new String[ 5 ];

boolean flag = false;
for(int x=0; x&lt;5; x&#43;&#43;) &#123;
              temp = currentQ.takeAnswer();
              if( temp == null)
              do &#123;
              flag = false;
                      temp = Question.randAnswer();

              for(int i =0;i&lt;x;i&#43;&#43;)
                      if( temp == null || temp.equals(dataArray[i]))
                             flag = true;
              &#125; while( flag);

              dataArray[x]=temp;
&#125;
</PRE>
<!-- END CODE //-->
<P>Once we have the text of the answers, we can create the enemy Sprites themselves. Once we have them started, we suspend them immediately. Once they are all created and in the appropriate ThreadGroup, we resume them all at one time. This helps ensure that they are closely synchronized.
</P>
<!-- CODE //-->
<PRE>
double spdX;

int h = (int)(SFrect.height/5);
spdX = 20*Math.random()&#43;5*bar.level;

Sprite s;
for(int x=0; x&lt;5; x&#43;&#43;) &#123;
s = new Sprite( group, dataArray[x]);
s.HEIGHT=h;
s.setXY( SFrect.width&#43;SFrect.x-25, SFrect.y&#43;(h*x));
       s.setSpeed( -spdX, 0);
       s.setID( Sprite.ENEMY);
s.im=createImage(s.WIDTH,s.HEIGHT);
s.start();
s.suspend();
       &#125;
group.resume();
T.resume();

&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="679-683.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="686-689.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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