📄 010-014.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:Fundamental Java</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=1//-->
<!--PAGES=010-014//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="001-010.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="014-016.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Other Benefits</FONT></H4>
<P>The benefits of Java go far beyond its use on the Web. Java’s blend of multiplatform, multi-threaded, and networking capabilities bring the promise of a universal OS closer to reality. For example, you can compile stand-alone applications once and execute the code on a diverse set of architectures, eliminating the need for porting your code. You can make use of Java’s communication and multithreaded capabilities to create applications that incorporate the decision-making powers of a heterogeneous network of computers. Java heralds a new age in computing, with you at the helm!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Current Limitations to Writing Games in Java</FONT></H4>
<P>Since this all sounds like an advertisement for using Java, let’s mention two disadvantages to writing games in Java:
</P>
<DL>
<DD><B>•</B> <I>Interpreted Java code is 10 to 20 times slower than comparable compiled C++ code</I>. Although this seems like a huge gap, Java’s performance is adequate for many types of games. In addition, the promised release of the Java “just-in-time” compiler and specialized Java chips will bring the speed of Java to C++ levels. The performance gap between Java and C++ will be less of a concern in the near future.
<DD><B>•</B> <I>The abstraction provided by the Java virtual machine and Abstract Windowing Toolkit makes platform-specific optimizations and tricks difficult</I>. For example, you won’t be able to rely on a really cool feature of a particular video board without eliminating the multiplatform nature of your applet. So the tradeoff here is between specialized performance and wide distribution.
</DL>
<P>Now it’s time for an advanced introduction to Java. We’re going to start with object-oriented fundamentals.
</P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Object-Oriented Fundamentals</FONT></H3>
<P>Java is an <I>object-oriented</I> language, and to use Java effectively, you need to build your programs and games in an object-oriented manner. While traditional languages such as C treat data as passive entities that are manipulated by procedures, object-oriented languages bundle data together with the procedures that operate on the data. This bundle is called an <I>object</I>. When writing Java games, you need to think in terms of the states and behaviors of the objects involved.</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Thinking with States and Behaviors</FONT></H4>
<P>Look at the room around you. You’ll see lots of things, such as lamps, computers, a piano, and a pet dog, if your room looks anything like mine. How might you represent these things in a computer program? One way is to model the <I>states</I> that the thing can be in, and the <I>behaviors</I> that the thing exhibits. States are usually described by nouns and adjectives (speed, energy, color, rich, happy), whereas behaviors correspond to verbs (attacking, firing, driving, flipping, vomiting). An object, with its bundle of data and procedures, allows you to represent the functionality of real-world (and imaginary-world) things with their states and behaviors. Here are some examples.</P>
<P>Let’s start with a lamp. A lamp exists in two states: on or off. By turning the knob on the lamp, you change the lamp state. So the behavior of a lamp is flipping from on to off, or off to on. State is essentially a passive concept—a thing exists in various states. Behavior is action and doing—the transition from state to state, for example.</P>
<P>The only way to turn a lamp on or off is by turning the knob. Put another way, you can’t alter the lamp’s state without using the knob. The lamp’s state is shielded, or <I>encapsulated</I>, from the outside world. The knob is the lamp’s <I>interface</I> to the world—the point at which another object can send a <I>message</I> to the lamp to switch states.</P>
<P>Figure 1-5 shows one way of representing a lamp object. The lamp’s state, shown as the inner circle of the diagram, is protected by the knob, which is the lamp’s interface.</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/01-05.jpg',461,316 )"><IMG SRC="images/01-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-05.jpg',461,316)"><FONT COLOR="#000077"><B>Figure 1-5</B></FONT></A> Diagram of lamp object</P>
<P>Here’s another example of an object—an alien UFO in a shoot-‘em-up video game. The UFO states and behaviors will depend on the actual game, of course, but let’s see what they might be. The UFO object is either dead or alive, so being dead or alive is one aspect of the UFO’s state. Other aspects might include direction, speed, or energy left. UFO behaviors could include updating position, changing direction, or dying. When does a UFO interact with other objects? Perhaps when you’ve fired a missile at it, and the missile scores a direct hit. The missile object sends a message to the UFO object—you’ve been hit!—and the UFO blows up. The UFO has an interface so other objects can notify it when a collision has occurred. A diagram of the UFO object we’ve described is shown in Figure 1-6. Of course, the actual state and behaviors will depend on the particular game.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/01-06.jpg',463,317 )"><IMG SRC="images/01-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-06.jpg',463,317)"><FONT COLOR="#000077"><B>Figure 1-6</B></FONT></A> Diagram of UFO object</P>
<P>Now that you’ve defined the states and behaviors of an object, you can define the <I>class</I> that it belongs to. Let’s see how to do this.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Defining a Class</FONT></H4>
<P>By defining a class, you construct a new type from which you can create actual objects. A Java class resembles a struct in C, except that it can hold <I>methods</I> as well. (A method is an object-oriented term for a function or procedure.) As an example, let’s create a Lamp class, based on our discussion in the preceding section. Here it is:</P>
<!-- CODE //-->
<PRE>
class Lamp {
private boolean lampIsOn; // instance variable
public void turnKnob() { // method
lampIsOn = !lampIsOn;
}
public Lamp() { // constructor
lampIsOn = false;
}
}
</PRE>
<!-- END CODE //-->
<P>This tiny class illustrates the three elements found in most class definitions: variables, methods, and constructors. Let’s discuss them in turn.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Variables</FONT></H4>
<P>Use variables to represent the state of an object. For example, the state of a Lamp object is on or off, so a boolean variable, which takes on the values <I>true</I> or <I>false</I>, will suffice:</P>
<!-- CODE SNIP //-->
<PRE>
private boolean lampIsOn;
</PRE>
<!-- END CODE SNIP //-->
<P><I>lampIsOn</I> is an example of an <I>instance variable</I>, which means that each Lamp object will have its own copy of <I>lampIsOn</I>. Declare variables in the same manner as you would in C.</P>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Methods</FONT></H4>
<P>Use methods to implement the behaviors of an object. For example, when another object “turns the knob” of a Lamp object, its state flips between on and off. The turnKnob() method implements this behavior by toggling <I>lampIsOn</I> between <I>true</I> and <I>false</I>.</P>
<!-- CODE SNIP //-->
<PRE>
public void turnKnob() {
lampIsOn = !lampIsOn; // ! is unary negation operator
}
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="001-010.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="014-016.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -