RHS   Allan   Game Techniques Exercises BallGame 01


 

BallGame 01

Now we'll make our small ball-experiment into a small BallGame.

Here you can download the basic game classes: Game.java, GamePanel.java
Their implementention is highly inspired by the WormChase game!

The ball should move automatically in both horizontal and vertical directions.
How much is determined by two variables in Ball.java - xStep and yStep. Their default value is 1, but thay can be changed using set-methods.

So GamePanel.gameUpdate(), which is called for each iteration in GamePanel.run(), moves the ball by activating the move(int, int) method for the ball object.
The move(int, int) method must take care of updating the position of the ball and make sure that the ball bounces if it hits any of the walls.
So it needs to know the maximum valid values for the coordinates - that's why two integers, representing the width and height, are passed to move().

Question 1
Download the ball: Ball.java and implement the move(int, int) method so the ball moves automatically for each iteration and bounces when it hits a wall.

Question 2
Change the code for GamePanel (and maybe Ball...) so the panel can be resized. The ball must use the full panel after a resize.

Question 3 (a little difficult...)
Create a class - SpaceShip - that draws a red triangel at the bottom of the panel (it's probably a good idea to switch back to the panel that can't be resized...)

Question 4 (difficult...)
Change the application so you can move the SpaceShip to the left and to the right (notup and down!) using the left- and right-arrow keys on the keyboard.


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04