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

📄 ch8.htm

📁 对于程序员来说可以利用JAVA来开发网络游戏!
💻 HTM
📖 第 1 页 / 共 2 页
字号:
people like me. So, remember that if you use the G and H keys
(or other middle keys) in your game, and it plays well for you,
it might not work out so well for players with different keyboards.
<P>
This might sound kind of picky, but part of Java programming is
trying to make everyone happy. Remember that Java games can be
enjoyed by a wide range of computer users. You should make it
a goal to do everything in your power to appease all of them.
Think of it this way: How many times in the past have you had
the opportunity to write one set of source code and have it work
on such a wide range of computer systems? I have personally been
involved with cross-platform multimedia development using C++,
and it's not very fun. My advice is to embrace Java and its support
for multiple platforms, and be open-minded when it comes to making
decisions that affect users of other platforms!
<P>
Now, where were we? Keyboards! The most common keys used on the
keyboard in games are the arrow keys. If you're writing an action
game, you might also have keys for firing and selecting between
weapons. When you're deciding on the keys to use, keep in mind
things like the creative usage of the Shift key in DOOM. If you
can limit the number of primary game control keys by making use
of a secondary key such as Shift, you've made the game controls
that much easier to use.
<H4><B>Rapid Fire</B></H4>
<P>
A common feature in action games is rapid fire, which involves
firing multiple times while a key is being held down. I don't
want to burden today's lesson with any source code, but I would
like to briefly cover how rapid fire could be implemented in Java
using standard keyboard support. As you will learn tomorrow, key
presses in Java are handled as events. For now, don't worry too
much about what an event is, except that it is something you can
write code to respond to. The importance is that an event occurs
for each key press, which means that you have the opportunity
to respond to each key press (and key release).
<P>
You don't, however, have the opportunity to respond to keys being
held down, which means that there is no direct way to implement
rapid fire. Alas, there is a fairly painless work-around! The
solution is to set a boolean member variable when a key is pressed,
and then fire the bullets in an event loop based on this boolean
variable being true. You receive a key release event when the
user lets go of the key, in which case you simply set the boolean
variable back to false. You control the speed of the rapid fire
by altering the speed of the event loop or by using a separate
delay counter.
<H3><A NAME="TheMouse"><B>The Mouse</B></A></H3>
<P>
Although the keyboard is firmly in place as the most necessary
of user input devices, the graphical nature of the Web establishes
the mouse, or a similar pointing device, as a standard input device
as well. The mouse, however, doesn't share the wide range of input
applications to games that the keyboard has. This stems from its
primary usage as a point-and-click device; if you haven't noticed,
lots of games don't follow the point-and-click paradigm.
<H4><B>Usage</B></H4>
<P>
In regard to gaming, the usefulness of the mouse is dependent
totally on the type of game and, therefore, the type of user interaction
dictated by the game. However, as quickly as some people write
off the mouse as being a useless interface in some games, others
praise the fine control it provides. A good example is DOOM. Personally,
I think the keyboard is much more responsive than the mouse and
the keyboard enables me to get around faster and with more control.
But I have friends who feel lost playing the game without a mouse.
<P>
Clearly, this is a situation in which the game designers saw a
way to provide support for both the keyboard and mouse. With the
exception of the most extreme cases, this should be your goal
as well. Different game players like different things, and the
safest solution is to hedge your bets and support all input devices
whenever possible. By following this simple rule, you can develop
games that can be appreciated and enjoyed by a broader group of
game players.
<H4><B>Interpreting Movement</B></H4>
<P>
Similar to the keyboard, Java support for the mouse is event-driven.
You'll learn more about the details of mouse events tomorrow,
but right now I want to cover the different interpretations of
mouse movement. The standard Java mouse event handlers provide
you with the current position of the mouse whenever the mouse
is moved. This position is referred to as the absolute position
of the mouse.
<P>
<I>Absolute position</I> is the specific on-screen location of
the mouse.
<P>
Figure 8.1 shows an example of absolute mouse position.
<P>
<A HREF="f8-1.gif" ><B>Figure 8.1 : </B><I>Absolute mouse position.</I></A>
<P>
The other type of position that is important when examining mouse
movement is the relative position of the mouse.
<P>
<I>Relative position</I> is the position of the mouse relative
to its prior position.
<P>
Relative mouse position is more useful in games because you are
usually concerned with whether the user moved the mouse left or
right, instead of whether the mouse is at position <TT><FONT FACE="Courier">(34,
272)</FONT></TT>, for example. Figure 8.2 shows an example of
relative mouse position.
<P>
<A HREF="f8-2.gif" ><B>Figure 8.2 : </B><I>Relative mouse position.</I></A>
<P>
Even though Java provides no direct support for relative mouse
movement, you can easily calculate a relative position by saving
the prior position and comparing the two. In this scenario, you
would maintain the prior mouse position in a member variable,
such as a <TT><FONT FACE="Courier">Point</FONT></TT> object. The
mouse movement event handler calculates the relative mouse position
as the differences between the X and Y values of the current mouse
position and the prior mouse position stored in the member variable.
These differences can then be used to determine how an object
in a game is moved. For example, a positive X difference in relative
position would correspond to an object being moved to the right.
<P>
Relative positioning is useful in a wide variety of games-basically,
any game in which you control something by specifying a change
in movement rather than an absolute location to move to. A good
example is a flight simulator, in which you control small changes
in the direction and altitude of the plane, rather than modifying
its exact position in the sky. An example of a game that would
work better with absolute mouse positioning is a Hogan's Alley
type game, in which the mouse's movement maps directly to the
movement of a gun sight used to shoot bad guys.
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
Today you learned about user input in games, including some useful
tips and problems to watch out for when designing the user input
support in your own games. You also learned about the two user
input devices supported by Java, the keyboard and mouse, along
with some suggestions about how to use them. You learned how rapid
fire can be implemented using Java keyboard support, and you finished
up with the two fundamental types of mouse positioning and how
they are used in games.
<P>
Although somewhat brief, today's lesson covered some important
issues when it comes to making determinations about user input
in games. The goal of today's lesson is mainly to encourage you
to put some thought into how you design the user input support
in your games. Tomorrow you get to put these design ideas to work
by learning exactly how to handle user input in Java.
<H2><A NAME="QA"><B><FONT SIZE=5 COLOR=#FF0000>Q&amp;A</FONT></B></A>
<BR>
</H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Will Java ever support more input devices, such as joysticks?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>It's hard to say for sure, but Sun has promised more extensive multimedia support in a future release of Java. Because joysticks are generally considered multimedia input devices, support for them might very 
well appear in a future release.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Are there any games that wouldn't require keyboard support?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Sure. Any game that requires extensive point-and-click style input, such as a card game, would probably be fine without any keyboard support. However, if you can figure out a way to add keyboard controls, by all 
means go for it.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Is it possible to mix keyboard and mouse controls?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Of course! This actually hits on an interesting point not covered in today's lesson: Many games work great by combining mouse and keyboard controls. A good example is the strafing feature in DOOM, which also 
works with the mouse; it is activated by holding down the Shift key while moving the mouse left or right.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><B><FONT SIZE=5 COLOR=#FF0000>Workshop</FONT></B></A>
</H2>
<P>
The Workshop section provides questions and exercises to help
you get a better feel for the material you learned today. Try
to answer the questions and at least think about the exercises
before moving on to tomorrow's lesson. You'll find the answers
to the questions in appendix A, &quot;Quiz Answers.&quot;
<H3><A NAME="Quiz"><B>Quiz</B></A></H3>
<OL>
<LI>What are the two types of mouse movement?
<LI>If you had to choose between supporting the keyboard or the
mouse, which would you choose?
<LI>How is interrupt <TT><FONT FACE="Courier">16h</FONT></TT>
used to handle key presses?
</OL>
<H3><A NAME="Exercises"><B>Exercises</B></A></H3>
<OL>
<LI>Play some commercial games and pay attention to what input
devices are supported and how.
<LI>Find out the limitations of input devices on computer systems
other than the type you have. For example, are some keys in different
locations or missing altogether?
<LI>Clean the roller mechanism inside your mouse; you need the
mouse in tip-top shape for the rest of this week's lessons!
</OL>
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch2gla.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch9.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

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