|
||||||
An Introduction to Games Programming with JavaHow to Create a Game Console within a Java Applet in a Web BrowserStudies show that certain computer games can help improve a child's literacy and numeracy. It is, therefore, the Java programmer's duty to start creating games
The UK's Daily Mail has informed the nation of How brain training games 'give children a grade boost. In her article Fiona MacRae says that:
Which is great news, not just for children, but also for any programmers planning to create new games, especially when Java makes games development so easy. Creating a Java Games Applet AppletAll that's required for a simple Java game are:
So, the first job is to do is to create the applet: import java.applet.*;
import java.awt.*;
public class game extends Applet implements Runnable { The "Runnable" statement allows threads to be run within the applet. These threads enable the applet to process inputs from the user (such as mouse clicks) at the same time as carrying out operations within the game. This means that the game applet can animate objects (such as figure 1 at the bottom of this article) and move them across a background (as can be seen in figure 2). Initiating the Java Game AppletThe programmer uses the init method (which runs automatically) to set up the initial state of the applet, and can use this to set the size of the screen and load the images: Image alien;
Image backdrop;
int alien_height;
int alien_width;
int applet_width = 300;
int applet_height = 225;
int x_speed;
int pos_speed = 2;
public void init () {
setSize(applet_width, applet_height);
alien = this.getImage (this.getDocumentBase (), "alien.png");
alien_width = alien.getWidth (null);
alien_height = alien.getHeight (null);
backdrop = this.getImage (this.getDocumentBase (), "background.jpg");
x_speed = pos_speed;
}
With everything loaded then then running of the game can be considered. Starting the Java AppletOnce the applet has run the init method then it will run the start method. In this example it is used to start the thread that is required for the animation to be carried out: int x_speed;
int pos_speed = 2;
public void start () {
Thread animation = new Thread (this);
animation.start ();
x_speed = pos_speed;
}
At this point the applet is up an running, and so the programmer can start to consider how the game should look. The Java Applet Game DisplayThe paint method is used to display items on the screen. This is run whenever the applet needs to be drawn: int x_pos = 0;
int y_pos = 30;
public void paint (Graphics g) {
g.drawImage (backdrop, 0, 0, this);
g.drawImage (alien, x_pos, y_pos, this);
}
However, nothing much will happen yet. The code for the animation is required. Animating Objects in a Java Applet GameAssociated with the runnable object's thread is the run method. This starts executing when the thread is started, and in this case it consists of an infinite loop that will move an image around the game's screen: int neg_speed = -1;
int y_speed;
boolean x_axis = true;
public void run () {
while (true) {
if (x_axis) {
if (x_pos + alien_width > applet_width) {x_speed = neg_speed;}
else if (x_pos < alien_width) {x_speed = pos_speed;}
x_pos += x_speed;
} else {
if (y_pos > applet_height - alien_height) {y_speed = neg_speed;}
else if (y_pos < alien_height) {y_speed = pos_speed;}
y_pos += y_speed;
}
repaint ();
try {Thread.sleep (20);} catch (InterruptedException ex) {}
}
}
The final statement of the method builds in a time delay. This, together with a screen repaint, creates the animation (which can be seen in figure 3). However, at the moment there is no user interaction. For that mouse clicks can be taken into account. Working With Mouse Clicks in a Java Based GameThreads allow user commands to be captured whilst the animation is running. The mouseDown method fires whenever a mouse button is clicked and, in this case, it's used to change the direction in which the image is moving (changing it from horizontal movement to vertical movement, and vice versa): public boolean mouseDown (Event e, int x, int y) {
if (x_axis) {
x_speed = 0;
y_speed = pos_speed;
x_axis = false;
} else {
x_speed = pos_speed;
y_speed = 0;
x_axis = true;
}
return true;
}
However, if the application is run at this point then a major problem can be observed. There is a terrible screen flicker. Removing Screen Flicker in a Java GameThe screen flicker is caused by Java updating the whole of the game screen, rather than just the components that have changed position. The solution is to use the update method and only update the essential areas of the screen: private Image dbImage;
private Graphics dbg;
public void update (Graphics g) {
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
}
Now any elements of the game will move smoothly around the screen. Obviously this particular game is unlikely to raise the IQ of any child, but it does show how easily and how quickly a simple, but professional looking game, can be created when a programmer uses Java and the Java applet. BNC101
The copyright of the article An Introduction to Games Programming with Java in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to Games Programming with Java in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||