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

📄 217-223.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=217-223//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="211-217.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="223-228.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 6-2</B> GunManager class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
public class GunManager &#123;

  private GunSprite gun;               // your gun
  private int gun_width;               // width of gun
  private int gun_height;
  private MissileSprite missile;       // missile
  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;
  private boolean displayHit;
  private int energy;

  private int maxEnergy;
  private int energyDec;

  private GameManager game;             // ptr to game manager
  static int width, height;             // applet dimensions

  static final int ENERGY_PER_HIT = 5; // energy used per hit

  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 maxEnergy,int energyDec,int width,int height,
                   Image gunImage,Intersect target[],Applet a) &#123;
    this.maxEnergy = maxEnergy;
    this.energyDec = energyDec;
    this.width = width;
    this.height = height;
    gun = new GunSprite(gunImage,a,this);

    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&#43;13,
                            0,target);

    game = (GameManager)a;              // set ptr to GameManager
  &#125;

  // set parameters for the new game
  public void newGame() &#123;
     gun.setPosition(width/2-gun_width,gun_y);
     gun.restore();
     displayHit = false;
     energy = maxEnergy;

  &#125;

  // move gun to the given x coordinate
  public void moveGun(int x) &#123;
    if (x &lt;= min_x) &#123;
      gun.setPosition(gun_min_x,gun_y);
      &#125;
    else if (x &gt;= max_x) &#123;
      gun.setPosition(gun_max_x,gun_y);
      &#125;
    else &#123;
      gun.setPosition(x-gun_width,gun_y);
    &#125;
  &#125;

  // fire missile from given x coordinate
  public void fireMissile(int x) &#123;
    if (!missile.isActive()) &#123;     // if missile sprite
                                       //   isn't active
      if (x &lt;= min_x) &#123;
       missile.init(mis_min_x);
      &#125;
      else if (x &gt;= max_x) &#123;
       missile.init(mis_max_x);
      &#125;
      else &#123;
       missile.init(x-1);                // initialize missile
      &#125;
    &#125;
  &#125;

  // update all the parameters associated with the
  //   gun. In this case, only the missile needs to move
  //   automatically.

  public void update() &#123;
    missile.update();
  &#125;

  // paint all sprites associated with gun
  // also paint status display for amount of energy left

  String energyString = "Energy";
  public void paint(Graphics g) &#123;
    // if gun is hit, flash a red rectangle
    //   instead of painting gun
    if (displayHit) &#123;
      g.setColor(Color.red);

      g.fillRect(0,gun_y,width,gun_height);
      displayHit = false;
    &#125;
    else &#123;
      gun.paint(g);
    &#125;
    missile.paint(g);

    // display energy left
    g.setColor(Color.red);
    g.drawString(energyString,3,13);
    g.fillRect(0,17,energy,10);
  &#125;

  // accessor function for gun
  public GunSprite getGun() &#123;
    return gun;
  &#125;
  // get the y-coordinate of the gun
  public int getGunY() &#123;
    return gun_y;
  &#125;

  // handles a hit from an alien
  public void handleHit() &#123;
    displayHit = true;             // set display hit flag
    energy -= energyDec;           // update energy
    if (energy &lt;= 0) &#123;     // game over if energy &lt;= 0
      game.gameOver();             // notify game manager
      gun.suspend();               // turn off sprites
      missile.suspend();
    &#125;
  &#125;

  // set the amount of energy lost per hit (energy decrement)
  public void setEnergyDec(int dec) &#123;
    energyDec = dec;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><B>Listing 6-3</B> UFOManager class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
public class UFOManager &#123;


  private UFO ufo[];                 // array of ufos

  int ufosKilled;                    // count ufos killed
  int level;                        // the level
                                   //     (i.e. #ufos on screen)
  int startLevel;
  int maxLevel;
  boolean playSound = false;         // initially no sound
  AudioClip expsound;                // sound clip of explosion

  // kill 13 ufos until next level
  static final int KILL_FOR_NEXT_LEVEL = 13;

  static int width, height;          // applet dimensions

  // constructor
  public UFOManager(int startLevel,int maxLevel,int width,int height,
                   Image ufoImages[],
                   Image attackImages[],
                   Image explodeImages[],
                   Applet a, AudioClip exp) &#123;
    this.startLevel = startLevel;
    this.maxLevel = maxLevel;
    this.width = width;
    this.height =  height;

    ufo = new UFO[maxLevel];
    for (int i=0; i&lt;ufo.length; i&#43;&#43;) &#123;
      ufo[i] = new UFO(ufoImages,attackImages,explodeImages,
                     width,height,this,a);
    &#125;
    expsound = exp;
    newGame();
  &#125;


  // allow the UFO class to communicate with the gun
  public void initialize(GunManager gm) &#123;
    UFO.initialize(gm);
  &#125;

  // set ufo at a random screen location
  private void initializePosition(Moveable m) &#123;
    m.setPosition(UFO.getRand(width - 100)&#43;50,
                 UFO.getRand(height - 150)&#43;31);

  &#125;

  // initialize parameters of new Game
  public void newGame() &#123;
    ufosKilled = 0;
    level = startLevel;              // start with startLevel ufos
                                     //   on the screen
    for (int i=0; i&lt;ufo.length; i&#43;&#43;) &#123;
      initializePosition(ufo[i]);
      if (i &gt;= level) &#123;              // suspend ufos
                                             //   above start level
       ufo[i].suspend();
      &#125;
    &#125;
  &#125;

  // return array of ufos
  public UFO[] getUFO() &#123;
    return ufo;
  &#125;

  // paint all ufos in a level
  public void paint(Graphics g) &#123;
    for (int i=0; i&lt;level; i&#43;&#43;) &#123;
      ufo[i].paint(g);
    &#125;
  &#125;

  // update all ufos in a level. Otherwise start
  //   ufo if it's not on screen
  public void update() &#123;
    for (int i=0; i&lt;level; i&#43;&#43;) &#123;
      if (ufo[i].isActive()) &#123;
       ufo[i].update();
      &#125;
      else &#123;                  // make new ufo
       initializePosition(ufo[i]);
       ufo[i].init();
      &#125;
    &#125;
  &#125;

  // tracks the number of ufos killed. If the
  //   num_killed is divisible by  KILL_FOR_NEXT_LEVEL
  //   increment the level
  public void killed() &#123;
    ufosKilled&#43;&#43;;
    if (ufosKilled % KILL_FOR_NEXT_LEVEL == 0) &#123;
      level = (level == maxLevel) ? maxLevel : level&#43;1;
    &#125;
  &#125;

  public void setStartLevel(int start) &#123;
    startLevel = start;
  &#125;

  public void setSound(boolean s) &#123;
    playSound = s;
  &#125;

  public void playExplosion() &#123;
    if (playSound &#38;&#38; expsound != null) &#123;
      expsound.play();
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="211-217.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="223-228.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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