CS4281 Internet Application Development Anthony, Chan Ho Kwing 96116563

Dennis, Yu Hon Fai 96096615

Marius, Yung Ka Ho 96103600

Objectives 2

Game Features 3

Double buffered smooth animation 3

Collision Detection 3

Background Animation 3

Obstacles 3

Clock 3

System Design and Architecture 4

Game Loop 4

The Collider class and its’ subclasses 4

The Background class and its’ subclasses 6

Collision Detection 7

Circle to circle collision detection 7

Circle to line segment collision detection 7

Interface Design and User Manual 9

Interface Design 9

User Manual 11

Conclusion 12

What we have learned 12

Contributions 12

File Listing 13

Objectives

The objectives of this assignment are to implement a pinball machine in Java and use the object oriented system development approach to develop the system so as to enhance the knowledge of Java programming as well as game programming.

Game Features

Double buffered smooth animation

The game has used double buffer to eliminate flicker so that smooth animation has achieved.

Collision Detection

Relatively precise collision detection has been developed including ball to ball collision, ball to line collision and ball to paddle collision.

Background Animation

A background animation that simulates what the pilot see in a space ship flying through space has been implemented and therefore the player can see many stars in the game. The brightness of the stars are also calculated depending on the distance form the viewer, therefore far star will be dimmer and close star will be brighter.

Obstacles

Different obstacles have been added to the game, for example, the paddle and blob obstacles. They will behave differently when the pinball collided with them. For example marks will be given to the player when the blob is collided by the pinball.

Clock

A clock counting how long the player has played is implemented and displayed in the game.

System Design and Architecture

The system developed has followed discipline in object oriented system development, for example, polymorphism and inheritance have been used extensively and have made the the development of the system easier.

Game Loop

In each discrete time frame of the game, we move the ball and detect any collision occurred and we draw the game. In pseudo code:

MoveObjects();

CollisionDetection();

CollisionResponse();

DrawTheGame();

The Collider class and its’ subclasses

The Collider class and its subclasses

All the objects that will collide with the pinball including the obstacles and the paddles are subclasses of the Collider class.

abstract class Collider
{
int score = 0;
void response() { };
abstract boolean collided(PinBall ball);
abstract boolean boundOff(PinBall ball);
abstract void draw(Graphics g);
};

The abstract Collider class

The inheritance used here allow us to use a for loop to perform collision detection of the PinBall to any kind of Collider and this really make life easier.

for(int j=0;j<colliders.size();j++)
{
Collider collider = (Collider)colliders.elementAt(j);
if( collider.collided(theBall) )
{
collider.boundOff(theBall);
collider.response();
score += collider.score;
break;
}
}

And the polymorphism mechanism allows the colliding object to behave differently when collided with the pinball. Also, this approach also allow the programmer to add a new instance of colliding objects easy, for example, to add a new Blob instance, what the programmer need to do is the following code:

Blob blob = new Blob(applet,80,80,30,ballImage,hitImage,scoreImage);

colliders.addElement(blob);

By writing just 2 lines of code, a blob instance is added and the collision detection and response of the blob to the pinball and the drawing of the blob are handled automatically.

The Background class and its’ subclasses

The Background class and its subclasses

The StarBackground and the ScrollBackground are subclasses of the abstract Background class. This architecture has many benefits, first it allows us to swap the background animation easily by writing:

background = starBackground;

or

background = scrollBackground;

Secondly, new type of background animation can be easily created by deriving the Background class and implements the step() and draw(Graphics g) methods. For example, if the developer want to create a fire background that simulate the fire and use this fire background in the program, what he need is to do is extending the Background class and override the draw() method and step() method.

Collision Detection

Collision between obstacles and the pinball are detected in each discrete frame. Including PinBall to Blob collision (circle to circle), Pinball to Paddle collision (circle to line segment) and others.

Since collisions are detected in each discrete time frame, there are several problems that may arise.

Problem 1: moving too fast

If the pinball moving to fast, it may penetrate the obstacle in 2 successive time frame and this make the detection of the collision fail.

Circle to circle collision detection

Circle to circle collision detection is easy, we just calculate the distance of the two circles and see whether this is smaller then the sum of the radii of the circles, the distance is smaller, we know that collision has occurred.

i.e. Distance(C1, C2) <= R1+R2 implies two ball collided

Circle to line segment collision detection

Assuming the line segment is defined as its’ two end points P1(x1,y1) and P2(x2,y2). And the circle C(xc,yc,r) where x, y is the coordinates of the center and r is the radius.

We show below how the collision between PinBall and the DownBound is detected in the game. A DownBound object prevents the PinBall from moving down from it.

First we parameterize the line

x = x1+(x2-x1)*t

y = y1+(y2-y1)*t

To see whether a point P is inside the region of the BoundDown object, we first calculate the interception point I by solving:

x = x1+(Ix-x1)*t

y = y1+(Iy-y1)*t

Since I and P have the same x-coordinate, therefore

x = x1+(Px-x1)*t

y = y1+(Iy-y1)*t

Solving for Iy, we have Iy = y1+(y2-y1)*(Px-x1)/(x2-x1);

Then we compare Iy with Py and if Iy is smaller than Py, we know that the point P is inside the region of the BoundDown object.

Now we can use a bounding box to represent the PinBall and test whether each of the four corner of the bounding box is inside the BoundDown region.

Interface Design and User Manual

Interface Design

The user interface is designed to be easy to use and attractive so that the users can perform all the operations they want to do and also develop an immerse feeling towards the game.

The following screen shots show different states of the game.

Main menu of the pinball machine

Playing the pinball machine

Game paused!

Game Over

User Manual

The following table lists all the operations that the users can perform.

Operations / Actions
To Start the game / Press the start game button
To launch the ball / Use the mouse to drag the launcher and release the mouse
To move the left Paddle / Press the key "z" or "Z"
To move the right paddle / Press the key "/"
To pause or un-pause the game / Press the key "p" or "P"

Conclusion

What we have learned

Many features in Java language have been investigated, for example, inheritance, polymorphism, interface and Java’s thread programming are all covered in the game. Inheritance and polymorphism have been used extensively and the approach has helped to simplify the system and also allow the system to be easily modified, extended and maintained. Besides, game programming skills have been studied and this includes how to use a game thread to run a game, how collisions are detected, how the pinball is moved and accelerated due to gravity and how double buffer is used to eliminate flicker.

The assignment has given us a wonderful opportunity to dig into Java programming and also to have much fun time on the process of developing a computer game.

Contributions

Graphics designs and report writing: Anthony, Chan Ho Kwing 96116563

Programming and report writing: Dennis, Yu Hon Fai 96096615

Information gathering, interface design and report writing: Marius, Yung Ka Ho 96103600

File Listing

Classes

Note: The classes with indents are subclasses of the class above it.

Vector2D.java --- The maths class use to manipulate vector

Collider.java --- The super class of all classes whose instances will collide with the PinBall

·  DownBound.java --- prevents the PinBall from moving down from it.

·  Paddle.java --- paddles

·  BoundBox.java --- Rectangular region that the pinball can NOT penetrate

·  Blob.java -- The circular Blob

o  BigBlob.java --- The Big Circular Blob

Background.java --- Super class of all Background

·  StarBackground.java --- Flying through space background

·  ScrollBackground.java --- Scrolling Background

Sprite.java --- The super class of all sprite classes that represent the sprite used in the game

·  Banner.java --- The Banner used to display the score and time played

PinBall.java --- The pinball class

World.java --- Defining the gravity and provide utility functions to load images

Star.java --- The star use in the background

MMAnimation.java --- The classes used to perform the animation in the main menu

ScoreBox.java --- score that animates once the pinball hit the blob

Launcher.java --- Launcher of the pinball

RollButton.java --- The rollover button

Interfaces

Loopable.java -- The Loopable interface

Page 10 of 13