|
RHS |
BallGame 04
Now it's time to refactor a bit.
Use Sprite superclass
All the moving objects we have - spaceship, bullet and ball (or grenade) - hold a lot of code that has to do with the fact that the classes are in fact sprites. As reuse is a good idea you should change the three classes so they extend the same superclass (maybe even implement the same interface...).
You can download the Sprite.java from the textbook here: Sprite.java
Either use it with a few adaptions or use it for inspiration for your own super-class.
Explosions
As a Sprite hold its own ImagesLoader and ImagesPlayer it is now rather easy to make it show an explosion.
Here's an image that holds 5 parts that, if they are played in an ImagesPlayer, gives the illusion of an explosion (use the files from BallGame03 to play the sound of an explosion) : explosion.gif
Adapt your game so the ball (grenade) explodes if you hit it with a bullet. After the explosion the ball/grenade and bullet should be gone. Then the ball/grenade should re-appear at the top of the frame - maybe with new speed.
Also adapt your game so the spaceship and ball/grenade explodes if they collide. After that the game should end (you lost).
Background
Using the technique from Chapter 6.2 you should now add
a moving background that gives the illusion of outer space.
One difference from Chapter 6.2 is that we only use one ribbon, another difference is that our ribbon never changes direction and that it moves vertically instead or horizontally.
Here's the Ribbon from the textbook - you'll need to adapt it a bit : Ribbon.java
And here's an image you can use as background (it's not perfect - but useful) : backgr1.gif