经典游戏,用java实现的坦克大战小游戏

今天给大家分享一个使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空下的星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值