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

📄 656-659.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=656-659//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="651-656.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="659-664.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Implementing Sprite.java Variables</FONT></H4>
<P>The first thing we must do with this class is define a whole slew of variables. Here&#146;s what each of them is for:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;int WIDTH, HEIGHT. These variables are the default Sprite width and height values. Most sprites will override these initial values.
<DD><B>&#149;</B>&nbsp;&nbsp;double <I>speedX</I>, <I>speedY</I>. These variables are used to determine Sprite&#146;s velocity in the X and Y directions.
<DD><B>&#149;</B>&nbsp;&nbsp;int <I>x</I>, <I>y.</I> These variables determine the Sprite&#146;s current position.
<DD><B>&#149;</B>&nbsp;&nbsp;int <I>lastX</I>, <I>lastY</I>. These variables are the previous x and y coordinates of the Sprite; they are used to erase the Sprite when not using double-buffered graphics.
<DD><B>&#149;</B>&nbsp;&nbsp;Rectangle <I>bounds</I>. A Rectangle (usually based on <I>x,y</I> and WIDTH,HEIGHT) that contains the entire Sprite.
<DD><B>&#149;</B>&nbsp;&nbsp;int DELAY. This variable is the time, in milliseconds, that the Sprite&#146;s Thread should pause before advancing the Sprite. This is used to ensure that no one Sprite takes up all available resources.
<DD><B>&#149;</B>&nbsp;&nbsp;static Graphics <I>theG.</I> This variable is used to allow all Sprites to use the same Graphics context to draw in. Thus, once the parent applet sets this property, every Sprite has someplace to draw.
<DD><B>&#149;</B>&nbsp;&nbsp;static int <I>warp.</I> This variable is a &#147;warp factor.&#148; Every Sprite multiplies its speed by this number, which is usually one. However, if we want to quickly increase the speed of every Sprite (including the background), we can increase this number.
<DD><B>&#149;</B>&nbsp;&nbsp;public Image <I>anim[].</I> This variable is an array of Images that can be set if we want a Sprite to use a series of Images to animate itself. Otherwise, we must provide a default drawing behavior for the Sprite.
<DD><B>&#149;</B>&nbsp;&nbsp;int <I>animIndex</I>, <I>animMax.</I> These variables contain the &#147;current&#148; <I>anim[]</I> frame and the size of the <I>anim[]</I> array, respectively.
<DD><B>&#149;</B>&nbsp;&nbsp;public Image <I>im.</I> This variable is an Image used for offscreen drawing, when necessary (especially when we start doing double-buffered graphics). Each Sprite must have this variable set by the applet (only Abstract Windowing Toolkit (AWT) components can create offscreen Image space).
<DD><B>&#149;</B>&nbsp;&nbsp;Color <I>theColor.</I> This is used for the default drawing color of this Sprite (only used by some Sprites).
<DD><B>&#149;</B>&nbsp;&nbsp;public int <I>id.</I> This variable contains the Sprite&#146;s ID, which is one of the constants declared in the Sprite class (we&#146;ll declare the actual values later).
</DL>
<P>Here&#146;s what the initial class declaration looks like:
</P>
<!-- CODE //-->
<PRE>
import java.awt.*;

public class Sprite extends Thread &#123;

int WIDTH=40,HEIGHT=25;
double speedX=0,speedY=0;
int x,y, animIndex,animMax;
int lastX,lastY;
int DELAY=300;
public Image anim[], im;

static Graphics theG;
static int warp=1;

Rectangle bounds;
Color theColor;
int id=0;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Defining Sprite IDs</FONT></H4>
<P>Next, we should decide on a system of Sprite IDs. For starters, we should define five different types of Sprites (although adding more later is simple): ENEMY, BULLET, USER, EXPLOSION, and MESSAGE. It would be simple and feasible to assign each of these Sprites a number between 1 and 5, and then use a series of <I>if</I> statements to check what sort of collision has taken place. However, if we use a little mathematics, we can come up with a far more versatile system. Suppose we used the simple 1&#150;5 numbering scheme mentioned before. To check a collision, we could do something like this:</P>
<!-- CODE SNIP //-->
<PRE>
public void collision(int num) &#123; // num == ID of the colliding object

if( num==BULLET &#38;&#38; this.id==ENEMY)
       stop();
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>A whole slew of <I>if</I> statements, or a large <I>case</I> statement, could probably handle all possible permutations for a two-Sprite collision. However, consider what would happen if we wanted to have three Sprites collide? four Sprites? 100 Sprites? Clearly the colossal nature of such a <I>case</I> statement makes this method impractical. How, then, should we proceed? One method that is very effective is to assign a prime number to each ID. Whenever a collision takes place, we pass the product of all of the IDs in the collision to each Sprite, which then can act accordingly. Because every collision number is the product of a unique set of prime numbers, we can use the modulus operator to determine what type of collision has taken place. Don&#146;t let all this math stuff scare you. If you aren&#146;t quite sure how all this works, try an example. Let&#146;s use the following set of ID definitions in Sprite:</P>
<!-- CODE SNIP //-->
<PRE>
static final int ENEMY = 2;
static final int BULLET = 3;
static final int USER = 5;
static final int EXPLOSION = 7;
static final int MESSAGE = 11;
</PRE>
<!-- END CODE SNIP //-->
<P>Now any collision involving a bullet will yield 0 if it is &#147;%&#148; (modulus division) by Sprite.BULLET. Keep in mind that the default ID of the Sprite class is 0, so that Sprites that do not get collided with (like the background) produce a &#147;zero&#148; collision that we can safely ignore. Here is some simple code that handles the most basic of collisions:
</P>
<!-- CODE //-->
<PRE>
public void collision( int num) &#123;

if( num == 0) return;
if( id == MESSAGE) return;

if( id == BULLET || num%BULLET == 0)
       stop();
else
if( num == USER*ENEMY)
       stop();
&#125;
</PRE>
<!-- END CODE //-->
<P>For more complicated games, of course, this method could become quite complex. One interesting idea would be to have a Sprite that is &#147;killed&#148; replace itself with an EXPLOSION Sprite. For more on this, see the Suggestion Box at the end of this chapter.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Initializing the Variables</FONT></H4>
<P>To make this class easier to use, let&#146;s define a couple of methods that allow easy initialization of key variables. These are pretty straightforward and aren&#146;t especially exciting, so let&#146;s get them out of the way quickly:
</P>
<!-- CODE //-->
<PRE>
public void setXY(int x, int y) &#123;
       this.x=x;
       this.y=y;

if( bounds == null)
       bounds = new Rectangle( x,y,WIDTH,HEIGHT);
else &#123;
       bounds.x=x;
       bounds.y=y;
       &#125;
&#125;

public void setBounds( Rectangle r) &#123;
       bounds = r;
       x = bounds.x;
       y = bounds.y;
&#125;

public void setID(int id) &#123;
       this.id = id;
&#125;

public int queryID() &#123;
       return id;
&#125;

public void setSpeed( double X, double Y) &#123;
       speedX=X;
       speedY=Y;
&#125;
public void setSpeed( double factor) &#123;
       speedX=factor;
       speedY=factor;
&#125;

public int getSpeed() &#123;
       if( speedY!=0)
             return warp*(int)(speedX/speedY);
       else
              return warp*(int)(speedX&#43;.5);
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="651-656.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="659-664.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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