坦克大战(一期)-添加配置文件的使用

Network
bingor_yhj 2 years ago
parent 5451bafed3
commit 10c7e9c552

@ -0,0 +1,6 @@
#tanks count at initialization
initTankCount=10
tankSpeed=5
bulletSpeed=10
gameWidth=1080
gameHeight=720

@ -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"));
}
}

@ -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() {

@ -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<initTankCount; i++) {
// tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(30)%4), GroupEnum.BAD, tankFrame));
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame));
}

@ -23,8 +23,8 @@ public class TankFrame extends Frame {
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>(); //敌方坦克
List<Explode> 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() {

Loading…
Cancel
Save