修改坦克速度

master
kn5886348135 3 years ago
parent 6a9ab6b79c
commit 3d66296c44

@ -1,6 +1,7 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.*; import java.awt.Graphics;
import java.awt.Rectangle;
public class Bullet { public class Bullet {
private static final int SPEED = 10; private static final int SPEED = 10;
@ -11,7 +12,7 @@ public class Bullet {
private boolean living = true; private boolean living = true;
private TankFrame tf; private TankFrame tf = null;
private Group group = Group.BAD; private Group group = Group.BAD;
@ -19,8 +20,8 @@ public class Bullet {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tf = tf;
this.group = group; this.group = group;
this.tf = tf;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -48,7 +49,6 @@ public class Bullet {
} }
private void move() { private void move() {
if (!living) return;
switch (dir) { switch (dir) {
case LEFT: case LEFT:
x -= SPEED; x -= SPEED;

@ -4,7 +4,7 @@ import java.awt.Graphics;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank {
private static final int SPEED = 5; private static final int SPEED = 1;
public static int WIDTH = ResourceMgr.tankD.getWidth(); public static int WIDTH = ResourceMgr.tankD.getWidth();
public static int HEIGHT = ResourceMgr.tankD.getHeight(); public static int HEIGHT = ResourceMgr.tankD.getHeight();
@ -15,7 +15,7 @@ public class Tank {
private Dir dir = Dir.DOWN; private Dir dir = Dir.DOWN;
private boolean moving = false; private boolean moving = true;
private TankFrame tf = null; private TankFrame tf = null;
@ -23,6 +23,25 @@ public class Tank {
private Group group = Group.BAD; private Group group = Group.BAD;
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.tf = tf;
this.group = group;
}
public void fire(){
int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
}
public Dir getDir() {
return dir;
}
public int getX() { public int getX() {
return x; return x;
} }
@ -55,24 +74,10 @@ public class Tank {
this.moving = moving; this.moving = moving;
} }
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) { public void setDir(Dir dir) {
this.dir = dir; this.dir = dir;
} }
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.tf = tf;
this.group = group;
}
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) tf.tanks.remove(this); if (!living) tf.tanks.remove(this);
switch (dir) { switch (dir) {
@ -116,12 +121,6 @@ public class Tank {
if (random.nextInt(10) > 5) this.fire(); if (random.nextInt(10) > 5) this.fire();
} }
public void fire(){
int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
}
public void die() { public void die() {
this.living = false; this.living = false;
} }

@ -15,8 +15,8 @@ public class TankFrame extends Frame {
Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);
List<Tank> tanks = new ArrayList<>();
List<Bullet> bullets = new ArrayList<>(); List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>();
public static final int GAME_WIDTH = 800,GAME_HEIGHT=600; public static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
public TankFrame() { public TankFrame() {
@ -66,9 +66,8 @@ public class TankFrame extends Frame {
} }
for (int i = 0; i < bullets.size(); i++) { for (int i = 0; i < bullets.size(); i++) {
for (int j = 0; j < tanks.size(); j++) { for (int j = 0; j < tanks.size(); j++)
bullets.get(i).collideWith(tanks.get(j)); bullets.get(i).collideWith(tanks.get(j));
}
} }
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) { // for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next(); // Bullet b = it.next();

Loading…
Cancel
Save