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

📄 223-228.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:Extending Your 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,&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=6//-->
<!--PAGES=223-228//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="217-223.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="228-230.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 6-4</B> GunSprite class</P>
<!-- CODE //-->
<PRE>
class GunSprite extends BitmapSprite
implements Moveable,Intersect &#123;

  protected GunManager gm;          // pointer to manager class

  public GunSprite(Image i, Applet a,GunManager gm) &#123;
    super(i,a);
    this.gm = gm;
  &#125;

  // the following methods implement Moveable:

  public void setPosition(int x,int y) &#123;
    locx = x;
    locy = y;
  &#125;

  public void setVelocity(int x,int y) &#123;

  &#125;

  public void updatePosition() &#123;

  &#125;

  // the following methods implement Intersect:

  // compare bounding boxes
  public boolean intersect(int x1,int y1,int x2,int y2) &#123;

    return visible &#38;&#38; (x2 &gt;= locx) &#38;&#38; (locx&#43;width &gt;= x1)
      &#38;&#38; (y2 &gt;= locy) &#38;&#38; (locy&#43;height &gt;= y1);

  &#125;

  // tell manager to display the hit
  public void hit() &#123;

    gm.handleHit();           // notify manager of hit

  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><B>Listing 6-5</B> UFO class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
public class UFO extends BitmapLoop implements Intersect &#123;

  byte state;

  // UFO states
  static final byte STANDBY = 0;
  static final byte ATTACK = 1;
  static final byte RETREAT = 2;
  static final byte LAND = 3;
  static final byte EXPLODE = 4;

  // probability of state transitions
  static final double STANDBY_EXIT = .95;
  static final double ATTACK_EXIT = .95;
  static final double RETREAT_EXIT = .95;
  static final double LAND_EXIT = .95;
  static final double FLIP_X = 0.9;
  static final int RETREAT_Y = 33;

  // bitmap animations
  protected Image ufo[];             // ufo animation
  protected Image attack[];          // attack animation
  protected Image explode[];         // explosion sequence

  // instance vars
  int max_x, max_y;                  // max coords of this UFO
  int explosion_counter;            // counter for explosion
                                    //   bitmaps
  UFOManager um;

  // class vars
  static Intersect target;           // refers to the gun
  static int gun_y;                  // the y-coord of gun
  static GameManager game;           // ptr to game manager

  // constructor: initialize image references, instance vars
  public UFO(Image ufoImages[],
     Image attackImages[],
            Image explodeImages[],
            int max_x,int max_y,
            UFOManager um,
            Applet a) &#123;
    super(0,0,null,ufoImages,a);
    this.max_x = max_x;
    this.max_y = max_y;
    currentImage = getRand(ufoImages.length);
    ufo = ufoImages;
    attack = attackImages;
    explode = explodeImages;
    game = (GameManager)a;
    this.um = um;
    startStandby();

  &#125;

  // finish initializing info about the player's gun
  //   this way, the ufo can communicate with the gun
  static public void initialize(GunManager gm) &#123;
    target = gm.getGun();             // refers to gun sprite
    gun_y = gm.getGunY();             // get gun y-coordinate
  &#125;

  // implement Intersect interface:
  public boolean intersect(int x1,int y1,int x2,int y2) &#123;

    return visible &#38;&#38; (x2 &gt;= locx) &#38;&#38; (locx&#43;width &gt;= x1)
      &#38;&#38; (y2 &gt;= locy) &#38;&#38; (locy&#43;height &gt;= y1);

  &#125;

  // this is called if a missile hits the alien
  public void hit() &#123;
    // alien is invulnerable when it's attacking
    //   but it gets "pushed back"
    if (state == ATTACK) &#123;
      locy -= 17;
    &#125;
    // otherwise explode!
    else if (state != EXPLODE) &#123;
      startExplode();              // start explode state
      game.incrementScore();       // add to score
      um.killed();                 // tell UFOManager
                                   //  another UFO's dead
    &#125;

  &#125;


  // set state and images loop
  public void init() &#123;
    startStandby();
    images = ufo;
    restore();
  &#125;

  // this implements the state machine
  public void update() &#123;
    // if alien hits target, notify target, and explode if
    //    it's not in attack or explode mode
    //    otherwise retreat
    if ((locy &#43; height &gt;= gun_y) &#38;&#38; state != EXPLODE &#38;&#38;
       target.intersect(locx,locy,locx&#43;width,locy&#43;height)) &#123;
       target.hit();
       if (state != ATTACK ) &#123;
         startExplode();
         return;
       &#125;
       else &#123;
         startRetreat();
       &#125;
      &#125;

    // otherwise, update alien state

    double r1 = Math.random();
    double r2 = Math.random();
    switch (state) &#123;
    case STANDBY:
      if (r1 &gt; STANDBY_EXIT) &#123;
       if (r2 &gt; 0.5) &#123;
          startAttack();
       &#125;
       else &#123;
          startLand();
       &#125;

      &#125;
      // flip ufo's x-direction if it goes too far right
      //   or left, or if random variable passes threshold
      else if ((locx &lt; width) || (locx &gt; max_x - width) ||
         (r2 &gt; FLIP_X)) &#123;
       vx = -vx;
      &#125;
      break;
    case ATTACK:

      if ((r1 &gt; ATTACK_EXIT) || (locy &gt; gun_y - 17)) &#123;
       startRetreat();
      &#125;
      else if ((locx &lt; width) || (locx &gt; max_x - width) ||
              (r2 &gt; FLIP_X)) &#123;
       vx = -vx;
      &#125;

      break;
    case RETREAT:
      if (r1 &gt; RETREAT_EXIT) &#123;
       if (r2 &gt; 0.5) &#123;
          startAttack();
       &#125;
       else &#123;
          startStandby();
       &#125;
      &#125;
      else if (locy &lt; RETREAT_Y) &#123;
       startStandby();
      &#125;
      break;
    case LAND:

      if (r1 &gt; LAND_EXIT) &#123;
       startStandby();
      &#125;
      else if (locy &gt;= max_y-height) &#123;
       landingRoutine();
      &#125;
      break;

    case EXPLODE:
      explosion_counter&#43;&#43;;     // bump counter

                                       // suspend once animation
                                       //   is finished
      if (explosion_counter == explode.length) &#123;
       suspend();
      &#125;

    &#125;

    super.update();                    // call BitmapLoop's update()
  &#125;

  // when the alien lands successfully
  protected void landingRoutine() &#123;
    game.alienLanded();         // tell game manager that
                                 //   the UFO's landed
    suspend();
  &#125;

  // start standby state
  protected void startStandby() &#123;
    vx = getRand(8)-4 ;
    vy = 0;
    state = STANDBY;
  &#125;

  // start attack state
  protected void startAttack() &#123;
    vx = getRand(10)-5;
    vy = getRand(5)&#43;7;
    images = attack;
    state = ATTACK;
  &#125;

  // start retreating state
  protected void startRetreat() &#123;
    vx = 0;
    vy = -getRand(3) - 2;
    images = ufo;
    state = RETREAT;
  &#125;

  // start landing state
  protected void startLand() &#123;
    vx = 0;
    vy = getRand(3) &#43; 2;
    state = LAND;
  &#125;

  // start explosion state
  protected void startExplode() &#123;
    images = explode;         // set bitmap to explosion sequence
    currentImage = 0;         // start at beginning of animation
    explosion_counter = 0;    // count the number of frames
    um.playExplosion();       // play explosion sound
    state = EXPLODE;

  &#125;

  // return a random int from 0 to x
  static public int getRand(int x) &#123;
    return (int)(x * Math.random());
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="217-223.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="228-230.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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