📄 255-260.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, 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=7//-->
<!--PAGES=255-260//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="250-255.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="260-264.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>When you call the disable() method of a MenuItem, that item can no longer be selected. For example, the Abort Game item is disabled initially. The enable() method permits an item to be selected.
</P>
<P>The Options Menu will have only one choice:</P>
<!-- CODE SNIP //-->
<PRE>
m2.add(new MenuItem("Change Options..."));
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, you must set the MenuBar of the GameFrame:
</P>
<!-- CODE SNIP //-->
<PRE>
setMenuBar(menubar);
</PRE>
<!-- END CODE SNIP //-->
<P>The next step is to define actions for the menu items. To do this, we need an action() method in GameFrame.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">Handling Menu Actions</FONT></H4>
<P>The action() method tests whether a given menu item is selected, and makes the appropriate response. For example, when the player chooses New Game, the following occurs:
</P>
<!-- CODE //-->
<PRE>
// handle actions
public boolean action(Event e,Object o) {
if (e.target instanceof MenuItem) {
String s = (String)o;
if (e.target == newItem) {
gm.newGame();
newItem.disable();
abortItem.enable();
}
...
</PRE>
<!-- END CODE //-->
<P>First, the GameManager starts a new game. Then the New Game menu item is disabled, and Abort Game is enabled.
</P>
<P>When the player chooses Exit, the Frame window closes, the applet stops running, and system resources are freed:</P>
<!-- CODE SNIP //-->
<PRE>
...
else if (s.equals("Exit")) {
hide();
gm.stop();
gm.destroy();
dispose();
}
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, let’s see what happens if the player chooses Change Options...:
</P>
<!-- CODE SNIP //-->
<PRE>
...
else if (s.equals("Change Options...")) {
d = new OptionsDialog(this,gm);
d.show();
}
</PRE>
<!-- END CODE SNIP //-->
<P>A modal dialog box pops up, awaiting the user’s response. Let’s lay out this dialog, and enable it to parse user input.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading28"></A><FONT COLOR="#000077">Defining the Customization Dialog</FONT></H4>
<P>We will let the player alter the difficulty of the game, and enable or disable sound. The two difficulty parameters that we will allow the player to modify are the starting level (the number of aliens on the screen initially) and the energy decrement (the amount of energy the player loses when hit by an alien).
</P>
<P>Figure 7-15 shows the Dialog box we will create.</P>
<P><A NAME="Fig15"></A><A HREF="javascript:displayWindow('images/07-15.jpg',260,153 )"><IMG SRC="images/07-15t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-15.jpg',260,153)"><FONT COLOR="#000077"><B>Figure 7-15</B></FONT></A> OptionsDialog</P>
<P>Because we will need to provide actions for this Dialog, let’s create a subclass called OptionsDialog. The OptionsDialog window is laid out using a GridLayout. The Labels Starting Level, Energy Decrement, Sound, and the two TextFields are inserted in the following order:
</P>
<!-- CODE //-->
<PRE>
public OptionsDialog(Frame parent,GameManager gm) {
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);
add(l[0]);
add(t[0]);
add(l[1]);
add(t[1]);
add(l[2]);
</PRE>
<!-- END CODE //-->
<P>Next is a Panel that contains two Checkboxes, arranged with FlowLayout:
</P>
<!-- CODE SNIP //-->
<PRE>
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");
</PRE>
<!-- END CODE SNIP //-->
<P>This Panel is then inserted into the grid, along with the two buttons:
</P>
<!-- CODE SNIP //-->
<PRE>
add(p);
add(b[0]);
add(b[1]);
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, pack() resizes the window to the preferred size of its components:
</P>
<!-- CODE SNIP //-->
<PRE>
pack();
}
</PRE>
<!-- END CODE SNIP //-->
<P>Now OptionsDialog must process the input from the player. The action() method waits for a click on the OK button or the Cancel button, and then it closes and disposes of the window.
</P>
<!-- CODE //-->
<PRE>
// handle actions
public boolean action(Event e,Object o) {
if (e.target instanceof Button) {
String str = (String)o;
// if user presses OK
if (str.equals(b[0].getLabel())) {
parseDialog();
}
// else user's pressed cancel, so
// don't do anything
hide();
dispose();
System.out.println("press d");
return true;
}
else return false;
}
</PRE>
<!-- END CODE //-->
<P>If the player clicked OK, the input is processed by parseDialog(), and is passed along to the GameManager class, which updates the parameters. The static method Integer.parseInt() converts a String to an int.
</P>
<!-- CODE //-->
<PRE>
protected void parseDialog() {
int start = -1,energy = -1;
boolean sound;
try {
start = Integer.parseInt(t[0].getText());
}
catch (Exception exc) {
}
try {
energy = Integer.parseInt(t[1].getText());
}
catch (Exception exc) {
}
sound = c[0].getState();
gm.setOptions(start,energy,sound);
}
</PRE>
<!-- END CODE //-->
<P>That completes the customization dialog for the Alien Landing game!
</P>
<H3><A NAME="Heading29"></A><FONT COLOR="#000077">Customized Source Code for GameManager</FONT></H3>
<P>The complete code for the GameFrame, OptionsDialog, and the new methods in GameManager is shown in Listings 7-4 through 7-6.
</P>
<P><B>Listing 7-4</B> GameFrame class</P>
<!-- CODE //-->
<PRE>
class GameFrame extends Frame {
protected Panel p;
protected MenuItem newItem,abortItem;
protected GameManager gm;
protected int width,height;
protected OptionsDialog d;
public GameFrame(Applet app,int width,int height) {
super("Alien Landing");
this.width = width;
this.height = height;
gm = (GameManager)app;
resize(width+13,height+65);
setResizable(false);
MenuBar menubar = new MenuBar();
Menu m1 = new Menu("Game");
newItem = new MenuItem("New Game");
m1.add(newItem);
abortItem = new MenuItem("Abort Game");
abortItem.disable();
m1.add(abortItem);
m1.add(new MenuItem("Exit"));
Menu m2 = new Menu("Options");
m2.add(new MenuItem("Change Options..."));
menubar.add(m1);
menubar.add(m2);
setMenuBar(menubar);
p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(app);
setLayout(new BorderLayout());
add("Center",p);
setCursor(Frame.CROSSHAIR_CURSOR);
show();
}
public void gameOver() {
abortItem.disable();
newItem.enable();
}
// handle actions
public boolean action(Event e,Object o) {
if (e.target instanceof MenuItem) {
String s = (String)o;
if (e.target == newItem) {
gm.newGame();
newItem.disable();
abortItem.enable();
}
else if (e.target == abortItem) {
gm.gameOver();
}
else if (s.equals("Exit")) {
hide();
gm.stop();
gm.destroy();
dispose();
}
else if (s.equals("Change Options...")) {
d = new OptionsDialog(this,gm);
d.show();
}
return true;
}
else return false;
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="250-255.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="260-264.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -