J2ME 2D Small Game Entry (iv) Join the bullets, realize the collision operation

Source: Internet
Author: User
Tags abs continue final join range
Computational aircraft games in the bullets are essential, they are numerous and full of the entire screen, these random or have a certain AI small objects, it is not always easy to achieve, sometimes you have to consider a lot of problems related to effectiveness. We defined the Gameobject, to a large extent, to facilitate the reuse of Sprite, because we have a lot of bullets, it is impossible not to add a bullet is a sprite, I need to share the same sprite. We implement it by inheriting Gameobject.



Here's an analysis of this bullet:

It will inherit from Gameobject;

Record the number of bullets;

An array of the states of a bullet, recording the type of each bullet, position x,y, speed vx,vy, survival alive, and so on.

Initialize Bullets

A drawing method that draws the bullet to the screen.

A collision detection method.



All right, here's the definition of our bullets, and note that this idea--reusing sprite--is important. (This is a reference to Tony's many designs)

public class Bullets extends Gameobject {

Private int[][] bullets;//Bullet State Array

Length of the private int bulletstotal;//array

Private Random rnd;//Random number

public static final int bullet_type_left=0;//Bullet initialization location type

public static final int bullet_type_right=1;//divided into about four kinds

public static final int bullet_type_top=2;

public static final int bullet_type_bottom=3;

private int width,height;//screen with high and wide, for random bullet position



Public Bullets (Image img,int picwidth,int picheight,int bulletstotal,int height) {

Super (Img,picwidth,picheight);

This.bulletstotal=bulletstotal;

Bullets=new Int[bulletstotal][6];

Rnd=new Random ();

This.width=width;

This.height=height;

}



public void Initbullets () {//Init Bullet State Array

for (int i = 0; i < bullets.length; i++) {

Initbullet (i);

}

}



private void Initbullet (int i) {//Initialize index bullet

Bullets[i][0] = (Rnd.nextint () & 0x7fffffff)% 4; Type

BULLETS[I][5] = 1; Alive 1 means surviving, and 0 means dead.

Switch (Bullets[i][0]) {

Case Bullet_type_left:

BULLETS[I][1] =-5;

BULLETS[I][2] = (Rnd.nextint () & 0x7fffffff)% height;

BULLETS[I][3] = (Rnd.nextint () & 0x7fffffff)% 3 + 1; Vx

BULLETS[I][4] = (rnd.nextint ())% 3; Vy

Break

Case Bullet_type_right:

BULLETS[I][1] = width + 5;

BULLETS[I][2] = (Rnd.nextint () & 0x7fffffff)% height;

BULLETS[I][3] = ((Rnd.nextint () & 0x7fffffff)% 3 + 1) *-1; Vx

BULLETS[I][4] = (rnd.nextint ())% 3; Vy

Break

Case Bullet_type_top:

BULLETS[I][1] = (Rnd.nextint () & 0x7fffffff)% width;

BULLETS[I][2] =-5;

BULLETS[I][3] = (rnd.nextint ())% 3; Vx

BULLETS[I][4] = (Rnd.nextint () & 0x7fffffff)% 3 + 1; Vy

Break

Case Bullet_type_bottom:

BULLETS[I][1] = (Rnd.nextint () & 0x7fffffff)% width;

BULLETS[I][2] = height + 5;

BULLETS[I][3] = (rnd.nextint ())% 3; Vx

BULLETS[I][4] = ((Rnd.nextint () & 0x7fffffff)% 3 + 1) *-1; Vy

Break

}

}



public void Updata (int i) {//update by speed I bullets under a frame of position, hit the rebound

BULLETS[I][1]+=BULLETS[I][3];

BULLETS[I][2]+=BULLETS[I][4];

if (bullets[i][1]<-5 | | bullets[i][1]>width+5) {

Bullets[i][3]*=-1;

}

if (bullets[i][2]<-5 | | bullets[i][2]>height+5) {

Bullets[i][4]*=-1;

}

}



private void Paint (Graphics g,int i) {//Draw the first bullet

Updataspritepos (i);//Update location

Sprite.paint (g);//Painting Sprtie

}



public void Paint (Graphics g) {//painting the entire bullet group

for (int i = 0; i < bullets.length; i++) {

if (bullets[i][5]==0) {//Dead bullets Don't paint

Continue

}

Sprite.setposition (bullets[i][1],bullets[i][2]); Update location

Sprite.paint (g);

}

}



public void Refreshbullets (Sprite planesprite, Boolean needcollision) {//Refresh the state of the dictionary array and make collision handling

for (int i = 0; i < bullets.length; i++) {

if (bullets[i][5]==0) {//Dead bullets are not updated

Continue

}

if (needcollision) {//If collision detection is required

if (Iscollision (Planesprite, I, 10)) {//if collision, handle

System.out.println ("Collision");

Navigate.mc.gameover = true;

Navigate.mc.explosion.sprite.setPosition (Bullets[i][1]-16,

BULLETS[I][2]-16);

BULLETS[I][5] = 0;//The bullet that killed the collision

Continue

}

}

Updata (i);//Update status

}

}



Private Boolean iscollision (Sprite sprite,int i,int range) {

To determine whether a collision

Updataspritepos (i);

Return Sprite.collideswith (this.sprite,true);

Boolean result=false;

int planexcenter=sprite.getx () +12;

int planeycenter=sprite.gety () +12;

int bulletxcenter=bullets[i][1]+3;

int bulletycenter=bullets[i][2]+3;

if (Math.Abs (Planexcenter-bulletxcenter) < range) {

if (Math.Abs (Planeycenter-bulletycenter) < range) {

result = true;

}

}

return result;

}



private void Updataspritepos (int i) {//update sprite to position of I bullet

Sprite.setposition (bullets[i][1],bullets[i][2]);

}



/* No use now

public void Resetdeadbullet () {

for (int i = 0; i < bullets.length; i++) {

if (bullets[i][5]==0) {//dead bullet

Initbullet (i);

}

}

}

*/



public void Killbullets (Sprite planesprite,int range) {kills bullets in a certain area

for (int i = 0; i < bullets.length; i++) {

if (bullets[i][5]!=0) {//alive bullets

if (Iscollision (Planesprite, I, range)) {

bullets[i][5]=0;

Initbullet (i);

}

}

}

}

}

How does the bullet represent?

First we use a two-dimensional array to record the bullet information:

Bullets[i][0] The type of bullet, there are up, down, left and right four, respectively, the bullet flew into the screen before the four kinds of positions;

BULLETS[I][1] represents the x-coordinate of a bullet;

Bullets[i][2] represents the y-coordinate of a bullet.

BULLETS[I][3] represents the X-directional velocity of a bullet;

Bullets[i][4] Indicates the y-direction velocity of the bullet;

BULLETS[I][5] Indicates the survival status of the bullet;



How does the bullet initialize?

We first write a method to initialize a single bullet, and then facilitate the array to invoke Initbullet (i), to update the entire state array.



How does a bullet draw?

We first wrote a method of drawing a single bullet, and then facilitated the array call Paint (g,i), to draw the entire state array.



How does a bullet collide?

There are a number of ways in which sprite itself provides border collision detection and pixel based collision detection. The former is not very suitable for our game, our aircraft is irregular objects, and the flight game is sensitive to collisions, and the latter is not the efficiency of our trust, so we are using a radius detection, the aircraft approximation as a circle, select the appropriate radius, Math.Abs ( Planexcenter-bulletxcenter) < range indicates collision.

The collision seems to be simple, in fact, is very complex problem, thankfully, two-dimensional collision is much simpler than three-dimensional collision. One trick is to make the expansion detection radius smaller and not to be big--the leak detection is better than false detection.



Bullets updated?

We use Refreshbullets to update, which is the main logical part. This method is responsible for facilitating the array detection collision, if the collision will be in the collision position of the bullet killed, and the corresponding treatment, here is the end of the game and explosion of the aircraft, otherwise update the position of the bullet.

We just linearly traverse the entire array, collision detection, followed by the update location; but there is a premise that collision detection is simple and the processing part is simple: In this game, collision detection is only the bullets and aircraft detection, collision detection at the end of the game is not executed (through the control Boolean Needcollision), and the process is simpler--it ends the game directly. If not, for example, after the process is not simply to end the game, we have to design a bit more complex. It might not be a simple plane-centric collision. We need to design good game events and design a collision system.

If the collision itself is more complex, or the number of bullets, the type of increase, our linear traversal of the array can not always detect all the bullets, the screen may need to partition, not in one area of the unit does not detect.

Anyway, when you think about your 1934, you're not going to be imagining bullets, airplanes or anything, you have to think about a system.

To sum up the public interface of the Bullet class:

N Bullets (Image img,int picwidth,int picheight,int bulletstotal,int height) constructor

n public void Initbullets () initialization Bullets Array

n public void Paint (Graphics g) Paint Bullets Array

n public void Refreshbullets (Sprite planesprite, boolean needcollision) update Bullet array state, collision detection, processing and other logic work synthesis

n public void Killbullets (Sprite planesprite,int Range)//later explained



So far, our game has begun to take shape, the next step is to add effect class, hey, a little meaning ...





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.