Current area: HOME -> Java Games tutorial #4
|
Java Games tutorial #4
Before
we start adding new code to the project, we're going to clean up
what's already there. What do we mean by "clean up"?
Simple: we're going to take the time to enforce some good
programming habits. In particular, we're going to replace
"hard-coded" numbers with symbolic constants.
I
can already hear you asking, "Hard-coded numbers? Symbolic
constants? What's he talking about?" An example should
clear everything up.
If
you examine the code in Battleship.java (and if you don't have
the code, you might want to get it from tutorial #3), you'll
find the following line in the constructor:
this.setSize(600,
500);
where
600 is the horizontal size of the game in pixels, and 500 is the
vertical height, also in pixels. We're going to replace both 600
and 500 with static constants, like this:
public
static final int GAME_WIDTH = 600;
public
static final int GAME_HEIGHT = 500;
.
.
.
this.setSize(GAME_WIDTH,
GAME_HEIGHT);
Why
bother with this? There are several reasons. First off, it will
save you time. We'll always define constants like GAME_WIDTH
and GAME_HEIGHT at the top of our
files. That way, if you ever need to change them, you just
scroll to the top of the file and edit the number. Using the old
method, you'd have to search through the code until you found
the setSize
statement and change the numbers there.
Second--and
more important--this technique helps reduce bugs. Suppose that
you have to refer to the game width (600) in several places
throughout the code. You might have statements like:
public
void paint(Graphics gfx)
{
gfx.setColor(Color.black);
gfx.fillRect(0,
0, 600, 500);
gfx.setColor(Color.red);
int
i
= 0;
while
(i
< 10) {
gfx.drawLine(i
* 600 / 10, 0, i
* 600 / 10, 500);
}
}
Now
imagine that you want to change the width of the game from 600
pixels to 640 pixels. You'd have to find every instance
of the number 600 and change it to 640. Even if you did this
perfectly, you might still introduce bugs--especially if you
have another value of 600 that doesn't represent the screen
width and you change it to 640 by accident.
Now
consider the same code using our symbolic constants:
public
static final int GAME_WIDTH = 600;
public
static final int GAME_HEIGHT = 500;
public
static final int LINE_COUNT = 10;
public
void paint(Graphics gfx)
{
gfx.setColor(Color.black);
gfx.fillRect(0,
0, GAME_WIDTH, GAME_HEIGHT);
gfx.setColor(Color.red);
int
i
= 0;
while
(i
< LINE_COUNT) {
gfx.drawLine(i
* GAME_WIDTH / LINE_COUNT, 0, i
* GAME_WIDTH / LINE_COUNT, GAME_HEIGHT);
}
}
Now,
no matter how many times you use GAME_WIDTH
and GAME_HEIGHT in the code, you
can change them all simultaneously just by redefining the
constants at the top of the file. Still want to change the
game's width from 600 to 640? No problem--just change this:
public
static final int
GAME_WIDTH = 600;
to
this:
public
static final int
GAME_WIDTH = 640;
Finally,
as you've probably already noticed, using constants helps others
understand your code. The original paint
method above is pretty abstract--you probably can't tell what
it's supposed to do. The second paint
method makes much more sense--you can see that it's supposed to
draw lines from the top of the screen to the bottom, evenly
spaced along the x-direction.
Now
that we've (hopefully) convinced you that constants are
wonderous and beautiful, let's go put some in our code.
|