From 10c7e9c5529912136d6c80cbe86000ab6c48ba0a Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Wed, 5 Oct 2022 22:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98(=E4=B8=80?= =?UTF-8?q?=E6=9C=9F)-=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/config.properties | 6 ++++++ src/com/msb/PropertyMgr.java | 33 +++++++++++++++++++++++++++++++++ src/com/msb/Tank.java | 8 ++++---- src/com/msb/TankDemo.java | 4 +++- src/com/msb/TankFrame.java | 4 ++-- 5 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 resources/config.properties create mode 100644 src/com/msb/PropertyMgr.java diff --git a/resources/config.properties b/resources/config.properties new file mode 100644 index 0000000..fb36f7e --- /dev/null +++ b/resources/config.properties @@ -0,0 +1,6 @@ +#tanks count at initialization +initTankCount=10 +tankSpeed=5 +bulletSpeed=10 +gameWidth=1080 +gameHeight=720 \ No newline at end of file diff --git a/src/com/msb/PropertyMgr.java b/src/com/msb/PropertyMgr.java new file mode 100644 index 0000000..0570e0f --- /dev/null +++ b/src/com/msb/PropertyMgr.java @@ -0,0 +1,33 @@ +package com.msb; + +import java.io.IOException; +import java.util.Objects; +import java.util.Properties; + +/** + * @Author bingor + * @Date 2022-10-05 22:35 + * @Description: com.msb + * @Version: 1.0 + */ +public class PropertyMgr { + + private static Properties props = new Properties(); + + static { + try { + props.load(PropertyMgr.class.getClassLoader().getResourceAsStream("config.properties")); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static Object get(String key) { + return props.get(key); + } + + public static void main(String[] args) { + System.out.println(get("initTankCount")); + } + +} diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 9904705..4b5dfd7 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -13,7 +13,7 @@ public class Tank { private int x,y; private DirEnum dir; - private static final int SPEED = 2; + private static final int SPEED = 4; private boolean move = true; //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 private TankFrame tankFrame; @@ -75,15 +75,15 @@ public class Tank { default: break; } - rectangle.x = this.x; - rectangle.y = this.y; - //敌方坦克随机发射炮弹 if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire(); //敌方坦克随机改变方向进行 if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.randomDir(); this.boundsCheck(); + + rectangle.x = this.x; + rectangle.y = this.y; } public void randomDir() { diff --git a/src/com/msb/TankDemo.java b/src/com/msb/TankDemo.java index 4a6b454..e657abc 100644 --- a/src/com/msb/TankDemo.java +++ b/src/com/msb/TankDemo.java @@ -14,8 +14,10 @@ public class TankDemo { TankFrame tankFrame = new TankFrame(); Random random = new Random(); + int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount")); + //创建5个敌方坦克 - for (int i=0; i<5; i++) { + for (int i=0; i bullets = new ArrayList<>(); List tanks = new ArrayList<>(); //敌方坦克 List explodes = new ArrayList<>(); //爆炸效果 - public static final int GAME_WIDTH = 800; - public static final int GAME_HEIGHT = 600; + public static final int GAME_WIDTH = 1000; + public static final int GAME_HEIGHT = 800; public TankFrame() {