|
|
|
|
|
|
|
CS 177/178 Programming with Multimedia Objects
Fall 2004
Elements of a Video game in Java
Lecture Notes for November 29, 2004
Prepared by Aditya Mathur
Reference for JAVARoids game: Black Art of Java Game Programming
by Joel Fan, Eric Ries, and Calin Tenitchi, Waite Group Press. 1996
|
|
|
|
A video game contains:
- Objects: These are also known as sprites.
- Animation: Sprites are animated and hence are capable of moving on the screen. Animation is achived in Java with the help of threads.
- Game Management: This is required for initializing various game variables such as the current score, instructions to the player, creating various management objects that are responsible for their actions during the game and initiating or stopping a game
- Event detection and processing: This is required to detect user initiated events, e.g. pressing a key to move a sprite, and internal events, e.g. collision of two objects.
|
|
Sprite:
- A figure on the screen capable of moving independent of other figures. Examples: ship, rectangle, text, and image.
- A sprite has a state and behavior. Sprite state includes its internal representation, its location on the screen, and its visibility. Sprite behavior includes painting and updating.
|
|
Threads:
- Sprites are animated using threads in Java.
- A thread is a subprogram that executes independently. For example, an applet can start a thread t1. Thread t1 could be, for example, responsible for moving the enemy sprites and while the applet is reponsible for all initialization, update, and handling events.
|
|
Animation
Animation is accomplished in Java using threads. Here is a summary description of the procedure used for animation using applets.
- The primary class that extends the applet class implements the Runnable interface. For example: public class GameManager extends Applet implements Runnable
- The GameManager class implements a method named start(). Here is a sample implementation:
Thread animation;
public void start() {
// start animation
if (animation == null) {
animation = new Thread(this);
if (animation != null) {
animation.start();
}
}
- Upon loading the applet, the init() method is executed followed by the start() method. As shown above, the start() method causes a new object, named animation in this example, is started. After having created animation, the run() method of animation is executed by the statement animation.start(); . Here is a sample run() method:
static final int REFRESH_RATE=100; // Screen refresh rate (in milliseconds)
public void run(){
while(!gameOver){
repaint(g);
updateObjects();
try{
Thread.sleep(REFRESH_RATE); // Make the thred go to sleep for some time.
}catch(Exception ex){}
}
}
Note that the run () method has a loop that continues until gameOver becomes true. Inside the loop, the screen is repainted and the state of each game object, e.g. enemy sprite, is updated. Then the thred goes to sleep for some time, which is equal to the REFRESH_RATE in this example. The loop resumes when the thread awakens.
- Usually the position of sprites that move is updated each time around in the loop and the screen repainted. Thus the sprite appears at a nerw position while its previous image is erased giving the impression that the sprite is moving. The sprite might have a different orientation in its new position.
Now move to the ThreadIlustrator class and then to JAVARoids game and examine the code. The primary class in this game is the GameManager which interacts with the other sprite management classes.
|
|
|
ThreadIlustrator Source |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|