今天给大家分享一个使用java编写的坦克大战小游戏,整体还是挺好玩的,通过对这款游戏的简单实现,加深对java基础的深刻理解。
一、设计思路
1.坦克大战小游戏通过java实现,其第一步需要先绘制每一关对应的地图,地图包括河流、草地、砖块、铁块等。
2.需要绘制玩家坦克、敌方坦克、以及坦克移动过程中使用到的碰撞算法,子弹与坦克之间的碰撞,
包括击中敌方坦克后的爆炸效果,通过重绘实现坦克的移动,以及各种道具的随机生成算法。
实际运行效果如下:

二、代码实现
1.首先需要将游戏中涉及到的各种对象梳理清楚,由于Java面向对象的特征,可以将一些对象公共特性抽象出来,比如将坦克抽象出一个超类,代码如下:
public abstract class Tank{
int x=0;
int y=0;
int tempX=0;
int tempY=0;
int size=32;
int direct=Constant.UP;
int speed=1;
int lives=1;
int frame=0;//控制敌方坦克切换方向的时间
boolean isAI=false;//是否自动
boolean hit;
boolean isShooting=false;
boolean isDestroyed=false;
boolean isProtected=false;
Map map;
GameMain gameMain;
Collision collision;
Bullet bullet;
int starNum=0; //星星数
public Tank(int size,int speed,Collision collision,Map map,GameMain gameMain) {
this.size=size;
this.collision=collision;
this.map=map;
this.gameMain=gameMain;
this.speed=speed;
}
public void move() {
//如果是AI坦克,在一定时间或碰撞后切换方法
if(this.isAI && gameMain.enemyStopTime>0) {
return;
}
this.tempX=this.x;
this.tempY=this.y;
if(this.isAI) {
this.frame++;
if(this.frame%100==0 || this.hit) {
this.direct=(int)(Math.random()*4);
this.hit=false;
this.frame=0;
}
}
if(this.direct==Constant.UP) {
this.tempY-=this.speed;
}else if(this.direct==Constant.DOWN) {
this.tempY+=this.speed;
}else if(this.direct==Constant.LEFT) {
this.tempX-=this.speed;
}else if(this.direct==Constant.RIGHT) {
this.tempX+=this.speed;
}
isHit();
if(!this.hit) {
this.x=this.tempX;
this.y=this.tempY;
}
}
public void isHit() {
if(this.direct==Constant.LEFT) {
if(this.x<=map.offsetX) {
this.x=map.offsetX;
this.hit=true;
}
}else if(this.direct==Constant.RIGHT) {
if(this.x>=map.offsetX+map.mapWidth-this.size) {
this.x=map.offsetX+map.mapWidth-this.size;
this.hit=true;
}
}else if(this.direct==Constant.UP) {
if(this.y<=map.offsetY) {
this.y=map.offsetY;
this.hit=true;
}
}else if(this.direct==Constant.DOWN) {
if(this.y>=map.offsetY+map.mapHeight-this.size) {
this.y=map.offsetY+map.mapHeight-this.size;
this.hit=true;
}
}
if(!this.hit) {
if(collision.tankMapCollision(this, map)) {
this.hit=true;
}
}
}
public abstract void drawTank(Graphics2D ctx2);
public void shoot(int type,Graphics2D ctx2) {
if(this.isAI && gameMain.enemyStopTime>0) {
return;
}
if(this.isShooting) {
return;
}
int tempX=this.x;
int tempY=this.y;
this.bullet=new Bullet(tempX, tempY, this.direct, gameMain, type, map, this, collision);
if(!this.isAI) {
if(this.starNum==0) {
this.bullet.speed=3;
this.bullet.fireNum=1;
}
if(this.starNum>0 && this.starNum<=3) {
this.bullet.speed+=this.starNum;
this.bullet.fireNum+=(this.starNum-1);
}
if(this.starNum>3) {
this.bullet.speed+=3;
this.bullet.fireNum+=3;
}
}
if(this.direct==Constant.UP) {
tempX = this.x + this.size/2 - this.bullet.size/2;
tempY=this.y-this.bullet.size;
}else if(this.direct==Constant.DOWN) {
tempX = this.x + this.size/2 - this.bullet.size/2;
tempY=this.y+this.bullet.size;
}else if(this.direct==Constant.LEFT) {
tempX=this.x-this.bullet.size;
tempY=this.y + this.size/2 - this.bullet.size/2;
}else if(this.direct==Constant.RIGHT) {
tempX=this.x+this.bullet.size;
tempY=this.y + this.size/2 - this.bullet.size/2;
}
this.bullet.x=tempX;
this.bullet.y=tempY;
if(!this.isAI) {
//音乐
Constant.executor.execute(new Runnable() {
@Override
public void run() {
Constant.ATTACK_AUDIO.play();
}
});
}
this.bullet.drawBullet(ctx2);
gameMain.bulletArr.add(this.bullet);
this.isShooting=true;
}
public void distroy() {
this.isDestroyed=true;
gameMain.crackArr.add(new CrackAnimation(gameMain, Constant.CRACK_TYPE_TANK, this));
if(this.isAI) {
Constant.executor.execute(new Runnable() {
@Override
public void run() {
Constant.TANK_DESTROY_AUDIO.play();
}
});
gameMain.appearEnemy--;
}else {
Constant.executor.execute(new Runnable() {
@Override
public void run() {
Constant.PLAYER_DESTROY_AUDIO.play();
}
});
this.starNum=0;
}
}
}
2.坦克子类实现
pub

最低0.47元/天 解锁文章
2308

被折叠的 条评论
为什么被折叠?



