Compare commits

...

2 Commits

Author SHA1 Message Date
kn5886348135 27b0107b7a 子弹和坦克各自维护Rectangle
3 years ago
kn5886348135 3131dbdfb5 边界检测
3 years ago

@ -7,6 +7,9 @@ public class Bullet {
private static final int SPEED = 6; private static final int SPEED = 6;
public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight(); public static int HEIGHT = ResourceMgr.bulletD.getHeight();
private Rectangle rect = new Rectangle();
private int x, y; private int x, y;
private Dir dir; private Dir dir;
@ -20,6 +23,11 @@ public class Bullet {
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tf = tf; this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = WIDTH;
rect.height = HEIGHT;
} }
public Group getGroup() { public Group getGroup() {
@ -73,14 +81,15 @@ public class Bullet {
break; break;
} }
rect.x = this.x;
rect.y = this.y;
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false; if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
} }
public void collideWith(Tank tank){ public void collideWith(Tank tank){
if (this.group == tank.getGroup()) return; if (this.group == tank.getGroup()) return;
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT); if (this.rect.intersects(tank.rect)) {
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if (rect1.intersects(rect2)) {
tank.die(); tank.die();
this.die(); this.die();
int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2; int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;

@ -1,6 +1,6 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.Graphics; import java.awt.*;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank {
@ -9,6 +9,8 @@ public class Tank {
public static int WIDTH = ResourceMgr.GoodTankD.getWidth(); public static int WIDTH = ResourceMgr.GoodTankD.getWidth();
public static int HEIGHT = ResourceMgr.GoodTankD.getHeight(); public static int HEIGHT = ResourceMgr.GoodTankD.getHeight();
Rectangle rect = new Rectangle();
private Random random = new Random(); private Random random = new Random();
private int x,y; private int x,y;
@ -30,6 +32,11 @@ public class Tank {
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tf = tf; this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = WIDTH;
rect.height = HEIGHT;
} }
public void fire(){ public void fire(){
@ -81,11 +88,31 @@ public class Tank {
break; break;
} }
rect.x = this.x;
rect.y = this.y;
if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire(); if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95) { if (this.group == Group.BAD && random.nextInt(100) > 95) {
randomDir(); randomDir();
} }
boundsCheck();
}
private void boundsCheck() {
if (this.x < 0) {
this.x = 2;
}
if (this.y < 28) {
this.y = 28;
}
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) {
this.x = TankFrame.GAME_WIDTH - Tank.WIDTH;
}
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) {
this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
}
} }
private void randomDir() { private void randomDir() {

Loading…
Cancel
Save