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).

  1. FLY3.java
  2. Tools2D.java
  3. 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<x_reset_body.length; i++){ x_plane_body[i] = x_reset_body[i]; y_plane_body[i] = y_reset_body[i]; } for(int i = 0; i<x_reset_cockpit.length; i++){ x_cockpit[i] = x_reset_cockpit[i]; y_cockpit[i] = y_reset_cockpit[i]; } for(int i = 0; i<r_ball.length; i++){ x_ball[i] = r_ball[i]; } for(int i = 0; i<r_basket.length; i++){ x_basket[i] = r_basket[i]; } crashes++; explode_now=true; explode_cnt = 0; speed=80; }
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: