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

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="656-659.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="664-667.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Defining the Sprite Images</FONT></H4>
<P>Next comes the tedious job of defining each type of Sprite&#146;s default &#147;look&#148;. This is used in the absence of an animation array of Images. To speed things up, rather than actually drawing the Sprite every time it is required, we will have the program generate an offscreen Image and put it in the animation array. Subsequent calls can simply make use of the default Image. Besides raw speed, the advantage of this is that the same code can be used whether the Sprite is preanimated or not. Here&#146;s the method that will take care of creating the default Image:
</P>
<!-- CODE //-->
<PRE>
public synchronized void generateImage() &#123;

if( im==null) return;

              Graphics blah = im.getGraphics();
              anim = new Image[1];
switch( id) &#123;
       case ENEMY: &#123;
            theColor = new Color( (int)(Math.random()*255),(int)(Math.ran-
dom()*255),(int)(Math.random()*255));
              blah.setColor( theColor);
              blah.fillRect( 0,0,bounds.width,bounds.height);
              break;
               &#125;
       case BULLET: &#123;
              theColor = Color.green;
              blah.setColor( theColor);
              blah.fillRoundRect( 0,0,bounds.width,bounds.height,5,5);
              break;
              &#125;
       case USER: &#123;
              theColor = Color.blue;
              blah.setColor( Color.black);
              blah.fillRect( 0,0,bounds.width,bounds.height);
              blah.setColor( Color.white);
              blah.drawOval( 0,0, bounds.width, bounds.height);
              blah.setColor( theColor);
              blah.fillOval( 0,0, bounds.width, bounds.height);
              break;
              &#125;
        &#125;

              anim[0]=im;
              animIndex=0;
              animMax=1;
&#125;
</PRE>
<!-- END CODE //-->
<P>This takes care of all of our default IDs except MESSAGE, which we will handle later. We declare this method to be <I>synchronized</I> because we don&#146;t want the Sprite to try to draw the Image before it is finished being created. Next, let&#146;s actually create the drawing methods:</P>
<!-- CODE //-->
<PRE>
public void paintSprite( Graphics g) &#123;

g.setColor( Color.black);

if( lastX!=x || lastY!=y)
       g.fillRect( lastX,lastY,bounds.width,bounds.height);

       if( anim == null)
              generateImage();
g.drawImage(anim[animIndex],x,y,null);
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Animating the Sprites</FONT></H4>
<P>Notice how this method works just fine whether or not we have given this Sprite a nifty animation array. However, if there is an animation array, we need a method that will advance it as necessary. This method should also move the Sprite to its new location based on the speed attributes:
</P>
<!-- CODE //-->
<PRE>
public void advance() &#123;

if( anim != null) &#123;
       animIndex&#43;&#43;;
       if( animIndex &gt;= animMax )
              animIndex = 0;
       &#125;

lastX=x;
lastY=y;
x &#43;= warp*(int)(speedX&#43;.5);
y &#43;= warp*(int)(speedY&#43;.5);

bounds.move(x,y);
&#125;
</PRE>
<!-- END CODE //-->
<P>Be sure to understand that a negative <I>speedX</I> or <I>speedY</I> value is totally legitimate, and is used to move the Sprite in the left or up directions, respectively. The last method required for a fully functional Sprite class is the one that makes it actually do something. If you remember that Sprite extends Thread, you should realize it needs a run() method:</P>
<!-- CODE SNIP //-->
<PRE>
public void run() &#123;
       while(true) &#123;
              try&#123; sleep(DELAY); &#125; catch(Exception e);
              paintSprite( theG);
              advance();
              &#125;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Scrolling the Background</FONT></H3>
<P>The scrolling background is best accomplished in two parts: the terrain (foreground) and the star field (background). Because the Terrain uses some pretty wacky methods to achieve great speed, we will write it as its own class, and not as a subclass of Sprite. However, just to demonstrate how useful the Sprite class is, we will write the StarField class as an extension of it.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Understanding the Terrain Class</FONT></H4>
<P>How does one write a scrolling foreground? Many games use a graphic that is longer than the active playing field and that looks exactly the same at both ends. This can then be scrolled at high speeds past the user, and when it &#147;runs out,&#148; another copy of it (or another compatible graphic) is added. This works pretty well, but the repetitive nature of it can be quite annoying to the user if the graphic is not large enough. Of course, the drawback to a large graphic is that it requires large amounts of memory to store, and time to develop.
</P>
<P>To avoid the drawbacks associated with predrawn terrains, we are going to create a &#147;dynamic&#148; terrain that is continuously generated by a Thread specifically written for this task. A &#147;terrain&#148; looks something like Figure 16-2.</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/16-02.jpg',650,43 )"><IMG SRC="images/16-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-02.jpg',650,43)"><FONT COLOR="#000077"><B>Figure 16-2</B></FONT></A>&nbsp;&nbsp;Terrain to be scrolled</P>
<P>This may seem complicated to draw. However, if you look at the terrain as a series of polygons, each with four sides, a way of creating it should become clear. Figure 16-3 gives an example.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/16-03.jpg',650,43 )"><IMG SRC="images/16-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-03.jpg',650,43)"><FONT COLOR="#000077"><B>Figure 16-3</B></FONT></A>&nbsp;&nbsp;Same terrain divided into quadrilaterals</P>
<P>To create this effect we will call upon the Polygon class. The terrain will be composed of a series of Polygons, each the same width, that have at least two points in common. This is highlighted in Figure 16-4. Every time the terrain is &#147;scrolled,&#148; the leftmost Polygon is removed and a new one is generated at the far right. At high speeds, this gives the illusion that the user is whizzing by a landscape tremendously fast.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/16-04.jpg',467,398 )"><IMG SRC="images/16-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-04.jpg',467,398)"><FONT COLOR="#000077"><B>Figure 16-4</B></FONT></A>&nbsp;&nbsp;Terrain coordinate system</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Coding the Terrain Class</FONT></H4>
<P>The Terrain class will use a Vector to store the number of Polygons of a specified width. In order to avoid using double-buffered graphics (which are just too slow for this) and to also avoid much flicker (which would ruin the high-speed effect), we will be using some tricky Graphics methods.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Declaring Variables</FONT></H4>
<P>To begin with, we start with some simple variable declarations. Notice that many of them are similar to ones we used in the Sprite class.
</P>
<!-- CODE //-->
<PRE>
import java.awt.*;
import java.util.*;
import java.lang.*;
import Sprite;

public class Terrain extends Thread &#123;

Vector v; // stores the Polygons
int WIDTH; // pixel width of one Polygon
Rectangle bounds; // Rectangle bounding the entire Terrain
Graphics theG; // Graphics to draw on
int lasty=0; // the y-coordinate of the "last" Polygon created
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="656-659.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="664-667.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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