📄 171-177.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:Building a Video Game</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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=5//-->
<!--PAGES=171-177//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="167-171.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="177-179.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Implementing MissileSprite</FONT></H4>
<P>Finally, the full definition of the MissileSprite is shown in Listing 5-8.
</P>
<P><B>Listing 5-8</B> MissileSprite class</P>
<!-- CODE //-->
<PRE>
class MissileSprite extends RectSprite {
protected int vy; // velocity in y coordinate
protected int start_y; // starting y coord
protected int stop_y; // stop at y coord
Intersect target[];
public MissileSprite(int w,int h,Color c,int vy,
int start_y,int stop_y,
Intersect target[]) {
super(w,h,c);
setFill(true); // fill rect sprite
this.vy = vy; // initialize speed
this.start_y = start_y; // initialize starting point
this.stop_y = stop_y; // initialize stopping point
this.target = target; // initialize targets
suspend();
}
// start the missile at the given x coordinate
public void init(int x) {
locx = x;
locy = start_y;
restore();
}
public void update() {
if (active) {
// move missile
locy += vy;
if (locy < stop_y) {
suspend();
}
// else if missile hits target, suspend it
else {
for (int i=0; i<target.length; i++) {
if (target[i].intersect(locx,locy,
locx+width,locy+height)) {
target[i].hit(); // tell target it's been hit
suspend();
break;
}
}
}
}
}
}
</PRE>
<!-- END CODE //-->
<P>MissileSprite also defines a method called init(), which starts the missile at the given x coordinate. The GunManager will use init() to fire the missile, as you will see next.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">GunManager</FONT></H4>
<P>The function of the GunManager class is to communicate the player’s commands to the GunSprite and MissileSprite. In other words, it translates the raw input given by the player into arguments to the sprite methods. Let’s see how this is done.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Computing Variables</FONT></H4>
<P>First of all, the constructor of the GunManager initializes the GunSprite and MissileSprite variables, <I>gun</I> and <I>missile</I>, and also initializes several variables used during game play. By computing these values at initialization, you can cut down on the amount of calculation required when the game is running at full tilt.</P>
<P>In general, you should always try to precompute commonly used values, and use constants (i.e., <I>final</I> variables) whenever possible. In keeping with this philosophy, here’s the GunManager’s constructor:</P>
<!-- CODE //-->
<PRE>
static final int MISSILE_WIDTH = 3;
static final int MISSILE_HEIGHT = 27;
static final int MISSILE_SPEED = -27; // missile flies upward
static final Color MISSILE_COLOR= Color.red;
public GunManager(int width,int height,
Image gunImage,Intersect target[],Applet a) {
this.width = width;
this.height = height;
gun = new GunSprite(gunImage,a);
gun_width = gunImage.getWidth(a)/2;
gun_height = gunImage.getHeight(a);
gun_y = height - gun_height;
min_x = gun_width;
max_x = width - gun_width;
gun_min_x = 0;
gun_max_x = width - 2*gun_width;
mis_min_x = min_x-2;
mis_max_x = max_x-2;
gun.setPosition(width/2-gun_width,gun_y); // center gun
missile = new MissileSprite(MISSILE_WIDTH,MISSILE_HEIGHT
MISSILE_COLOR,MISSILE_SPEED,
height-gun_height,
0,target);
}
</PRE>
<!-- END CODE //-->
<P>In case you feel discombobulated, Figure 5-12 illustrates what all these variables mean for the GunSprite.
</P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/05-12.jpg',465,585 )"><IMG SRC="images/05-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/05-12.jpg',465,585)"><FONT COLOR="#000077"><B>Figure 5-12</B></FONT></A> GunManager variables</P>
<P>Why all these variables? The gun will be controlled by the x coordinate of the mouse, and we want the center of the gun aligned with the mouse pointer. Thus, GunSprite needs to be drawn at an offset, <I>gun_width</I>, from the mouse x coordinate. Furthermore, the missile launcher should stay within the applet’s bounding rectangle, regardless of where the mouse goes. If the mouse pointer is less than <I>min_x</I>, for example, the gun should be drawn at <I>gun_min_x</I>, and the missile fired at <I>mis_min_x</I>. The case for <I>max_x</I> is similar.</P>
<P>Listing 5-9 shows the GunManager.</P>
<P><B>Listing 5-9</B> GunManager class</P>
<!-- CODE //-->
<PRE>
public class GunManager {
private GunSprite gun; // your gun
private int gun_width; // width of gun
private int gun_height;
private MissileSprite missile; // missile
static int width, height; // applet dimensions
private int min_x,max_x; // min and max x coords
// for gun movement
private int gun_min_x,gun_max_x;
private int mis_min_x,mis_max_x;
private int gun_y;
static final int MISSILE_WIDTH = 3;
static final int MISSILE_HEIGHT = 27;
static final int MISSILE_SPEED = -27; // missile flies upward
static final Color MISSILE_COLOR= Color.red;
public GunManager(int width,int height,
Image gunImage,Intersect target[],Applet a) {
this.width = width;
this.height = height;
gun = new GunSprite(gunImage,a);
gun_width = gunImage.getWidth(a)/2;
gun_height = gunImage.getHeight(a);
gun_y = height - gun_height;
min_x = gun_width;
max_x = width - gun_width;
gun_min_x = 0;
gun_max_x = width - 2*gun_width;
mis_min_x = min_x-2;
mis_max_x = max_x-2;
gun.setPosition(width/2-gun_width,gun_y);
missile = new MissileSprite(MISSILE_WIDTH,MISSILE_HEIGHT,
MISSILE_COLOR,MISSILE_SPEED,
height-gun_height,
0,target);
// move gun to the given x coordinate
public void moveGun(int x) {
if (x <= min_x) {
gun.setPosition(gun_min_x,gun_y);
}
else if (x >= max_x) {
gun.setPosition(gun_max_x,gun_y);
}
else {
gun.setPosition(x-gun_width,gun_y);
}
}
// fire missile from given x coordinate
public void fireMissile(int x) {
if (!missile.isActive()) { // if missile sprite
// isn't active
if (x <= min_x) {
missile.init(mis_min_x);
}
else if (x >= max_x) {
missile.init(mis_max_x);
}
else {
missile.init(x-2); // initialize missile
}
}
}
// update all the parameters associated with the
// gun. In this case, only the missile needs to move
// automatically. Also the gun manager checks if the
// missile hits anything
public void update() {
missile.update();
}
// paint all sprites associated with gun
public void paint(Graphics g) {
gun.paint(g);
missile.paint(g);
}
// accessor function for gun
public GunSprite getGun() {
return gun;
}
public int getGunY() {
return gun_y;
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="167-171.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="177-179.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -