From 21229d3a8d74d4726794d81baa747537d041663f Mon Sep 17 00:00:00 2001
From: zhanxinlin <2452913979@qq.com>
Date: Wed, 28 Sep 2022 22:38:21 +0800
Subject: [PATCH] first commit
---
tankWar/.idea/.gitignore | 0
tankWar/.idea/misc.xml | 9 ++
tankWar/.idea/modules.xml | 8 ++
tankWar/.idea/vcs.xml | 6 ++
tankWar/.idea/workspace.xml | 110 ++++++++++++++++++++++++
tankWar/src/Bullet.java | 61 ++++++++++++++
tankWar/src/Direction.java | 8 ++
tankWar/src/FrameTank.java | 161 ++++++++++++++++++++++++++++++++++++
tankWar/src/Main.java | 15 ++++
tankWar/src/Tank.java | 86 +++++++++++++++++++
tankWar/tankWar.iml | 11 +++
11 files changed, 475 insertions(+)
create mode 100644 tankWar/.idea/.gitignore
create mode 100644 tankWar/.idea/misc.xml
create mode 100644 tankWar/.idea/modules.xml
create mode 100644 tankWar/.idea/vcs.xml
create mode 100644 tankWar/.idea/workspace.xml
create mode 100644 tankWar/src/Bullet.java
create mode 100644 tankWar/src/Direction.java
create mode 100644 tankWar/src/FrameTank.java
create mode 100644 tankWar/src/Main.java
create mode 100644 tankWar/src/Tank.java
create mode 100644 tankWar/tankWar.iml
diff --git a/tankWar/.idea/.gitignore b/tankWar/.idea/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/tankWar/.idea/misc.xml b/tankWar/.idea/misc.xml
new file mode 100644
index 0000000..fe90235
--- /dev/null
+++ b/tankWar/.idea/misc.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tankWar/.idea/modules.xml b/tankWar/.idea/modules.xml
new file mode 100644
index 0000000..05423c6
--- /dev/null
+++ b/tankWar/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tankWar/.idea/vcs.xml b/tankWar/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/tankWar/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tankWar/.idea/workspace.xml b/tankWar/.idea/workspace.xml
new file mode 100644
index 0000000..cf65ef4
--- /dev/null
+++ b/tankWar/.idea/workspace.xml
@@ -0,0 +1,110 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1664294698795
+
+
+ 1664294698795
+
+
+
+
+ 1664294827676
+
+
+
+ 1664294827676
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tankWar/src/Bullet.java b/tankWar/src/Bullet.java
new file mode 100644
index 0000000..a70dc12
--- /dev/null
+++ b/tankWar/src/Bullet.java
@@ -0,0 +1,61 @@
+import java.awt.*;
+
+/**
+ * @Description: 子弹
+ * @Author: zhanxinlin
+ * @Date: 2022-09-27 23:10
+ */
+public class Bullet {
+ private static final int SPEED = 10;
+ private static final int WIDTH = 20;
+ private static final int HEIGHT = 20;
+
+ private int x;
+ private int y;
+ private FrameTank frameTank;
+ private Direction direction;
+ private boolean live = true;
+
+ public Bullet(int x, int y, Direction direction, FrameTank frameTank) {
+ this.x = x;
+ this.y = y;
+ this.direction = direction;
+ this.frameTank = frameTank;
+ }
+
+ public void paint(Graphics g) {
+ // 子弹超出界限,从子弹集合中移除
+ if (!live) {
+ frameTank.bulletList.remove(this);
+ }
+
+ Color color = g.getColor();
+ g.setColor(Color.RED);
+ g.fillOval(x, y, WIDTH, HEIGHT);
+ g.setColor(color);
+ move();
+ }
+
+ private void move() {
+ // 定义坦克的移动特征
+ switch (direction) {
+ case LEFT:
+ x -= SPEED;
+ break;
+ case UP:
+ y -= SPEED;
+ break;
+ case RIGHT:
+ x += SPEED;
+ break;
+ case DOWN:
+ y += SPEED;
+ break;
+ }
+
+ // 子弹超出临界范围
+ if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
+ live = false;
+ }
+ }
+}
diff --git a/tankWar/src/Direction.java b/tankWar/src/Direction.java
new file mode 100644
index 0000000..36176b0
--- /dev/null
+++ b/tankWar/src/Direction.java
@@ -0,0 +1,8 @@
+/**
+ * @Description: 方向枚举
+ * @Author: zhanxinlin
+ * @Date: 2022-09-27 22:21
+ */
+public enum Direction {
+ LEFT, UP, RIGHT, DOWN
+}
diff --git a/tankWar/src/FrameTank.java b/tankWar/src/FrameTank.java
new file mode 100644
index 0000000..ca5a7aa
--- /dev/null
+++ b/tankWar/src/FrameTank.java
@@ -0,0 +1,161 @@
+import java.awt.*;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Description: 坦克大战窗口
+ * @Author: zhanxinlin
+ * @Date: 2022-09-27 21:34
+ */
+public class FrameTank extends Frame{
+ static final int GAME_WIDTH = 800;
+ static final int GAME_HEIGHT = 600;
+
+ Tank myTank = new Tank(200 ,200, Direction.DOWN, this);
+ List bulletList = new ArrayList<>(); // 用集合来装子弹
+
+ // 构造方法
+ public FrameTank() {
+ this.setSize(GAME_WIDTH, GAME_HEIGHT);
+ this.setResizable(false); // 窗口大小不可拉伸
+ this.setVisible(true); // 窗口可见
+ this.setTitle("tank war");
+
+ // 窗口监听器,点击"×",则关闭窗口,停止程序
+ this.addWindowListener(new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ System.exit(0);
+ }
+ });
+
+ // 键盘按钮监听器
+ this.addKeyListener(new MyKeyListener());
+ }
+
+ Image offScreenImage = null;
+
+ /**
+ * 双缓冲解决窗口闪烁
+ *
+ * @param g 画笔
+ */
+ @Override
+ public void update(Graphics g) {
+ if (offScreenImage == null) {
+ offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
+ }
+ Graphics gOffScreen = offScreenImage.getGraphics();
+ Color c = gOffScreen.getColor();
+ gOffScreen.setColor(Color.BLACK);
+ gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
+ gOffScreen.setColor(c);
+ paint(gOffScreen);
+ g.drawImage(offScreenImage, 0, 0, null);
+ }
+
+ /**
+ * 画图方法,系统调用
+ *
+ * @param g
+ */
+ @Override
+ public void paint(Graphics g) {
+ Color color = g.getColor();
+ g.setColor(Color.WHITE);
+ g.drawString("子弹的数量:" + bulletList.size(), 10, 60);
+ g.setColor(color);
+
+ // 坦克把自己画出来
+ myTank.paint(g);
+
+ // 子弹把自己画出来
+ for (int i = 0; i < bulletList.size(); i++) {
+ bulletList.get(i).paint(g);
+ }
+ }
+
+ class MyKeyListener extends KeyAdapter {
+ boolean leftDirection = false;
+ boolean upDirection = false;
+ boolean rightDirection = false;
+ boolean downDirection = false;
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ int key = e.getKeyCode();
+ switch (key) {
+ case KeyEvent.VK_LEFT:
+ leftDirection = true;
+ break;
+ case KeyEvent.VK_UP:
+ upDirection = true;
+ break;
+ case KeyEvent.VK_RIGHT:
+ rightDirection = true;
+ break;
+ case KeyEvent.VK_DOWN:
+ downDirection = true;
+ break;
+ case KeyEvent.VK_CONTROL:
+ myTank.fire();
+ break;
+ default:
+ break;
+ }
+
+ setMainTankDirection();
+ }
+
+ @Override
+ public void keyReleased(KeyEvent e) {
+ int key = e.getKeyCode();
+ switch (key) {
+ case KeyEvent.VK_LEFT:
+ leftDirection = false;
+ break;
+ case KeyEvent.VK_UP:
+ upDirection = false;
+ break;
+ case KeyEvent.VK_RIGHT:
+ rightDirection = false;
+ break;
+ case KeyEvent.VK_DOWN:
+ downDirection = false;
+ break;
+ default:
+ break;
+ }
+
+ setMainTankDirection();
+ }
+
+ /**
+ * 改变坦克的方向
+ */
+ private void setMainTankDirection() {
+ if (!leftDirection && !upDirection && !downDirection && !rightDirection) { // 没有按方向键,静止不动
+ myTank.setMoving(false);
+ } else { // 按了方向键
+ myTank.setMoving(true);
+
+ if (leftDirection) {
+ myTank.setDirection(Direction.LEFT);
+ }
+ if (rightDirection) {
+ myTank.setDirection(Direction.RIGHT);
+ }
+ if (upDirection) {
+ myTank.setDirection(Direction.UP);
+ }
+ if (downDirection) {
+ myTank.setDirection(Direction.DOWN);
+ }
+ }
+ }
+ }
+}
diff --git a/tankWar/src/Main.java b/tankWar/src/Main.java
new file mode 100644
index 0000000..dcc431c
--- /dev/null
+++ b/tankWar/src/Main.java
@@ -0,0 +1,15 @@
+/**
+ * @Description: 程序启动入口
+ * @Author: zhanxinlin
+ * @Date: 2022-09-27 22:00
+ */
+public class Main {
+ public static void main(String[] args) throws InterruptedException {
+ FrameTank frameTank = new FrameTank();
+
+ while (true) {
+ Thread.sleep(50);
+ frameTank.repaint(); // 重画窗口,重新调用paint方法
+ }
+ }
+}
diff --git a/tankWar/src/Tank.java b/tankWar/src/Tank.java
new file mode 100644
index 0000000..a53947e
--- /dev/null
+++ b/tankWar/src/Tank.java
@@ -0,0 +1,86 @@
+import java.awt.*;
+
+/**
+ * @Description: 坦克类
+ * @Author: zhanxinlin
+ * @Date: 2022-09-27 22:49
+ */
+public class Tank {
+ private static final int SPEED = 5;
+ private int x;
+ private int y;
+ private Direction direction;
+ private boolean moving = false;
+ private FrameTank frameTank = null;
+
+ public Tank(int x, int y, Direction direction, FrameTank frameTank) {
+ this.x = x;
+ this.y = y;
+ this.direction = direction;
+ this.frameTank = frameTank;
+ }
+
+ /**
+ * tank把自己画出来
+ *
+ * @param g 画笔
+ */
+ public void paint(Graphics g) {
+ Color color = g.getColor();
+ g.setColor(Color.GREEN);
+ g.fillRect(x, y, 50, 50);
+ g.setColor(color);
+ move();
+ }
+
+ /**
+ * 坦克移动方法
+ */
+ private void move() {
+ if (!moving) {
+ return;
+ }
+
+ // 定义坦克的移动特征
+ switch (direction) {
+ case LEFT:
+ x -= SPEED;
+ break;
+ case UP:
+ y -= SPEED;
+ break;
+ case RIGHT:
+ x += SPEED;
+ break;
+ case DOWN:
+ y += SPEED;
+ break;
+ }
+ }
+
+ /**
+ * 坦克开火方法
+ * 难点:如何将坦克对象new出来的子弹对象bullet传递到FrameTank对象的子弹对象bullet中
+ * 解决:坦克对象要持有FrameTank对象的引用
+ */
+ public void fire() {
+ // 将子弹加入子弹集合
+ frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank));
+ }
+
+ public Direction getDirection() {
+ return direction;
+ }
+
+ public void setDirection(Direction direction) {
+ this.direction = direction;
+ }
+
+ public boolean isMoving() {
+ return moving;
+ }
+
+ public void setMoving(boolean moving) {
+ this.moving = moving;
+ }
+}
diff --git a/tankWar/tankWar.iml b/tankWar/tankWar.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/tankWar/tankWar.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file