用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

仿98版坦克大战

2016-11-24 作者: 陨石坠灭举报

[java]代码库

//话说这是我的心血啊~~~
//由于导入项目时中文是乱码,所以把注释给删了。。。
//项目运用了抽象编程的思想,关联太多,所以仅仅该部分是远远不能运行的
//其中参数非常之多,然后我写了Tank8,运用了设计模式,运用了一个非常好的游戏代码框架,所以这个项目bug太多,太杂已经基//本上被我抛弃,但是,但是!!!这是我看完《java编程思想》后写的,里面有好多好多值得学习的地方,而且项目相对比较完整
//项目运行会有个别电脑有区别,我的是64位,比较正常。。。 
//是我最得意的作品。。。


package ObjectT;

import java.awt.Point;
import java.util.LinkedList;
import java.util.Random;

import EnumT.Direction;
import EnumT.ObjectType;
import EnumT.ObjectType.ExplodeType;
import EnumT.ObjectType.TankType;
import EnumT.ObjectType.TankType.TankColor;
import EnumT.ObjectType.TreasureType;
import EnumT.Status;
import MapT.MainFram;
import MapT.Map_War;
import MapT.ObjectPanel;
import MapT.ScoreListener;
/**
 */
public class Tank extends ObjectT implements Runnable{
	
	public static boolean STOP=false;
	
	private final TankType tanktype;
	private boolean enable;
	private int lifetime;
	private boolean isPlayer;
	
	private boolean hasBao=false;
	private boolean destroyable;
	
	private int speed=1;
	private int step =1;
	private int speed2=100;
	private int state=0;
	
	private ScoreListener sl=null;
	
	private long timeL;
	private final long interval=400;
	
	private TankColor tankColor=TankColor.gray;
	
	private static LinkedList<Tank> tankBank=new LinkedList<Tank>();
		
	public Tank(TankType tanktype,int curImg,Point location,ObjectPanel objp,boolean isPlayer) {
		super(tanktype.getParentType(),
				new Rectangle(location.x,location.y,Map_War.GRIDNE*Map_War.WIDTHE,Map_War.GRIDNE*Map_War.WIDTHE), 20, 
				RSourse.ImgSourse.getTankImgs(tanktype),
				objp);
		this.tanktype=tanktype;
		this.isPlayer=isPlayer;
		setCurIntList(curImg);
		setTag(isPlayer?1:3);
		if(isPlayer||tanktype==TankType.tank3){
			setLifetime(3);
		}else{
			setLifetime(1);
			if(tanktype==TankType.tank2){
				speed2=70;
			}
		}		
		enable=true;
		destroyable=false;
		timeL=System.currentTimeMillis();
	}
	/**
	 * 
	 */
	private void bomb(){
		if(isPlayer)
			RSourse.SoundSourse.playBomb(getObjp());
		Bullet b=Bullet.get(this, getObjp());
		if(b!=null){
			getObjp().add(b);
			getObjp().addThread(b);
		}
	}
	/**
	 * 
	 */
	public void Bomb(){
		if(System.currentTimeMillis()-timeL>=interval){
			if(enable&&((!getObjp().isStop())&&(!isStop())))
			bomb();
			if(isPlayer&&getState()>1){
				try {
					Thread.sleep(100);
					bomb();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			timeL=System.currentTimeMillis();
		}
	}
	/**
	 * 
	 * @param DX
	 * @param DY
	 * @param k
	 * @return
	 */
	public boolean canMove(int DX,int DY,int k){
		if((getX()==0&&k<0&&DX>0)||(getY()==0&&k<0&&DY>0)||
				(getX()+getWidth()==Map_War.WIDTH&&k>0&&DX>0)||
				(getY()+getHeight()==Map_War.WIDTH&&k>0&&DY>0)){
			return false;
		}
		
		if(getX()<0||getY()<0||
				(getX()+getWidth()>Map_War.WIDTH)||
				(getY()+getHeight()>Map_War.WIDTH)){
			if(isPlayer){
				
			}else{
				destroyE();
			}
		}
		
		Rectangle rect=getRectangle();
		rect.setX(rect.getX()+DX*k);
		rect.setY(rect.getY()+DY*k);
		for (ObjectT obj : getObjp().getObjArr()) {
			if(this!=obj&&obj!=null&&
					(getTop()==obj.getTop()||obj.getTop()==0||(isPlayer&&obj.getTop()==60))
					&&rect.inScope(obj.getRectangle())){
				if(obj.getType()==ObjectType.treasure){
					obj.destroy();
					addTreasure(((Treasure)obj).gettType());
				}
				return false;
			}
		}
		return true;
	}
	/**
	 * @param trtype
	 */
	public void addTreasure(TreasureType trtype){
		RSourse.SoundSourse.playTreasure(getObjp());
		switch (trtype) {
		case life:
			setLifetime(lifetime+1);
			break;
		case timing:
			getObjp().addThread(new Runnable() {
				@Override
				public void run() {
					try {
						STOP=true;
						for (ObjectT obj : getObjp().getObjArr()) {
							if(obj!=null&&obj.getType()==ObjectType.tank){
								if(!((Tank)obj).isPlayer){
									((Tank)obj).setStop(true);
								}
							}
						}
						Thread.sleep(8000);
						STOP=false;
						for (ObjectT obj : getObjp().getObjArr()) {
							if(obj!=null&&obj.getType()==ObjectType.tank){
								if(!((Tank)obj).isPlayer){
									((Tank)obj).setStop(false);
								}
							}
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			break;
		case bomb:
			for (ObjectT obj : getObjp().getObjArr()) {
				if(obj!=null&&obj.getType()==ObjectType.tank){
					if(!((Tank)obj).isPlayer){
						((Tank)obj).destroy2();
					}
				}
			}
			break;
		case athanasy:
			if(getLocl()==null){
				setDestroyable(false);
				TSObj tsobj=TSObj.get(this,getObjp(), false);
				getObjp().add(tsobj);
				getObjp().addThread(tsobj);
				addLocationListener(tsobj);
			}
			
			break;
		case steel_wall:
		{
			getObjp().addThread(new Runnable() {
				@Override
				public void run() {
					try {
						ObjectT[] walls=Wall.getWalls();
						if(walls!=null)
							for (ObjectT objectT : walls) {
								getObjp().remove(objectT);
							}
						ObjectT[] steels=Wall.getWalls2((Map_War)getObjp());
						getObjp().addAll(steels);
						Thread.sleep(20000);
						if(steels!=null)
							for (ObjectT objectT : steels) {
								getObjp().remove(objectT);
							}
						getObjp().addAll(walls);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});

		}
			break;
		case upgrade:
			setState(getState()+1);
			break;
		default:
			break;
		}
	}
	/**
	 *
	 * @param tanktype
	 * @param objp
	 * @return
	 */
	public boolean equals(TankType tanktype,ObjectPanel objp) {
		return getObjp()==objp&&tanktype==getTanktype();
	} 
	/**
	 * ̹
	 * @param DX
	 * @param DY
	 * @param k
	 * @return
	 */
	public boolean move(int DX,int DY,int k){		
		if(!canMove(DX, DY, k))return false;
		try {
			int x0=getX()+DX*k,y0=getY()+DY*k;
			if(step>=DX&&step>=DY){
				Thread.sleep(speed);
				setLocation(x0, y0);
				return true;
			}
			while(getX()!=x0||getY()!=y0){
				Thread.sleep(speed);
				setLocation(
					Math.abs(x0-getX())<step?getX()+Math.abs(x0-getX())*k:getX()+step*k,
					Math.abs(y0-getY())<step?getY()+Math.abs(y0-getY())*k:getY()+step*k);
			}	
		} catch (Exception e) {
			
		}
		return true;
	}
	/**
	 * 
	 * @param dir
	 * @return
	 */
	public boolean move(Direction dir){
		if(!enable)return false;
		int WidE=Map_War.WIDTHE*Map_War.GRIDNE/4;
		if(dir!=getDir()){
			int DX=0;
			int DY=0;
			int k=1;
			switch (dir) {
			case up:
				DY=WidE;
				k=-1;
				break;
			case down:
				DY=WidE;
				k=1;
				break;
			case left:
				DX=WidE;
				k=-1;
				break;
			case right:
				DX=WidE;
				k=1;
				break;
			}						
			if(!canMove(DX, DY, k)){
				setCurIntList(RSourse.ImgSourse.getIndex(tanktype, tankColor, getDir(), 1));
				switch (getDir()) {
					case up:
						move(0,WidE,-1);
						break;
					case down:
						move(0,WidE,1);
						break;
					case left:
						move(WidE,0,-1);
						break;
					case right:
						move(WidE,0,1);
						break;
				}
			}
			setDir(dir);
			setCurIntList(RSourse.ImgSourse.getIndex(tanktype, tankColor, getDir(), 0));
			return true;
		}else{			
			setCurIntList(RSourse.ImgSourse.getIndex(tanktype, tankColor, getDir(), 1));
		}		
		boolean kk=true;
		switch (dir) {
			case up:
				kk=move(0,WidE,-1);
				break;
			case down:
				kk=move(0,WidE,1);
				break;
			case left:
				kk=move(WidE,0,-1);
				break;
			case right:
				kk=move(WidE,0,1);
				break;
		}
		setCurIntList(RSourse.ImgSourse.getIndex(tanktype, tankColor, getDir(), 0));
		return kk;
	}
	/////////////////////////////////////////////////////////////////////////
	/**
	 * 
	 * @param tank
	 */
	public static synchronized void add(Tank tank){
		if(tank!=null&&(!tank.isPlayer))
			tankBank.push(tank);
	}
	/**
	 * 
	 * @param tanktype
	 * @param objp
	 * @return
	 */
	public static synchronized Tank get(TankType tanktype,ObjectPanel objp){
		for (int i = 0; i < tankBank.size(); i++) {
			Tank tank=tankBank.get(i);
			if(tank.equals(tanktype, objp)){
				tankBank.remove(tank);
				tank.getTanktype().setHasBao(tanktype.getHasBao());
				InitTank(tank, objp);
				tank.setHasBao(tanktype.getHasBao());
				if(tank.isHasBao()){
					objp.addThread(tank.new changeColor());
				}
				return tank;
			}
		}
		Direction dir=Direction.next();
	Tank tank=new Tank(tanktype, 
				RSourse.ImgSourse.getIndex(tanktype,
						tanktype.getHasBao()? TankColor.gray :
					(tanktype==TankType.tank3?TankColor.green:TankColor.gray),
								dir, 0),
				getRandomLocation(objp), objp, false);
		tank.setTankColor(tanktype==TankType.tank3?TankColor.green:TankColor.gray);
		tank.setDir(dir);
		tank.setHasBao(tanktype.getHasBao());
		tank.setDestroyable(false);
		if(tank.isHasBao()){
			objp.addThread(tank.new changeColor());
		}
		return tank;
	}
	/**
	 * 
	 * @param player1
	 * @param isBegan
	 */
	public static void InitPlayer1(Tank player1,boolean isBegan){
		
		player1.setLocation(4*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
		player1.setDir(Direction.up);
		player1.setStop(true);
		player1.setDestroyable(false);
		player1.enable=true;
		player1.setWorkAble(true);
		player1.speed=1;
		player1.step=1;
		player1.setTimeL(System.currentTimeMillis());
		player1.setCurIntList(RSourse.ImgSourse.getIndex(player1.getTanktype(), player1.getTankColor(), Direction.up, 0));
		if(isBegan){
			player1.setLifetime(3);
			player1.setState(0);
			System.err.println(player1.getState());
		}
		
		if(isBegan||player1.getLifetime()>0){
			TSObj tsobj=TSObj.get(player1, player1.getObjp(), true);
			player1.getObjp().add(tsobj);
			player1.getObjp().addThread(tsobj);
			player1.addLocationListener(tsobj);
		}
		
		
	}
	/**
	 * 
	 * @param player2
	 * @param isBegan
	 */
	public static void InitPlayer2(Tank player2,boolean isBegan){
		
		player2.setLocation(8*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
		player2.setDir(Direction.up);
		player2.setDestroyable(false);
		player2.setStop(true);
		player2.enable=true;
		player2.setWorkAble(true);
		player2.speed=1;
		player2.step=1;
		player2.setTimeL(System.currentTimeMillis());
		player2.setCurIntList(RSourse.ImgSourse.getIndex(player2.getTanktype(), player2.getTankColor(), Direction.up, 0));
		if(isBegan){
			player2.setLifetime(3);
			player2.setState(0);			
		}
		
		
		
		if(isBegan||player2.getLifetime()>0){
			TSObj tsobj=TSObj.get(player2, player2.getObjp(), true);
			player2.getObjp().add(tsobj);
			player2.getObjp().addThread(tsobj);
			player2.addLocationListener(tsobj);
		}
	}
	/**
	 * 
	 * @param tank
	 * @param objp
	 */
	public static void InitTank(Tank tank,ObjectPanel objp){
		tank.setLocation(getRandomLocation(objp));
		tank.setTankColor(
				tank.isHasBao()? TankColor.gray :
					(	tank.getTanktype()==TankType.tank3?TankColor.green:TankColor.gray));
		tank.setCurIntList(RSourse.ImgSourse.getIndex(tank.getTanktype(), tank.getTankColor(), Direction.next(), 0));
		tank.setStop(true);
		tank.enable=true;	
		if(tank.tanktype==TankType.tank3){
			tank.setLifetime(3);
		}else{
			tank.setLifetime(1);
		}
		objp.addThread(TSObj.get(tank, objp, true));
		tank.setTimeL(System.currentTimeMillis());
	}
	/**
	 *
	 * @param objp
	 * @return
	 */
	public static Tank getPlayer1(ObjectPanel objp){
		
		objp.setDrawNow(false);
		Tank player1=new Tank(TankType.tank1,RSourse.ImgSourse.getIndex(TankType.tank1, TankColor.blue,	Direction.up,0), 
				new Point(4*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE), objp, true);
		//player1.setLocation(4*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
		player1.setTankColor(TankColor.blue);
		player1.setDir(Direction.up);
		player1.setStop(true);
		player1.setState(0);
		objp.setDrawNow(true);
		
		
		TSObj tsobj=TSObj.get(player1, objp, true);
		objp.add(tsobj);
		objp.addThread(tsobj);
		player1.addLocationListener(tsobj);
		
		return player1;
	}
	/**
	 *
	 * @param objp
	 * @return
	 */
	public static Tank getPlayer2(ObjectPanel objp){
		
		objp.setDrawNow(false);
		Tank player2=new Tank(TankType.tank1,RSourse.ImgSourse.getIndex(TankType.tank1, TankColor.yellow,	Direction.up,0), 
				new Point(8*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE), objp, true);
		//player2.setLocation(8*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
		player2.setTankColor(TankColor.yellow);
		player2.setDir(Direction.up);
		player2.setTag(2);
		player2.setState(0);
		player2.setStop(true);
		objp.setDrawNow(true);
		
		
		TSObj tsobj=TSObj.get(player2, objp, true);
		objp.add(tsobj);
		objp.addThread(tsobj);
		player2.addLocationListener(tsobj);
		
		return player2;
	}
	/**
	 * 
	 */
	public static void clear(){
		tankBank.clear();
	}
	/**
	 *
	 * @param rect
	 * @param objp
	 * @return
	 */
	public static boolean canBeing(Rectangle rect,ObjectPanel objp){
		if(rect.getX()<0||rect.getY()<0||
				rect.getX()+rect.getWidth()>Map_War.WIDTH||
				rect.getY()+rect.getHeight()>Map_War.WIDTH)return false;
		for (ObjectT obj : objp.getObjArr()) {
			if(obj!=null&&(obj.getTop()==20||obj.getTop()==0)&&rect.inScope(obj.getRectangle())){
				return false;
			}
		}		
		return true;
	}
	/**
	 * 
	 * @param objp
	 * @return
	 */
	public static Point getRandomLocation(ObjectPanel objp){
		
		Random ran=new Random(System.currentTimeMillis());
		int width=Map_War.GRIDNE*Map_War.WIDTHE;
		int scole=Map_War.GRIDNE*(Map_War.GRIDN-1);
		Rectangle rect=new Rectangle(ran.nextInt(scole)*Map_War.GRIDNE*Map_War.WIDTHE, 0,width ,width);		
		while(!canBeing(rect, objp)){
			rect.setX((ran.nextInt(scole)*Map_War.GRIDNE*Map_War.WIDTHE));
		}	
		
		return rect.getLocation();
	}
	private static Random r=new Random(System.currentTimeMillis());
	public static boolean getRandomBool(){		
		return r.nextBoolean();
	}
	@Override
	/**
	 *
	 */
	public void destroyE() {
		setStop(false);
		if(isPlayer){
			getObjp().remove(this);
		}else{
			setEnable(false);
			getObjp().remove(this);
			add(this);
		}
	}
	/**
	 * 
	 */
	public void destroy2(){
		//if(!destroyable)return;
		enable=false;
		setLifetime(0);
		if(getLifetime()<=0&&(!isPlayer)){
			setStop(false);
			RSourse.SoundSourse.playExplode1(getObjp());
			Explode ex=Explode.get(ExplodeType.explodePngs, this, getObjp());
			getObjp().add(ex);
			getObjp().addThread(ex);
			if(sl!=null)sl.TankDestroyed(getTanktype());
			getObjp().func_child("setSizeTank",new Object[]{
					(int)getObjp().func_child("getSizeTank",null)-1
			});
			super.destroy();
			add(this);
		}
	}
	@Override
	/**
	 *
	 */
	public void destroy() {
		if(!destroyable)return;
		if(isPlayer&&((Map_War)getObjp()).IsWG())return;
		enable=false;
		setState(0);
		//tanktype.setHasBao(false);
		if(isHasBao()&&((tanktype==TankType.tank3&&getLifetime()==3)||(tanktype!=TankType.tank3))){
			Treasure tr=Treasure.get(getObjp());
			getObjp().add(tr);
			getObjp().addThread(tr);
		}
		setLifetime(getLifetime()-1);
		if(getLifetime()<=0){
			setStop(false);
			RSourse.SoundSourse.playExplode1(getObjp());
			Explode ex=Explode.get(ExplodeType.explodePngs, this, getObjp());
			getObjp().add(ex);
			getObjp().addThread(ex);
			if(!isPlayer){
				if(sl!=null)sl.TankDestroyed(getTanktype());
				getObjp().func_child("setSizeTank",new Object[]{
						(int)getObjp().func_child("getSizeTank",null)-1
				});
				super.destroy();
				add(this);
			}else{
				super.destroy();
			}
		}else{
			
			if(tanktype==TankType.tank3){
				//this.tanktype.setHasBao(false);
				switch (getLifetime()){
				case 1:
					setTankColor(TankColor.gray);break;
				case 2:
					setTankColor(TankColor.blue);break;
				case 3:
					setTankColor(TankColor.purple);break;
				default:
					break;
				}
			}else if(isPlayer){
				RSourse.SoundSourse.playExplode1(getObjp());
				Explode ex=Explode.get(ExplodeType.explodePngs, this, getObjp());
				getObjp().add(ex);
				getObjp().addThread(ex);
				
//				setDir(Direction.up);
//				if(getTag()==1){
//					setLocation(4*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
//				}else{
//					setLocation(8*Map_War.GRIDNE*Map_War.WIDTHE,12*Map_War.GRIDNE*Map_War.WIDTHE);
//				}
				
				if(getTag()==1)
				InitPlayer1(this, false);
				else if(getTag()==2)
					InitPlayer2(this, false);
				
			}
			setEnable(true);
		}
	}
	
	///////////////////////////////////////
	public TankType getTanktype() {
		return tanktype;
	}
	public boolean isEnable(){
		return enable;
	}
	public int getLifetime() {
		return lifetime;
	}
	public void setLifetime(int lifetime) {
		this.lifetime = lifetime;
		if(sl!=null&&isPlayer)sl.LifeTimeChanged(getTag(), lifetime);
	}
	public boolean IsPlayer(){
		return isPlayer;
	}
	public TankColor getTankColor() {
		return tankColor;
	}
	public int getSpeed() {
		return speed;
	}
	public int getStep() {
		return step;
	}
	public void setTankColor(TankColor tankColor) {
		this.tankColor = tankColor;
	}
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	public void addScoreListener(ScoreListener sl){
		this.sl=sl;
		if(isPlayer){
			sl.LifeTimeChanged(getTag(), getLifetime());
		}
	}
	public long getInterval() {
		return interval;
	}
	public long getTimeL() {
		return timeL;
	}
	public void setTimeL(long timeL) {
		this.timeL = timeL;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public boolean isHasBao(){
		return hasBao;
	}
	public void setHasBao(boolean hasBao){
		this.hasBao=hasBao;
	}
	public boolean isDestroyable(){
		return destroyable;
	}
	public void setDestroyable(boolean destroyable) {
		this.destroyable = destroyable;
	}
	@Override
	/**
	 * 
	 */
	public void run() {
		new BulletFactory(this);
		while(enable&&(MainFram.status==Status.stop||MainFram.status==Status.playingGame)){
			try {
				if(!move(getDir())){
					Thread.sleep(10);
					if(getRandomBool()&&getRandomBool())
						move(Direction.next());
					else if(getRandomBool()){
						Bomb();
					}
				}				
				//Bomb();
				Thread.sleep(speed2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
		}
	}
	/**
	 * 
	 * 
	 */
	class changeColor implements Runnable{

		@Override
		public void run() {
			if(!isHasBao())return;
			try {
				Thread.sleep(500);;
	
				while (isHasBao()&&((tanktype==TankType.tank3&&getLifetime()==3)||(tanktype!=TankType.tank3))) {
					setTankColor(TankColor.red);
					Thread.sleep(200);
					setTankColor(TankColor.gray);
					Thread.sleep(200);
				}
			} catch (Exception e) {
				
			}
			if(tanktype==TankType.tank3){
				switch (getLifetime()){
				case 1:
					setTankColor(TankColor.gray);break;
				case 2:
					setTankColor(TankColor.blue);break;
				case 3:
					setTankColor(TankColor.purple);break;
				default:
					break;
				}
			}
		}
		
	}
}

[代码运行效果截图]


仿98版坦克大战

[源代码打包下载]




网友评论    (发表评论)

共12 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...