The realization of explosion effect
When the plane was hit by a cannonball, an explosive effect was needed to make our picture more exciting. The implementation of explosion effects is also common in game development.
We define the Exlode class to represent the explosion of information, and the explosion class is different from the normal class in that he actually stores a series of exploded images and then rounds them. Finally, what we see is a cool set of effects.
Here we have a series of exploded images:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9D/E0/wKioL1mIDPjgncMdAAF76XnM1CQ351.png "title=" Figure 1.png "alt=" Wkiol1midpjgncmdaaf76xnm1cq351.png "/>
A small fireball from the blast to the fire ball, then to the vanishing fireball. Exploding objects just take turns loading the pictures.
We copy these images to the project, create a new: Images/explode folder, and copy 16 pictures to the folder below.
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/E0/wKiom1mIDOqiAPX2AAH4NTWcDKs738.png "title=" Figure 2.png "alt=" Wkiom1midoqiapx2aah4ntwcdks738.png "/>
Basic design of explosion class
"Example 1" explosion class explode
Package cn.sxt.game;
Import Java.awt.Graphics;
Import Java.awt.Image;
/*
* Explosion class
*/
public class Explode {
Double x, y;
Static image[] IMGs = new IMAGE[16];
static {
for (int i=0;i<16;i++) {
Imgs[i] = gameutil.getimage ("images/explode/e" + (i+1) + ". gif");
Imgs[i].getwidth (NULL);
}
}
int count;
public void Draw (Graphics g) {
if (count<=15) {
G.drawimage (Imgs[count], (int) x, (int) y, null);
count++;
}
}
Public Explode (double x,double y) {
this.x = x;
This.y = y;
}
}
We define image[] to save the picture information, and use the static block of code, that is, when the class loads the pictures are loaded, and from the class, do not need to create an exploded object every time loading pictures, to ensure the efficiency of the operation.
By counting the counter count to control which picture to draw, due to our image naming is very canonical, is in order from 1-16, so that the program read these image objects in turn.
Main window class creating exploded objects
If you want to show an exploded object, we still need to define the exploded object in the main window, and create an exploded object at the aircraft coordinates when the aircraft and shells collide, showing the explosion effect.
"Example 2" Mygameframe: Increase the explosion effect
public class Mygameframe extends Frame {
Image bgimg = gameutil.getimage ("images/bg.jpg");
Image planeimg = gameutil.getimage ("Images/plane.png");
Plane Plane = new Plane (planeimg,300,300,3);
arraylist<shell> shelllist = new arraylist<shell> ();
Explode bao;//Creating an Exploded object
The Paint method works by drawing the entire window and internal content. Automatically called by the system.
@Override
public void Paint (Graphics g) {
G.drawimage (bgimg, 0, 0, NULL);
Plane.drawmyself (g); Draw the plane itself
Draw all the bullets in the container
for (int i=0;i<shelllist.size (); i++) {
Shell B = Shelllist.get (i);
B.draw (g);
Rectangular inspection of aircraft and all projectiles
Boolean peng = B.getrect (). intersects (Plane.getrect ());
if (Peng) {
Plane.live = false; The plane dies, the screen doesn't show.
if (bao==null) {
Bao = new Explode (PLANE.X,PLANE.Y);
}
Bao.draw (g);
}
}
}
The remaining code is consistent with the previous version and is limited to space, no longer showing
}
The results of the program execution, when the aircraft and shells collide when the explosion occurred, 3:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/E0/wKiom1mIDWbSDXZJAAFP_4fLtjI222.png "title=" Figure 3.png "alt=" Wkiom1midwbsdxzjaafp_4fltji222.png "/>
"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.
The notes contain six parts from shallow into deep:
A-java Introductory Phase
B-Database from beginner to proficient
C-hand Edge mobile front end and web front end
D-j2ee from understanding to combat
E-java Advanced Frame Fine Solution
F-linux and Hadoop
This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1954200
13.8-Full stack Java notes: Flying Game Project | explode| mygameframe| Plane