J2ME 2D Game Getting Started Tour (IV) Add to Bullet Function and Implement Collision

xiaoxiao2021-03-06  111

The bullets in the aircraft game are essential, and they are much more and full of screens, these random or small objects with a certain AI, which is not always that is so easy, sometimes you have to consider a lot of effectiveness. We have defined GameObject before, to a large extent, for convenience of retrovating Sprite, because we have a lot of bullets, it is impossible to add a bullet, which is a sprite, I need to share the same sprite. We are implemented by inheriting GameObject.

Let's analyze this bullet:

It will inherit from GameObject;

Record the number of bullets;

An array of states of a bullet, record the type TYPE, position X, Y, speed Vx, Vy, whether Alive, etc.

Initialized bullet

A drawing method draws the bullets to the screen.

A collision detection method.

Ok, first this, the following is the definition of our bullets, pay attention to this idea - reuse Sprite, this is important. (Here, refer to Tony's design)

Public class bullets extends GameObject {

Private int [] [] bullets; // bullet status array

PRIVATE INT BULLETSTOL; / / Array LENGTH

Private random rnd; // random number

Public static final int bullet_type_left = 0; // Bullet initialization position type

Public static final int bullet_type_right = 1; // is divided into four left and right

Public static final int bullet_type_top = 2;

Public static final int bullet_type_bottom = 3;

Private int width, height; // High and wide screen for random bullets

Public Bullets (Image Img, Int Picwidth, Int Picheight, Int BulletStotal, Int Width, INT Height) {

Super (IMG, PicWidth, Picheight);

THIS.BULLETSTOAL = BulletStotal;

Bullets = new int [bulletstotal] [6];

RND = new random ();

THIS.WIDTH = Width;

THISHEIGHT = HEIGHT;

}

Public void initbullets () {// Initialized bullet status array

For (int i = 0; i

INITBULLET (i);

}

}

Private void Initbullet (INT i) {// Initializing the INDEX number bullet

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

Bullets [i] [5] = 1; // alive 1 indicates survival, 0 means death

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 () & 0x7ffffff)% 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 the position of I bullets according to speed, hit the wall 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) {// Painting Item I

UpdataaspritePos (i); // Update location

Sprite.paint (g); // Painting SPRTIE

}

Public void Paint (Graphics G) {// Painting The entire bullet group

For (int i = 0; i

IF (Bullets [i] [5] == 0) {// Dirty bullet does not paint

CONTINUE;

}

Sprite.SetPosition (Bullets [i] [1], Bullets [i] [2]); // Update location

Sprite.paint (g);

}

}

Public Void Refreshbullts (Sprite PlaneSprite, Boolean NeedCollision) {// Refresh the status of the dictionary array, and crash

For (int i = 0; i

IF (bullets [i] [5] == 0) {// dead bullets are not updated

CONTINUE;

}

IF (NeedCollision) {// If you need collision detection

IF (iScollision (PlaneSprite, I, 10)) {// If you collide, proceed

//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; // kill the bullets

CONTINUE;

}

}

Updata (i); // update status

}

}

Private Boolean Iscollision (Sprite Sprite, Int I, INT RANGE) {

/ / Judgment whether collision

// UpdataaspritePos (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

IF (Math.Abs ​​(Planecenter - BulletyCenter

Result = TRUE;

}

}

Return Result;

}

Private Void UpDataSpritePos (INT i) {// Location of Sprite to I-Character

Sprite.SetPosition (Bullets [i] [1], Bullets [i] [2]);

}

/ * No Uses

Public void resetdeadbullet () {

For (int i = 0; i

IF (Bullets [i] [5] == 0) {// dead bullet

INITBULLET (i);

}

}

}

* /

Public void killbullets (sprite planesprite, int Ran) {kill bullets in a certain area

For (int i = 0; i

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

IF (ISCOLLISION (PlaneSprite, I, Range) {

Bullets [i] [5] = 0;

INITBULLET (i);

}

}

}

}

}

How does the bullet say?

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

Bullets [i] [0] indicates the type of bullets, there are top, lower, left, and right, respectively indicate the four locations in front of the bullets.

Bullets [i] [1] indicates the X coordinate of the bullet;

Bullets [i] [2] indicates the Y coordinate of the bullet

Bullets [i] [3] indicates the X direction speed of the bullet;

Bullets [i] [4] represents the Y direction speed of the bullet;

Bullets [i] [5] indicates the survival status of the bullet;

How is the bullet initialized?

We first write a method of initializing a single bullet, and then make an array call initbullet (i); to update the entire status array.

How do bullets draw?

We first wrote a method of drawing a single bullet, and then convenient array calls Paint (g, i); to draw the entire status array.

How does the bullet collide?

There are many ways, where Sprite itself provides border collision detection and pixel-based collision detection. The former is not suitable for our game, our aircraft is an irregular object, and the flight game is sensitive to the collision; and the latter's efficiency is not trustworthy, so we use a radius to detect, watch the plane approximation Circular, select the appropriate radius, Math.Abs ​​(Planexcenter-Bulletxcenter

The collision seems simple, in fact, a very complicated issue, is worthy of fortunate, two-dimensional collision is much simpler than three-dimensional collision. A tip skill is that I would like to make the radius of expansion and don't be big - missed, and there is much better than the error detection.

Bullet update?

We use refreshbullets to update, which is the main logical part. This method is responsible for the convenience of array detection collision, if the collision will kill the bullets in the collision position, and make the corresponding processing, here is the game and exploding aircraft; otherwise update the position of the bullet.

We just linearly traverse the entire array of collision, which is the update location; however, there is a premise that the collision detection is simple and the processing part is also very simple: in this game, collision detection is only the detection of bullet groups and aircraft The collision detection is not performed after the game is over (by controlling Boolean NeedCollision); handling is simpler - directly over the game. If this is not the case, if it is not a simple end game after processing, we have to design more complex. It may not be a simplicity of the crash as a center. We need to design a game event and design a collision system.

If the collision itself is more complicated, or when the number of bullets, the type of variety increases, our linear traversal array cannot always detect all bullets, and the screen is required to be partitioned, and the unit does not detect in a region.

In short, when you think about you 1934, you will not imagine bullets, what is the plane, you have to think about a system.

Summarize the public interface of the bullet:

N Bullets (Image IMG, INT Picwidth, Int Picheight, Int BulletStotal, Int Width, Int Height) constructor

N public void initbullets () initialized bullet array

N Public Void Paint (Graphics G) Paint Bullet Architecture

N Public Void Refreshbullets (Sprite Planesprite, Boolean NeedCollision) Update the integration of logical work such as bullet arrays, collision detection, processing

N public void killbullets (sprite planesprite, int ing) // later explained

So far, our game has been put into size, the next step is to join the effect class, 嘿嘿 a bit means ...

转载请注明原文地址:https://www.9cbs.com/read-98263.html

New Post(0)