📄 522-526.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=522-526//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="519-521.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="526-530.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Moving and Rotating Polygons</FONT></H3>
<P>We’re going to subclass two Sprite classes now: MoveablePolygon and RotateablePolygon.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">The MoveablePolygon Class</FONT></H4>
<P>MoveablePolygon will subclass from PolygonSprite, which we defined above. The polygon will be stored in the int arrays <I>tx[]</I> and <I>ty[],</I> which are the x and y coordinates of polygon’s vectors in its local coordinate system:</P>
<!-- CODE SNIP //-->
<PRE>
protected int tx[],ty[]; // offset vectors
</PRE>
<!-- END CODE SNIP //-->
<P>The MoveablePolygon constructor will receive the local vectors and store them in <I>tx[]</I> and <I>ty[].</I> By storing these vectors, it’ll be easy to compute the new screen coordinates of the polygon every time it moves. Since it’s so convenient to specify a polygon in a local system, we will also provide, in Listing 13-5, a new constructor for PolygonSprite that takes local coordinates and converts them to the screen coordinates required by Polygon <I>p</I>.</P>
<P><B>Listing 13-5</B> PolygonSprite constructor for local coordinates</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
// constructor: take center and offset vectors
/////////////////////////////////////////////////////////////////
public PolygonSprite(int tx[], int ty[], int n,
int centerx, int centery, Color c) {
int x[], y[];
x = new int[n];
y = new int[n];
for (int i=0; i<n; i++) { // compute abs coords
x[i] = centerx + tx[i];
y[i] = centery + ty[i];
}
p = new Polygon(x,y,n);
locx = centerx;
locy = centery;
color = c;
visible = true;
fill = false; // don't fill polygon
}
</PRE>
<!-- END CODE //-->
<P>The MoveablePolygon constructor will call this PolygonSprite constructor before storing the local vectors. In addition, MoveablePolygon declares the instance variables <I>height</I> and <I>width</I>, which store the screen bounds, and <I>max_radius</I>, which is the approximate maximum radius of the polygon. The method updatePosition() uses <I>max_radius</I> to decide if the polygon should wrap around to the other side. Here’s a small excerpt of updatePosition() (from MoveablePolygon), which wraps the polygon from the right side of the screen to the left.</P>
<!-- CODE SNIP //-->
<PRE>
// if polygon is off the rhs of display
// move it to the left
if (locx - max_radius > width) {
locx -= (width + 2*max_radius);
}
</PRE>
<!-- END CODE SNIP //-->
<P>Another important method is updatePoints(), which accomplishes the local to screen coordinate transformation:
</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
// convert from local coords to screen coords of the
// polygon points
/////////////////////////////////////////////////////////////////
public void updatePoints() {
for (int i=0; i<p.npoints; i++) {
p.xpoints[i] = locx + tx[i];
p.ypoints[i] = locy + ty[i];
}
}
</PRE>
<!-- END CODE //-->
<P>The source for the MoveablePolygon is shown in Listing 13-6. Note how this class builds upon the PolygonSprite.
</P>
<P><B>Listing 13-6</B> MoveablePolygon class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// MoveablePolygon: encapsulates a moveable PolygonSprite
//
/////////////////////////////////////////////////////////////////
class MoveablePolygon extends PolygonSprite
implements Moveable
{
protected int tx[],ty[]; // offset vectors
protected int vx; // velocity x;
protected int vy; // velocity y;
protected int height = 1000,
width = 1000; // default screen limits
protected int max_radius = 0; // max radius of polygon
// methods:
/////////////////////////////////////////////////////////////////
// Constructor: construct with offset vectors
/////////////////////////////////////////////////////////////////
public MoveablePolygon(int tx[], int ty[], int n,
int centerx, int centery, Color c) {
super(tx,ty,n,centerx,centery,c); // constructor
vx = vy = 0;
this.tx = new int[n];
this.ty = new int[n];
for (int i=0; i<n; i++) {
this.tx[i] = tx[i]; // save offset vectors
this.ty[i] = ty[i];
}
}
/////////////////////////////////////////////////////////////////
// Constructor: construct with offset vectors,
// initialize screen bounds and max_radius
/////////////////////////////////////////////////////////////////
public MoveablePolygon(int tx[], int ty[], int n,
int centerx, int centery, Color c,
int w, int h, int r) {
super(tx,ty,n,centerx,centery,c); // constructor
vx = vy = 0;
this.tx = new int[n];
this.ty = new int[n];
for (int i=0; i<n; i++) {
this.tx[i] = tx[i]; // save offset vectors
this.ty[i] = ty[i];
}
height = h;
width = w;
max_radius = r;
}
/////////////////////////////////////////////////////////////////
// implement Moveable methods
/////////////////////////////////////////////////////////////////
// move polygon to specifiedlocation
public void setPosition(int x, int y) {
locx = x;
locy = y;
// compute screen coords of polygon from local coords
for (int i=0; i<p.npoints; i++) {
p.xpoints[i] = locx + tx[i];
p.ypoints[i] = locy + ty[i];
}
}
// set velocity of polygon
public void setVelocity(int x, int y) {
vx = x;
vy = y;
}
public void scale(double factor) {
for (int i=0; i<p.npoints; i++) { // scale offset vectors
tx[i] = (int)Math.round(factor*tx[i]);
ty[i] = (int)Math.round(factor*ty[i]);
}
updatePoints();
}
// update position of center of polygon. Wrap center
// to other side of screen if polygon exits screen bounds
public void updatePosition() {
locx += vx;
locy += vy;
// if center of polygon is off the rhs of display
// move it to the left
if (locx - max_radius > width) {
locx -= (width + 2*max_radius);
}
// if center of polygon is off the lhs of display
// move it to the right
else if (locx < -max_radius ) {
locx += (width + 2*max_radius);
}
// if center of polygon is off the bottom of display
// move it to the top
if (locy - max_radius > height) {
locy -= (height + 2*max_radius);
}
// if center of polygon is off the top of display
// move it to the bottom
else if (locy < -max_radius ) {
locy += (height + 2*max_radius);
}
}
/////////////////////////////////////////////////////////////////
// convert from local coords to screen coords of the
// polygon points
/////////////////////////////////////////////////////////////////
public void updatePoints() {
for (int i=0; i<p.npoints; i++) {
p.xpoints[i] = locx + tx[i];
p.ypoints[i] = locy + ty[i];
}
}
/////////////////////////////////////////////////////////////////
// default update method
/////////////////////////////////////////////////////////////////
public void update() {
if (isActive()) {
updatePosition(); // move center
updatePoints(); // compute polygon points
}
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="519-521.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="526-530.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -