选题意义:
坦克大战可以帮助我们熟悉Java程序开发的环节与流程,是新手必练的简单项目,
坦克大战游戏背景
1 环境要求
操作系统:Windows 7(SP1)以上
JAVA虚拟机:JDK1.8以上
开发环境:Eclipse(4.5以上)
2 角色设定
用户在系统中扮演的角色,以及可以执行的职责。
玩 家 操纵玩家坦克,与敌方坦克作战,同时保护本基地。敌方坦克随机移动,发射子弹。
3 设定玩家(玩家一,玩家二,即一个人玩还是两个人玩),我方坦克的方向和子弹由用户控制,所有墙块都可以打碎,但是铁墙是打不碎的,草地不能阻止坦克和子弹的前进,河流能阻止坦克前进,但是不能阻止子弹前进。我方基地被毁则游戏结束,自己的坦克被毁游戏也结束,唯一赢得方式是消灭所有的敌方坦克,则可以进入下一关。一关比一关的难度大,通过地图来实现。
Wall:
Visible:
git提交截图:
关键代码
Game类
1.Bullet
public class Bullet {
//子弹的默认的速度为坦克速度的2倍
public static final int DEFAULT_SPEED = Tank.DEFAULT_SPEED << 1;
//炮弹的半径
public static final int RADIUS = 5;
private int x,y;
private int speed = DEFAULT_SPEED;
private int dir;
private int atk;
private Color color;
//子弹是否可见
private boolean visible = true;
public Bullet(int x, int y, int dir, int atk,Color color) {
this.x = x;
this.y = y;
this.dir = dir;
this.atk = atk;
this.color = color;
}
//给对象池使用的,所有的属性都是默认值。
public Bullet() {
}
/**
* 炮弹的自身的绘制的方法
* @param g
*/
public void draw(Graphics g){
if(!visible)return;
logic();
g.setColor(color);
g.fillOval(x-RADIUS,y-RADIUS,RADIUS<<1,RADIUS<<1);
}
/**
* 子弹的逻辑
*/
private void logic(){
move();
}
private void move(){
switch (dir){
case Tank.DIR_UP:
y -= speed;
if(y <= 0){
visible = false;
}
break;
case Tank.DIR_DOWN:
y += speed;
if(y > Constant.FRAME_HEIGHT){
visible = false;
}
break;
case Tank.DIR_LEFT:
x -= speed;
if(x < 0){
visible = false;
}
break;
case Tank.DIR_RIGHT:
x += speed;
if(x > Constant.FRAME_WIDTH){
visible = false;
}
break;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDir() {
return dir;
}
public void setDir(int dir) {
this.dir = dir;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
2.Explode
public class Explode {
public static final int EXPLODE_FRAME_COUNT = 12;
//导入资源
private static Image[] img;
//爆炸效果的图片的宽度和高度
private static int explodeWidth;
private static int explodeHeight;
static {
img = new Image[EXPLODE_FRAME_COUNT/3];
for (int i = 0; i < img.length; i++) {
img[i] = MyUtil.createImage("res/boom_"+i+".png");
}
}
//爆炸效果的属性
private int x,y;
//当前播放的帧的下标[0-11]
private int index;
//
private boolean visible = true;
public Explode() {
index = 0;
}
public Explode(int x, int y) {
this.x = x;
this.y = y;
index = 0;
}
public void draw(Graphics g){
//对爆炸效果图片的宽高的确定。
if(explodeHeight <= 0){
explodeHeight = img[0].getHeight(null);
explodeWidth = img[0].getWidth(null)>>1;
}
if(!visible)return;
g.drawImage(img[index/3] , x-explodeWidth ,y-explodeHeight ,null);
index ++;
//播放完最后一帧,设置为不可见
if(index >= EXPLODE_FRAME_COUNT ){
visible = false;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
@Override
public String toString() {
return "Explode{" +
"x=" + x +
", y=" + y +
", index=" + index +
", visible=" + visible +
'}';
}
}
3.GameInfo
public class GameInfo {
//从配置文件中读取
//关卡数量
private static int levelCount;
static{
Properties prop = new Properties();
try {
prop.load(new FileInputStream("level/gameinfo"));
levelCount = Integer.parseInt(prop.getProperty("levelCount"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getLevelCount() {
return levelCount;
}
}
4.LevelInof
public class LevelInof {
//构造方法私有化
private LevelInof(){}
//定义静态的本类类型的变量,来指向唯一的实例
private static LevelInof instance;
public static LevelInof getInstance(){
if(instance == null){
//创建了唯一的实例
instance = new LevelInof();
}
return instance;
}
//关卡编号
private int level;
//关卡的敌人的数量
private int enemyCount;
//通关的要求的时长,-1意味着 不限时
private int crossTime = -1;
//敌人类型信息
private int[] enemyType;
//游戏难度 >=1值
private int levelType;
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getEnemyCount() {
return enemyCount;
}
public void setEnemyCount(int enemyCount) {
this.enemyCount = enemyCount;
}
public int getCrossTime() {
return crossTime;
}
public void setCrossTime(int crossTime) {
this.crossTime = crossTime;
}
public int[] getEnemyType() {
return enemyType;
}
public void setEnemyType(int[] enemyType) {
this.enemyType = enemyType;
}
public int getLevelType() {
return levelType <=0 ? 1 : levelType;
}
public void setLevelType(int levelType) {
this.levelType = levelType;
}
//获得敌人类型数组中的随机的一个元素。
//获得一个随机的敌人的类型
public int getRandomEnemyType(){
int index = MyUtil.getRandomNumber(0,enemyType.length);
return enemyType[index];
}
}