diff --git a/src/main/java/com/example/tankbattle/Bullet.java b/src/main/java/com/example/tankbattle/Bullet.java index 1b42773..77ffab4 100644 --- a/src/main/java/com/example/tankbattle/Bullet.java +++ b/src/main/java/com/example/tankbattle/Bullet.java @@ -1,6 +1,7 @@ package com.example.tankbattle; -import java.awt.*; +import java.awt.Graphics; +import java.awt.Rectangle; public class Bullet { private static final int SPEED = 10; @@ -11,7 +12,7 @@ public class Bullet { private boolean living = true; - private TankFrame tf; + private TankFrame tf = null; private Group group = Group.BAD; @@ -19,8 +20,8 @@ public class Bullet { this.x = x; this.y = y; this.dir = dir; - this.tf = tf; this.group = group; + this.tf = tf; } public void paint(Graphics g) { @@ -48,7 +49,6 @@ public class Bullet { } private void move() { - if (!living) return; switch (dir) { case LEFT: x -= SPEED; diff --git a/src/main/java/com/example/tankbattle/Tank.java b/src/main/java/com/example/tankbattle/Tank.java index 4d503b0..bf9136e 100644 --- a/src/main/java/com/example/tankbattle/Tank.java +++ b/src/main/java/com/example/tankbattle/Tank.java @@ -4,7 +4,7 @@ import java.awt.Graphics; import java.util.Random; 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 HEIGHT = ResourceMgr.tankD.getHeight(); @@ -15,7 +15,7 @@ public class Tank { private Dir dir = Dir.DOWN; - private boolean moving = false; + private boolean moving = true; private TankFrame tf = null; @@ -23,6 +23,25 @@ public class Tank { 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() { return x; } @@ -55,24 +74,10 @@ public class Tank { this.moving = moving; } - public Dir getDir() { - return dir; - } - public void setDir(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) { if (!living) tf.tanks.remove(this); switch (dir) { @@ -116,12 +121,6 @@ public class Tank { 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() { this.living = false; } diff --git a/src/main/java/com/example/tankbattle/TankFrame.java b/src/main/java/com/example/tankbattle/TankFrame.java index de668af..2587338 100644 --- a/src/main/java/com/example/tankbattle/TankFrame.java +++ b/src/main/java/com/example/tankbattle/TankFrame.java @@ -15,8 +15,8 @@ public class TankFrame extends Frame { Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); - List tanks = new ArrayList<>(); List bullets = new ArrayList<>(); + List tanks = new ArrayList<>(); public static final int GAME_WIDTH = 800,GAME_HEIGHT=600; public TankFrame() { @@ -66,9 +66,8 @@ public class TankFrame extends Frame { } 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)); - } } // for (Iterator it = bullets.iterator(); it.hasNext()) { // Bullet b = it.next();