GAMES TWO: LESSON FIVE: Explosion Upon Impact
What we need is a good, old-fashioned explosion to add color and excitement
to our game. In this upgrade of the FLY applet, we actually get to see an
explosion whenever the plane collides with the balloon or the mountains. (It
may take a few collisions, however, before the explosion frames are all
loaded... depending on your processor speed.)
You may notice that the explosion doesn't happen just exactly where you
expect it to occur. This slight defect is something that you can easily fix
for yourself when you do the assignment (described below).
- FLY3.java
- Tools2D.java
- Point2D.java
In order to produce an explosion we must first load the graphics used for
the explosion animation. This is easily accomplished in a loop:
e=new Image[12];
for(int i=0; i<11; i++){
e[i]=getImage(getDocumentBase(), "cel"+(i+1)+".gif");
}
To accomodate the explosion the following lines are added to the reset
method (it is assumed that the necessary variables have already been
declared globally):
public void reset(){
x_explode = x_plane_body[0];
y_explode = y_plane_body[0];
for(int i = 0; i
Also the run method must be altered to accomodate two conditions: 1)
exploding and 2) not exploding.
public void run(){
while(true){
repaint();
if(!explode_now){
//draw the planes and balloon, etc
}
else{
explode_cnt++;
if(explode_cnt>10){
explode_now=false;
speed=80;
}
}
Finally, the paint method must also be modified to draw explosions at the
right time (you should inspect the paint method).
ASSIGNMENT: