📄 539-543.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 the JAVAroids 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=13//-->
<!--PAGES=539-543//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="533-539.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="543-547.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">The Asteroid Sprite</FONT></H4>
<P>The Asteroid sprite, shown in Listing 13-10, derives from RotateablePolygon. The variable <I>value</I> stores the number of points the player gets for shooting it There are three intersection calculations in the Asteroid definition—one for each type of asteroid-object collision. Again, we use the simple algorithm of determining whether the object’s center is within <I>max_radius</I> of the Asteroid.</P>
<P><B>Listing 13-10</B> Asteroid class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Asteroid sprite class
//
/////////////////////////////////////////////////////////////////
public class Asteroid extends RotateablePolygon {
// maximum rate of rotation
static final int MAX_ROTATE_RATE = 8;
// value of this asteroid
public int value;
public Asteroid(int tx[], int ty[], int n,
int centerx, int centery, Color c,
int w, int h, int r, int v) {
super(tx,ty,n,centerx,centery,c,w,h,r);
value = v; // set value
// set rotation rate
rate = GameMath.getRand(2*MAX_ROTATE_RATE)-MAX_ROTATE_RATE;
setFill(true); // fill the polygon
}
/////////////////////////////////////////////////////////////////
// intersection routines
/////////////////////////////////////////////////////////////////
// check for intersection with fire:
// compute midpt of the fire
// and see if it's within the max_radius of this asteroid
public boolean intersect(Fire f) {
if (isActive() && f.isActive()) {
int midptx = (f.p.xpoints[1] + f.p.xpoints[0]) / 2;
int midpty = (f.p.ypoints[1] + f.p.ypoints[0]) / 2;
return (Math.abs(midptx - locx) < max_radius) &&
(Math.abs(midpty - locy) < max_radius);
}
else
return false;
}
// check for intersection with Ship
// see if ship's center is within max_radius of this
// asteroid, with 2 pixels of leeway on each side
public boolean intersect(Ship s) {
return isActive() && s.isActive() &&
(Math.abs(s.locx - locx+2) < max_radius) &&
(Math.abs(s.locy - locy+2) < max_radius) ;
}
// check for intersection with Enemy
// see if Enemy's center is within max_radius of this
// asteroid, with 2 pixels of leeway on each side
public boolean intersect(Enemy e) {
return isActive() && e.isActive() &&
(Math.abs(e.locx - locx+2) < max_radius) &&
(Math.abs(e.locy - locy+2) < max_radius) ;
}
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">The Ship Sprite</FONT></H4>
<P>Ship is the sprite that the player controls. It inherits from RotateablePolygon, and provides methods for rotating left, rotating right, and thrusting. Ship overrides RotateablePolygon’s update() method (which provides the continuous rotation used by Asteroid). The new update() method rotates the ship only in response to a rotateLeft() or rotateRight() message. The thrust() method updates the ship’s velocity, based on the angle <I>theta</I> (inherited from RotateablePolygon). The Ship class is shown in Listing 13-11.</P>
<P><B>Listing 13-11</B> Ship class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Ship sprite: adds a few methods to RotateablePolygon
//
/////////////////////////////////////////////////////////////////
public class Ship extends RotateablePolygon {
// default rate of rotation is 15 degrees
static final int ROT_RATE = 15 ;
// has ship rotated?
boolean rotated = false;
// actual amount of rotation
int rotation_amount;
/////////////////////////////////////////////////////////////////
// Ship constructor
/////////////////////////////////////////////////////////////////
public Ship(int tx[], int ty[], int n, // coordinates
int centerx, int centery, // location
Color c, // color
int w, int h, // screen bounds
int r) { // ship length
super(tx,ty,n,centerx,centery,c,w,h,r);
// align the nose of the ship with theta = 0
// this is necessary because the direction of ship's
// thrust is based on theta, so the angles must be
// rotated so that the angle of the ship's nose
// and theta match.
for (int i=0 ; i < angle.length; i++) {
angle[i] = checkAngleBounds(angle[i]-90);
}
rotate(-90); // initially ship
// faces up
}
/////////////////////////////////////////////////////////////////
//
// Rotation methods: called by ShipManager
//
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// rotate left => decreasing theta in screen coordinate system
/////////////////////////////////////////////////////////////////
public void rotateLeft() {
rotated = true;
rotation_amount = -ROT_RATE;
}
/////////////////////////////////////////////////////////////////
// rotate right => increasing theta in screen coordinate system
/////////////////////////////////////////////////////////////////
public void rotateRight() {
rotated = true;
rotation_amount = ROT_RATE;
}
/////////////////////////////////////////////////////////////////
// override update to prevent continuous rotation
/////////////////////////////////////////////////////////////////
public void update() {
if (isActive()) {
updatePosition();
if (rotated) {
rotate(rotation_amount);
rotated = false;
}
else {
updatePoints();
}
}
}
/////////////////////////////////////////////////////////////////
//
// handle ship thrusting:
// update ship's velocity based on direction it's pointed
//
/////////////////////////////////////////////////////////////////
static final double ACCEL_FACTOR = 1.0;
public void thrust() {
vx = (int)Math.round((double)vx +
GameMath.cos(theta)*ACCEL_FACTOR);
vy = (int)Math.round((double)vy +
GameMath.sin(theta)*ACCEL_FACTOR);
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="533-539.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="543-547.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -