📄 530-533.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:Building the JAVAroids 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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=13//-->
<!--PAGES=530-533//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="526-530.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="533-539.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Allocating Responsibility</FONT></H3>
<P>Now that we’ve spent much of this chapter building sprite classes, it’s time to think of how JAVAroids is going to be organized at the top level. Let’s see how we can organize the sprites involved.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">The Top-Level Organization</FONT></H4>
<P>First, the asteroids. Since there will be lots of asteroids floating around, and each one is going to have similar characteristics, such as shape, color, or the number of points it’s worth, it makes sense to have an asteroid manager that takes care of initializing the right number of asteroids with the proper attributes. The asteroid manager will also be in charge of game situations that involve more than one asteroid, such as asteroid division, and counting the number of asteroids. The asteroid manager will communicate to the asteroids and tell them when to appear and disappear.
</P>
<P>Each asteroid will be represented by an Asteroid sprite. The Asteroid sprite will be in charge of painting itself and keeping track of individual Asteroid characteristics, such as its radius, color, value, and position. The Asteroid sprite will derive from the RotateablePolygon sprite.</P>
<P>The Ship sprite will also be subclassed from the RotateablePolygon sprite, and it controls the appearance of the ship on the screen. It makes sense to have a ship manager class that keeps track of ship characteristics, such as the number of shots or shield strength, and translates instructions from the player (such as “rotate left”) into messages to the Ship sprite. The ship manager also tracks the sprites that the player fires (which we’ll call Fire sprites).</P>
<P>Similarly, we’ll have an Enemy sprite that draws enemy ships, and an enemy manager that controls when the enemy ships appear and when they fire. The effect manager will handle explosions (by creating an Explosion sprite) and sound effects from collisions. Finally, the game manager will be responsible for tasks that involve the entire game: initialization, displaying instructions and “Game Over” messages, and tracking score, to name a few.</P>
<P>Table 13-1 sums up the manager classes and the sprite objects that they control.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 13-1</B> Manager classes and sprite objects
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Manager Classes
<TH WIDTH="50%" ALIGN="LEFT">Sprites
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Game manager
<TD>None
<TR>
<TD>Asteroid manager
<TD>Asteroid sprite
<TR>
<TD>Ship manager
<TD>Ship sprite, Fire sprite
<TR>
<TD>Enemy manager
<TD>Enemy sprite, Fire sprite
<TR>
<TD>Effect manager
<TD>Explosion sprite
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Figure 13-11 illustrates the division of responsibility that we’ve outlined above. It’s quite general, but it illustrates how you can split the basic tasks of any game into smaller, logically coherent units. The arrows in the diagram show how the manager classes and the sprites get initialized. The managers are like puppeteers that manipulate the puppet sprites.
</P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/13-11.jpg',599,744 )"><IMG SRC="images/13-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-11.jpg',599,744)"><FONT COLOR="#000077"><B>Figure 13-11</B></FONT></A> Division of responsibility in JAVAroids</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Handling Collisions</FONT></H4>
<P>Objects in JAVAroids interact with one another when they collide. There are many types of collisions that can happen in this game. It’s up to the manager classes to handle the results of collisions between the sprites. Let’s say, arbitrarily, that the asteroid manager will be responsible for handling all collisions involving asteroid sprites. There are three types of collisions here:
</P>
<DL>
<DD><B>•</B> Asteroid with player’s ship
<DD><B>•</B> Asteroid with enemy ships
<DD><B>•</B> Asteroid with player fire or enemy fire
</DL>
<P>Now, we’ll say that the enemy manager will track collisions between the objects that it’s responsible for and the player’s ship. In particular, there are three more types of collisions here:
</P>
<DL>
<DD><B>•</B> Enemy ship with player’s ship
<DD><B>•</B> Enemy ship with player fire
<DD><B>•</B> Enemy fire with player’s ship
</DL>
<P>Figure 13-12 graphs the six types of collisions that are possible in the current implementation. The edges along the graph define which manager class is responsible for handling the collision.
</P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/13-12.jpg',600,517 )"><IMG SRC="images/13-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-12.jpg',600,517)"><FONT COLOR="#000077"><B>Figure 13-12</B></FONT></A> Collision handling graph</P>
<P>Now let’s start building the game. Although it may seem like a daunting task, the key is to construct the program piece by piece. First, let’s define the five new Sprite classes we need. Don’t worry—the definition of most of these classes is really easy, since we’ve built most of the functionality in the MoveablePolygon and RotateablePolygon classes!
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="526-530.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="533-539.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -