There are no new object-oriented topics this week, but I am going to introduce you to a 2D games environment which might help you understand the nature of classes and objects more clearly by allowing you to do graphical object-oriented programming in a games context.
Note that you will not be assessed on this! I am introducing it to you to provide a "fun" environment in which you can experiment with object-oriented programming.
The Golden T Game Engine (GTGE) is a Java library developed by Golden T Studios which allows you to easily develop 2D games. The source code is available here.
Type in the following Java code:
import com.golden.gamedev.*; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.KeyEvent; public class MyGame extends Game { public void initResources() { } public void update(long elapsedTime) { } public void render(Graphics2D g) { } public static void main (String args[]) { System.out.println("Game development environment OK! "); } }The basic structure of this should be familiar from your work with Java so far. You have a class making up the main program and it has a main() method which is the entry point of the program. Compile and run the program, to check your settings are OK. You should get the message:
Game development environment OK!
When developing with GTGE you need to create a Game class with three methods, blank at the moment: initResources(), update() and render(). As you build up your game you will fill these in. Your class needs to include "extends Game" as shown above. We will look at what this means later in the unit.
You're now going to actually set up a graphical environment for the game. With GTGE, games can either be full-screen or windowed. For the purposes of the tutorials, we are going to use windowed games, but a real game would probably be full-screen.
To set up a windowed environment, add the following code to your main() method in the MyGame class:
GameLoader gameLoader = new GameLoader(); MyGame myGame = new MyGame(); gameLoader.setup(myGame,new Dimension(640,480),false); gameLoader.start();
This code:
The basic architecture of any game involves a game loop:
The game is continuously looping round and round;
each time the game loop runs, the following tasks are performed:
The methods of the GTGE game class that you need to fill in are as follows: initResources(), update() and render(). The function of each is described below:
To illustrate the purpose of the render() method, you're now going to use Java graphics commands to actually draw something (a yellow circle) on the screen. Fill in the render() method with the following code:
public void render(Graphics2D g) { g.setColor(Color.BLACK); g.fillRect(0,0,getWidth(),getHeight()); g.setColor(Color.YELLOW); g.fillOval(320,240,20,20); }Compile and run. You'll now see a yellow ball in the middle of the screen, and the screen will have a black background. The first two lines set the colour to black and fill in the whole of the window; the second two lines set the colour to yellow and draw a ball at coordinates (320,240) of width 20 and height 20.
Obviously a real game is going to need to react to keyboard input. To do this we use the keyDown() method, which is an in-built method of the Game class, to react to a key being pressed down.
if(keyDown(KeyEvent.VK_LEFT)) { // Move left x--; } else if (keyDown(KeyEvent.VK_RIGHT)) { // Move right x++; } else if (keyDown(KeyEvent.VK_UP)) { // Move up y--; } else if (keyDown(KeyEvent.VK_DOWN)) { // Move down y++; }keyDown () takes a parameter representing the key to test; it tests whether a particular key is currently pressed down. KeyEvent.VK_LEFT, etc, represent the cursor keys. Change your code which draws the ball so that it is drawn at x and y rather than 320 and 240. Try the program.
So far we’ve just drawn simple geometrical shapes. However, in a game we more commonly use graphics to represent the game characters. How do we do this in GTGE? We actually use standard a standard Java feature, the BufferedImage class. First add a BufferedImage as an attribute of your MyGame class:
private BufferedImage image;Then load it in the initResources() method:
image = getImage("U:\\images\\hero.png");and then draw it in render():
g.drawImage(image,x,y,null);where x and y are the coordinates of the game character.
You have now seen how to create a simple graphical application using GTGE. What this unit is really all about, though, is object-oriented development. So we are now going to use GTGE to display your Hero class from Topics 3 and 4 on the screen.
Try writing a chase game in which a Monster (see advanced question, topic 4) chases the Hero. The game should end when the monster has eaten the hero. You'll need to give your Monster class a BufferedImage attribute and a draw() method, as for the Hero. If you need a monster image there is one here.