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

📄 591-596.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:Daleks!</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=14//-->
<!--PAGES=591-596//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch13/588-590.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="596-599.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 14<BR>Daleks!
</FONT></H2>
<P><I>Ethan Koehler</I></P>
<P>This chapter presents an enhanced version of a classic computer game. You must help Dr. Who evade his ruthless but dim-witted enemies, the Daleks. The Daleks will move closer to you every turn. If they reach your square, the game is over. At your disposal are a limited number of sonic screwdrivers (handy devices that destroy adjacent Daleks), an ability to teleport to random destinations, and your superior intellect. The Daleks can also be killed when they attempt to move into the same square on the board, which reduces them to a pile of rubble. Any Daleks trying to move into a square containing rubble will also be destroyed.
</P>
<P>In developing this applet, I paid a lot of attention to the issues of bandwidth and processor speed. The sounds, graphics, and animations used are simple but effective. The result is a program that loads and runs quickly&#151;features that are easy to overlook with Java. In addition, many game parameters are customizable from HTML, meaning the game board, images, and intro screen can be modified to incorporate different themes and challenges.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Playing the Game</FONT></H3>
<P>When the applet first runs, the player is greeted with an animated intro screen and a bar showing the progress of loading images. When the images are done loading, the player can press any key to begin the game. Figure 14-1 shows a typical board setup on level 1. The player&#146;s character always begins in the center of the screen. The Daleks are scattered randomly around the board and move one square in the direction of the player every turn. The numeric keypad is used to control movement (diagonal movement is allowed). The player may press &#147;S&#148; to activate a sonic screwdriver to destroy adjacent Daleks. The number of remaining screwdrivers is displayed in the info bar at the bottom of the screen. The player may teleport at any time by pressing &#147;T&#148;. This will move the player to a random space on the board, which may be adjacent to one of the Daleks, resulting in the player&#146;s capture and the end of the game. Finally, the player can press &#147;L&#148; to make a &#147;last stand,&#148; allowing the Daleks to move continuously until the player or all the Daleks are destroyed.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/14-01.jpg',492,357 )"><IMG SRC="images/14-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/14-01.jpg',492,357)"><FONT COLOR="#000077"><B>Figure 14-1</B></FONT></A>&nbsp;&nbsp;The Daleks! board</P>
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Setting Up the Game</FONT></H3>
<P>The applet is contained in two classes, daleks14 and animatedIntroClass. A daleks14 object contains all the game routines and directs the traffic throughout the game. An animatedIntroClass object contains the methods necessary to create the title screen shown between games. Listing 14-1 shows the init() method of the daleks14 class, which is automatically called when the applet is loaded.
</P>
<P><B>Listing 14-1</B> The daleks14.init() method</P>
<!-- CODE //-->
<PRE>
public void init()  &#123;
//  get parameters from the calling HTML file
getHTMLParameters();

       //  initialize dalek arrays
      dalX = new int[maxEnemies];
      dalY = new int[maxEnemies];
      dalF = new int[maxEnemies];

      drStartX = maxX / 2;
      drStartY = maxY / 2;

      // create enough room to fit gameboard and status bar
      resize(maxX*imgW,maxY*imgH&#43;25);

      // load game graphics
     doctorPic[0] = getImage(getCodeBase(),"dw1l.gif");
     doctorPic[1] = getImage(getCodeBase(),"dw1r.gif");
     doctorPic[2] = getImage(getCodeBase(),"dw2l.gif");
     doctorPic[3] = doctorPic[2];
     doctorPic[4] = getImage(getCodeBase(),"dw3l.gif");
     doctorPic[5] = doctorPic[4];
     doctorPic[6] = getImage(getCodeBase(),"dw4l.gif");
     doctorPic[7] = getImage(getCodeBase(),"dw4r.gif");
     doctorPic[8] = getImage(getCodeBase(),"dw5l.gif");
     doctorPic[9] = getImage(getCodeBase(),"dw5r.gif");
     dalekPic[0] = getImage(getCodeBase(),"dalekl.gif");
     dalekPic[1] = getImage(getCodeBase(),"dalekr.gif");
     rubblePic[0] = getImage(getCodeBase(),"daldl.gif);
     rubblePic[1] = getImage(getCodeBase(),"daldr.gif");
     deadPic = getImage(getCodeBase(),"drdead.gif");

     // load graphic digits
        for (int j=0; j&lt;10; j&#43;&#43;)
              digit[j] = getImage(getCodeBase(),"green"&#43;j&#43;".gif");

        for (int j=0; j&lt;10; j&#43;&#43;)  &#123;
              tracker.addImage(doctorPic[j], j);
              tracker.addImage(digit[j], j&#43;10);
              &#125;
       for (int j=0; j&lt;2; j&#43;&#43;)  &#123;
              tracker.addImage(dalekPic[j], 20&#43;j);
              tracker.addImage(rubblePic[j], 22&#43;j);
              &#125;
       tracker.addImage(deadPic, 24);

       // start loading images
       tracker.checkAll(true);

      // create the off-screen bitmap
      nextScreen = createImage(maxX*imgW,maxY*imgH&#43;25);
      offScreenGC = nextScreen.getGraphics();

      prepareScreen();

animateIntro.setInfo(nextScreen, maxX, maxY, imgH, imgW, title,
                     flyingWidget, numWidgets);
      animateIntro.start();
      animIntro = true;
      repaint();
      &#125;
</PRE>
<!-- END CODE //-->
<P>This procedure sets up the game. First, a call is made to getHTMLParameters(void), which allows the user to customize many aspects of the game. The arrays of Dalek information are initialized at this point. By waiting to allocate the arrays, the method allows any number of Daleks in the game. A call to resize(int, int) ensures that the game graphics will fit in the applet window. The method then assigns each graphic image to the appropriate Image object. The various Images contained in the <I>doctorPic</I> array are needed because the player&#146;s appearance changes at certain levels (mimicking Dr. Who&#146;s ability to regenerate on the TV show). Each player and Dalek has a right- and left-facing Image. All the Image objects are given to a newly created MediaTracker object <I>tracker</I>, which handles loading the graphics and reports on their progress. The <I>nextScreen</I> variable is basically a worksheet Image where we build each successive game screen, which can be accessed by using <I>offScreenGC</I>, its graphics context. The call to prepareScreen() draws the current game status on the offscreen Image. Finally, the animatedIntroClass object is initialized and run.</P>

<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Programming Tip</B></FONT>
<P>Java addresses some of the complexity of C&#43;&#43; by eliminating the ability to dynamically allocate memory. After an array is initialized, it cannot be extended, which can greatly reduce the flexibility of Java applets. Daleks! avoids this problem by waiting to initialize the Dalek arrays until the maximum number of Daleks allowed is determined (this can be set through an HTML &lt;param&gt; tag).
</P>
</TABLE>

<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch13/588-590.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="596-599.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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