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

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="683-686.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="690-692.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading43"></A><FONT COLOR="#000077">Initializing Once More</FONT></H4>
<P>One method that was called from doCheck() was designed to create the user&#146;s object and initialize it properly. This also creates a new StarField and/or Terrain if one has not been instantiated already (although this could have been accomplished elsewhere).
</P>
<P>This method is pretty straightforward, and here&#146;s what it looks like:</P>
<!-- CODE //-->
<PRE>
void initUser() &#123;

       if( SF == null) &#123;
              SF = new StarField(100, SFrect, createImage(SFrect.width,&#8658;
              SFrect.height), everything, "sf");
              SF.setBounds( SFrect);
              SF.setSpeed(2,0);
              SF.start();
              &#125;
       if( T == null || !T.isAlive()) &#123;
              T = new Terrain(50,Trect,getGraphics());
              T.start();
              T.suspend();
              &#125;
       if( user != null &#38;&#38; user.isAlive()) user.stop();
       user = new userSprite(everything, "user");
       user.setID( Sprite.USER);
       user.setXY( 50, (int)(SFrect.height/2));
       user.im = createImage( user.WIDTH, user.HEIGHT);
       user.setSpeed(0);
       user.start();
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading44"></A><FONT COLOR="#000077">Creating a Firing Mechanism</FONT></H4>
<P>Whenever the user wants to &#147;shoot&#148; an answer, we need to create a bullet and send it hurtling toward its destination. To add a little intrigue to the scoring mechanism, we also dock the user one point for each bullet he or she fires. Here&#146;s the code for a method that creates a new bullet at a given X and Y coordinate:
</P>
<!-- CODE //-->
<PRE>
public Sprite newBullet(int x, int y) &#123;
Sprite b;
Rectangle temp = new Rectangle( x&#43;user.bounds.width&#43;5,
y&#43;(int)(user.bounds.width/2), 15,3);

       b = new Sprite(bullets,"bang");
       b.setBounds( temp);
       b.im = createImage(temp.width, temp.height);
       b.setSpeed(25,0);
       b.setID( Sprite.BULLET);
bar.addScore(-1);
return b;
&#125;
</PRE>
<!-- END CODE //-->
<P>A related method that is useful only for debugging purposes is one that allows you to fire bullets at any location with the mouse. This makes it easy to test different questions without actually having to play the game. Just remember to remove it in the final product!
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseDown(Event evt, int x, int y) &#123;
Sprite b= newBullet(user.x,user.y);
b.start();
return true;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading45"></A><FONT COLOR="#000077">Giving the User Control</FONT></H4>
<P>We added one control for debugging purposes above, but let&#146;s also add the code that allows the player to move his or her ship around. First, we need to handle movement. This is accomplished with the numeric keypad. We use the numbers 1&#150;9 to represent eight different directions that the user could travel in (N, W, S, E, NE, NW, SE, SW):
</P>
<!-- CODE //-->
<PRE>
public boolean keyDown(Event evt, int key) &#123;

switch( key) &#123;

case '8': &#123;
       user.move( user.x,user.y-5);
       return true;
       &#125;

case '2': &#123;
       user.move( user.x,user.y&#43;5);
       return true;
       &#125;

case '4': &#123;
       user.move( user.x-5,user.y);
       return true;
       &#125;
case '6': &#123;
       user.move( user.x&#43;5,user.y);
       return true;
       &#125;
case '1': &#123;
       user.move( user.x-5,user.y&#43;5);
       return true;
       &#125;
case '3': &#123;
       user.move( user.x&#43;5,user.y&#43;5);
       return true;
       &#125;
case '7': &#123;
       user.move( user.x-5,user.y-5);
       return true;
       &#125;
case '9': &#123;
       user.move( user.x&#43;5,user.y-5);
       return true;
       &#125;

&#125;
return false;
&#125;
</PRE>
<!-- END CODE //-->
<P>Next, we add handling to allow the user to fire a bullet:
</P>
<!-- CODE SNIP //-->
<PRE>
case ' ': &#123;
       Sprite b=newBullet(user.x,user.y);
       b.start();
       return true;
</PRE>
<!-- END CODE SNIP //-->
<P>We also add two keys that are useful for debugging but should be disabled in the final version. &lt;P&gt; and &lt;R&gt; are used to pause and resume the game. This can be easily accomplished by appropriate calls to the &#147;everything&#148; ThreadGroup:
</P>
<!-- CODE SNIP //-->
<PRE>
case 'p' : &#123;
       everything.suspend();
       return true;
       &#125;
case 'r' : &#123;
       everything.resume();
       return true;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Last, we add one of WordQuest&#146;s neatest special effects: the warp factor. When the user holds down the &lt;W&gt; key, his or her ship kicks into warp mode, and everything flies past much faster. The StarField also makes a cool streaking effect. To do this, we must first have a keyDown event that turns on the warp effect:
</P>
<!-- CODE SNIP //-->
<PRE>
case 'w': &#123;
       Sprite.warp= 5;
       return true;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>This, however, requires a corresponding keyUp event that turns off the effect:
</P>
<!-- CODE //-->
<PRE>
public boolean keyUp(Event evt, int key) &#123;

switch(key) &#123;

case 'w': &#123;
       SPEED=1;
       Sprite.warp=1;
       return true;
       &#125;
&#125;
return false;
&#125;
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading46"></A><FONT COLOR="#000077">A Few Enhancements</FONT></H3>
<P>Although WordQuest is practically finished, there is still one small matter that we have not resolved. We discussed earlier the idea of Thread <I>synchronization</I>. This is very important in Java, and is something worth discussing further. If you have played WordQuest at all, you will notice that although the enemy Sprites, being in the same group, run pretty much at the same rate, they are not totally synchronized. Some Sprites run faster, some slower, depending on how resources get allocated in each cycle. This effect is not visually appealing, so we need to come up with a way of having absolute synchronization among Sprites.</P>
<P>The key to synchronization is having each Sprite keep in touch with all of the other Sprites it needs to synchronize with. While there are many ways to accomplish this, we will explore one simple way. Once you understand the basic concept, it is trivial to extend it to more complicated examples.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="683-686.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="690-692.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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