Compare commits

...

13 Commits

Author SHA1 Message Date
kn5886348135 90d7c933ba 代码格式
3 years ago
kn5886348135 b53ad2ca36 移动代码位置
3 years ago
kn5886348135 22a515741c 删除set方法
3 years ago
kn5886348135 3d66296c44 修改坦克速度
3 years ago
kn5886348135 6a9ab6b79c 坦克和子弹分组
3 years ago
kn5886348135 2f49ccba39 碰撞检测,敌方坦克被击中后消失
3 years ago
kn5886348135 41e9c91ac8 添加敌方坦克
3 years ago
kn5886348135 64198d3eeb 代码格式
3 years ago
kn5886348135 4dbbef3ac4 代码格式
3 years ago
kn5886348135 1c269829e6 子弹位置
3 years ago
kn5886348135 b7505d7bff 将子弹换成图片
3 years ago
kn5886348135 e02f4e2bb0 添加图片,渲染坦克
3 years ago
kn5886348135 4f99e1cbc1 删除子弹,避免内存泄漏
3 years ago

@ -1,25 +1,56 @@
package com.example.tankbattle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Bullet {
private static final int SPEED = 1;
private static final int WIDTH = 30, HEIGHT = 30;
private static final int SPEED = 10;
public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight();
private int x, y;
private Dir dir;
public Bullet(int x, int y, Dir dir) {
private boolean living = true;
TankFrame tf = null;
private Group group = Group.BAD;
public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tf = tf;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
if (!living) {
tf.bullets.remove(this);
}
switch (dir) {
case LEFT:
g.drawImage(ResourceMgr.bulletL, x, y, null);
break;
case UP:
g.drawImage(ResourceMgr.bulletU, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceMgr.bulletR, x, y, null);
break;
case DOWN:
g.drawImage(ResourceMgr.bulletD, x, y, null);
break;
default:
break;
}
move();
}
@ -41,5 +72,21 @@ public class Bullet {
default:
break;
}
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
}
public void collideWith(Tank tank){
if (this.group == tank.getGroup()) return;
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if (rect1.intersects(rect2)) {
tank.die();
this.die();
}
}
private void die() {
this.living = false;
}
}

@ -0,0 +1,5 @@
package com.example.tankbattle;
public enum Group {
GOOD, BAD
}

@ -3,6 +3,12 @@ package com.example.tankbattle;
public class Main {
public static void main(String[] args) throws InterruptedException {
TankFrame tf = new TankFrame();
// 初始化敌方坦克
for (int i = 0; i < 5; i++) {
tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf));
}
while (true) {
Thread.sleep(50);
tf.repaint();

@ -0,0 +1,26 @@
package com.example.tankbattle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ResourceMgr {
public static BufferedImage tankL, tankU, tankR, tankD;
public static BufferedImage bulletL, bulletU,bulletR, bulletD;
static {
try {
tankL = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankL.gif"));
tankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankU.gif"));
tankR = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankR.gif"));
tankD = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankD.gif"));
bulletL = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletL.gif"));
bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.gif"));
bulletR = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletR.gif"));
bulletD = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletD.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -1,49 +1,65 @@
package com.example.tankbattle;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Tank {
private static final int SPEED = 1;
public static int WIDTH = ResourceMgr.tankD.getWidth();
public static int HEIGHT = ResourceMgr.tankD.getHeight();
private Random random = new Random();
private int x,y;
private Dir dir = Dir.DOWN;
private static final int SPEED = 5;
private boolean moving = false;
private boolean moving = true;
private TankFrame tf = null;
public boolean isMoving() {
return moving;
private boolean living = true;
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.group = group;
this.tf = tf;
}
public void setMoving(boolean moving) {
this.moving = moving;
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 void setDir(Dir dir) {
this.dir = dir;
public int getX() {
return x;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Tank(int x, int y, Dir dir, TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.tf = tf;
public int getY() {
return y;
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, 50, 50);
g.setColor(c);
move();
public boolean isMoving() {
return moving;
}
private void move() {
@ -64,9 +80,48 @@ public class Tank {
default:
break;
}
if (random.nextInt(10) > 5) this.fire();
}
public void fire(){
tf.bullets.add(new Bullet(this.x, this.y, this.dir));
public void paint(Graphics g) {
if (!living) tf.tanks.remove(this);
switch (dir) {
case LEFT:
g.drawImage(ResourceMgr.tankL, x, y, null);
break;
case UP:
g.drawImage(ResourceMgr.tankU, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceMgr.tankR, x, y, null);
break;
case DOWN:
g.drawImage(ResourceMgr.tankD, x, y, null);
break;
default:
break;
}
move();
}
public void setDir(Dir dir) {
this.dir = dir;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void die() {
this.living = false;
}
}

@ -13,13 +13,13 @@ import java.util.List;
public class TankFrame extends Frame {
Tank myTank = new Tank(200, 200, Dir.DOWN, this);
Bullet b = new Bullet(300, 300, Dir.DOWN);
private static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>();
static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
public TankFrame() {
public TankFrame() {
setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false);
setTitle("tank battle");
@ -50,11 +50,29 @@ public class TankFrame extends Frame {
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克的数量:" + tanks.size(), 10, 60);
g.setColor(c);
myTank.paint(g);
b.paint(g);
for (Bullet bullet1 : bullets) {
bullet1.paint(g);
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i = 0; i < bullets.size(); i++) {
for (int j = 0; j < tanks.size(); j++)
bullets.get(i).collideWith(tanks.get(j));
}
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next();
// if (!b.live) it.remove();
// }
}
@ -115,6 +133,7 @@ public class TankFrame extends Frame {
}
private void setMainTankDir() {
if (!bL && !bU && !bR && !bD) {
myTank.setMoving(false);
} else {

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1022 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 864 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

@ -0,0 +1,29 @@
package com.example.tankbattle;
import org.junit.jupiter.api.Test;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
public class ImageTest {
@Test
public void test(){
System.out.println(ImageTest.class.getClassLoader());
try {
BufferedImage image = ImageIO.read(new File("C:/work/javaprojects/Tank_60/src/images/bulletD.gif"));
assertNotNull(image);
BufferedImage image2 = ImageIO.read(ImageTest.class.getClassLoader().getResourceAsStream("src/images" +
"/bulletD.gif"));
assertNotNull(image2);
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
Loading…
Cancel
Save