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

📄 260-264.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:Creating Customizable Games with the AWT</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=7//-->
<!--PAGES=260-264//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="255-260.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="264-267.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 7-5</B> OptionsDialog class</P>
<!-- CODE //-->
<PRE>
class OptionsDialog extends Dialog &#123;
  Label l[] = new Label[3];
  TextField t[] = new TextField[2];
  Button b[] = new Button[2];
  CheckboxGroup cg = new CheckboxGroup();
  Checkbox c[] = new Checkbox[2];
  Panel p = new Panel();
  GameManager gm;

  public OptionsDialog(Frame parent,GameManager gm) &#123;
    super(parent,"Alien Landing Options",true);
    this.gm = gm;
    setLayout(new GridLayout(4,2,13,13));
    l[0] = new Label("Starting Level",Label.LEFT);
    l[1] = new Label("Energy Decrement",Label.LEFT);
    l[2] = new Label("Sound",Label.LEFT);
    t[0] = new TextField(String.valueOf(gm.startLevel),3);
    t[1] = new TextField(String.valueOf(gm.energyDec),3);
    c[0] = new Checkbox("On",cg,gm.sound);
    c[1] = new Checkbox("Off",cg,!gm.sound);
    p.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
    p.add(c[0]);
    p.add(c[1]);
    b[0] = new Button("OK");
    b[1] = new Button("Cancel");
    add(l[0]);
    add(t[0]);
    add(l[1]);
    add(t[1]);
    add(l[2]);
    add(p);
    add(b[0]);
    add(b[1]);
    pack();

  &#125;

  // handle actions
  public boolean action(Event e,Object o) &#123;
    if (e.target instanceof Button) &#123;
      String str = (String)o;
       // if user presses OK
      if (str.equals(b[0].getLabel())) &#123;
      parseDialog();
      &#125;
      // else user pressed cancel, so
      // don't do anything
      hide();
      dispose();
      return true;
    &#125;
    else return false;
  &#125;

  protected void parseDialog() &#123;
    int start = -1,energy = -1;
    boolean sound;
    try &#123;
       start = Integer.parseInt(t[0].getText());
    &#125;
    catch (Exception exc) &#123;
    &#125;

    try &#123;
       energy = Integer.parseInt(t[1].getText());
    &#125;
    catch (Exception exc) &#123;
    &#125;

    sound = c[0].getState();
    gm.setOptions(start,energy,sound);
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P><B>Listing 7-6</B> New GameManager variables and methods</P>
<!-- CODE //-->
<PRE>
// customizable parameters
  int startLevel;
  int energyDec;
  boolean sound;

    int width, height;                    // applet dimensions

  // strings
  String scoreString = "Score: ";
  String ufoLandedString = "UFOs Landed: ";
  String gameOverString =     "  GAME OVER  ";
  String alienLandingString = "Alien Landing";
  int stringWidth;
  String introString[] = new String[7];

  GameFrame f;

  public void init() &#123;
    setBackground(Color.black);         // applet background
    width = 240;
    height = 270;
    resize(width,height);
    startLevel = 2;
    energyDec = 5;
    sound = false;

    f = new GameFrame(this,width,height);

    loadImages();

    try &#123;
       expsound = getAudioClip(getCodeBase(),"Explosion.au");
    &#125;
    catch (Exception e) &#123; &#125;
    um = new     UFOManager(startLevel,MAX_LEVEL,width,height,ufoImages,
                      attackImages,explodeImages,
                      this,expsound);
    gm = new GunManager(MAX_ENERGY,energyDec,width,height,gunImage,
                      um.getUFO(),
                      this);
    um.initialize(gm);              // initialize gun parameters

    playing = false;                   // not playing
    screen = INTRO;                    // show intro screen

    image = createImage(width,height); // make offscreen buffer
    offscreen = image.getGraphics();
    offscreen.setFont(bigFont);        // font for intro
    fm = offscreen.getFontMetrics(bigFont); // font metrics
    stringWidth = fm.stringWidth(alienLandingString);

    introString[0] = "You are Humanity's last hope!";
    introString[1] = "Destroy the green Alien Landers";
    introString[2] = "by using the Mouse to Move";
    introString[3] = "your Missile Launcher. Click";
    introString[4] = "to Fire Missile. If 5 Aliens";
    introString[5] = "Land, or Energy Runs Out,";
    introString[6] = "Humans will be Eaten Alive!";

 &#125;

  // handle game over
  public void gameOver() &#123;
    if (playing) &#123;
      playing = false;
      screen = GAME_OVER;
      f.gameOver();     // restore menu items
    &#125;
  &#125;

  // CUSTOMIZE MANAGERS!
  public void setOptions(int startLevel,
                   int energyDec,boolean sound) &#123;
    if (startLevel &gt;= 1 &#38;&#38; startLevel &lt; MAX_LEVEL) &#123;
      this.startLevel = startLevel;
      um.setStartLevel(startLevel);   // set startLevel
    &#125;
    if (energyDec &gt;= 0 &#38;&#38; energyDec &lt;= MAX_ENERGY) &#123;
      this.energyDec = energyDec;
      gm.setEnergyDec(energyDec);     // set energy lost
    &#125;
    this.sound = sound;
    um.setSound(sound);             // set sound

  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading30"></A><FONT COLOR="#000077">Using Applet Parameters</FONT></H3>
<P>Finally, let&#146;s see how users can customize your game applets by using <I>applet parameters. </I></P>
<H4 ALIGN="LEFT"><A NAME="Heading31"></A><FONT COLOR="#000077">What Are Applet Parameters?</FONT></H4>
<P>The APPLET tag allows you (or another user) to refer to your game applet in an HTML file. The PARAM tag allows the user to pass additional parameters that the applet can read. Applet parameters are like command line arguments&#151;the parameters are available when the applet starts running, and interpretation of the parameters is up to the particular applet.
</P>
<P>For example, applet parameters might tell the Alien Landing applet where to get image or sound files, how many ships the player should start with, and what the maximum number of aliens is. Here&#146;s an APPLET tag that does this:</P>
<!-- CODE SNIP //-->
<PRE>
 &lt;APPLET code="Alien.class" width=170 height=213&gt;
  &lt;PARAM name="NumShips" value="3"&gt;
  &lt;PARAM name="ShipImage" value="ship.gif"&gt;
  &lt;PARAM name="MaxAliens" value= 13&gt;
&lt;/APPLET&gt;
</PRE>
<!-- END CODE SNIP //-->
<P>As you see, the PARAM tag has two attributes:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;NAME: This is the name of the parameter.
<DD><B>&#149;</B>&nbsp;&nbsp;VALUE: This is the value of the parameter.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading32"></A><FONT COLOR="#000077">Reading Applet Parameters</FONT></H4>
<P>To read applet parameters, use the instance method
</P>
<!-- CODE SNIP //-->
<PRE>
public String getParameter(String name);
</PRE>
<!-- END CODE SNIP //-->
<P>defined in the Applet class. For example, the following code fragment reads parameters defined by the APPLET tag above. You could put it in the init() method, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
// this code occurs in an applet
String numShips = getParameter("NumShips");
String shipImage = getParameter("ShipImage");
</PRE>
<!-- END CODE SNIP //-->
<P>Then the applet can set game conditions or load image files depending on the parameters it reads.
</P>
<P>By using applet parameters, your game can be customized as soon as it starts running!</P>
<H3><A NAME="Heading33"></A><FONT COLOR="#000077">Quick AWT Reference</FONT></H3>
<P>The java.awt package offers a rich variety of classes and methods. Here are reference tables for the material we&#146;ve covered in this chapter.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="255-260.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="264-267.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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