用java实现的坦克大战

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Vector;
public class warOfTank {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MAP.map();
		MyFrame mf = new MyFrame("Hello window 7!");
	       mf.addWindowListener(new WindowAdapter()//添加窗口关闭处理函数
	       {
	           public void windowClosing(WindowEvent e)
	           {
	              System.exit(0);
	           }});
	}
}

class MAP{
	public static byte [][] map = new byte [20][20];
	public static void map(){
		for(int i = 0 ; i < map.length ;i++)
		{
			map[0][i] = -1;
			map[map.length-1][i] = -1;
		}
		for(int i = 0 ; i < map.length ; i++)
		{
			map[i][0] = -1;
			map[i][map.length-1] = -1;
		}
	}
}

class MyFrame extends Frame{
	private Image iBuffer;
	private Graphics gBuffer;
	final int wide = 30;//每个格子的宽度
	final int height = 30;//每个格子的高度
	BasicTank bt = null;
	BasicTank bb = null;
	BasicTank cc = null;
	BasicTank dd = null;
	MyFrame mf = this;
	MyFrame(String name){
		setTitle(name);
		setBounds(300,50,MAP.map.length+MAP.map.length*30+40,MAP.map.length+MAP.map.length*30+40);
		setVisible(true);
		addKeyListener(new MyKeyMonitor());
		bt = new BasicTank();
		bb = new EnemyTank(1,10,0);
		cc = new EnemyTank(10,10,0);


		new EnemyThread(this).start();

		
	}
	
	class MyKeyMonitor extends KeyAdapter{
		public void keyPressed(KeyEvent e){
			int keyCode = e.getKeyCode();
			switch(keyCode)
			{
			case KeyEvent.VK_UP :
										
										bt.moveUp();
										repaint();
									break;
			case KeyEvent.VK_DOWN :  	
										
										bt.moveDown();
										repaint();
									break;
			case KeyEvent.VK_LEFT :		bt.moveLeft();
										repaint();
									break;
			case KeyEvent.VK_RIGHT :  	bt.moveRight();
											repaint(); 
										break;
			case KeyEvent.VK_SPACE :
				bt.openFire(gBuffer,mf);
				repaint(); 
				break;
				
			}
		}
	}
	
	public void paint(Graphics g){
		if(iBuffer == null){
			iBuffer = createImage(this.getSize().width,this.getSize().height);
			gBuffer = iBuffer.getGraphics();
		}
		Color c = g.getColor();
		gBuffer.fillRect(0,0, getSize().width, getSize().height);
		
		for(int i = 0 ; i < MAP.map.length ; i++)
			for(int j = 0 ; j < MAP.map.length ; j++)
				if(MAP.map[i][j] == 0)
				{
					gBuffer.setColor(Color.yellow);
					gBuffer.fillRect(30+30*i+i,40+30*j+j, 30, 30);

				}
					else
					{
						gBuffer.setColor(Color.RED);
						gBuffer.fillRect(30+30*i+i,40+30*j+j, 30, 30);
					}

		for(int i = 0;i ammo = new Vector(1,1);
	BasicTank(){
		publicID++;
 		for(int i = 0;i<3;i++)
			for(int j = 0;j<3;j++)
				if(Up[i][j] == 1)
					MAP.map[LocationX+j][LocationY+i] = ID ;
		
		UP = false;
		
	}
	
	BasicTank(int x,int y,int direction){
		LocationX = x;
		LocationY = y;
		Direction = direction;
		publicID++;
		switch(Direction){
		case 0 : 		for(int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(Up[i][j] == 1)
									MAP.map[LocationX+j][LocationY+i] = ID ;
						break;
		case 1 : 		for(int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(Down[i][j] == 1)
									MAP.map[LocationX+j][LocationY+i] = ID ;
						break;
		case 2 : 		for(int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(Left[i][j] == 1)
									MAP.map[LocationX+j][LocationY+i] = ID ;
						break;
		case 3 : 		for(int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(Right[i][j] == 1)
									MAP.map[LocationX+j][LocationY+i] = ID ;
						break;	
		}
		
	}
	void clear(){
		for(int i = 0;i<3;i++)
			for(int j = 0;j<3;j++)
				if(MAP.map[LocationX+i][LocationY+j] == ID)
					MAP.map[LocationX+i][LocationY+j] = 0;
	}
	boolean judgerTurn(int direction){
		boolean a = false;
		switch(direction)
		{case 0 : 	 	for(int i=0;i<3;i++)
							for(int j=0;j<3;j++)
								if(Up[j][i] == 1)
									if(MAP.map[LocationX+i][LocationY+j-1]==0||MAP.map[LocationX+i][LocationY+j-1]==ID)
										a = true;
									else
										return false;
						break;
						
		case 1 : 	 	for(int i=0;i<3;i++)
							for(int j=0;j<3;j++)
								if(Down[j][i] == 1)
									if(MAP.map[LocationX+i][LocationY+j+1]==0||MAP.map[LocationX+i][LocationY+j+1]==ID)
										a = true;
									else
										return false;
						break;
		case 2 : 	 	for(int i=0;i<3;i++)
							for(int j=0;j<3;j++)
								if(Left[j][i] == 1)
									if(MAP.map[LocationX+i-1][LocationY+j]==0||MAP.map[LocationX+i-1][LocationY+j]==ID)
										a = true;
									else
										return false;
						break;
		case 3 : 	 	for(int i=0;i<3;i++)
							for(int j=0;j<3;j++)
								if(Right[j][i] == 1)
									if(MAP.map[LocationX+i+1][LocationY+j]==0||MAP.map[LocationX+i+1][LocationY+j]==ID)
										a = true;
									else
										return false;
						break;
		}

		return a;
	}
	 
	 boolean judgeMove(){
		 boolean  a = false;
		 switch(Direction){
		 case 0 :
			 if(MAP.map[LocationX][LocationY] == 0&&MAP.map[LocationX+1][LocationY-1] == 0&&MAP.map[LocationX+2][LocationY] == 0)
				 a = true;
		 case 1 :
			 if(MAP.map[LocationX][LocationY+2] == 0&&MAP.map[LocationX+2][LocationY+2] == 0&&MAP.map[LocationX+1][LocationY+3] == 0)
				 a = true;
		 case 2 :
			 if(MAP.map[LocationX][LocationY] == 0&&MAP.map[LocationX-1][LocationY+1] == 0&&MAP.map[LocationX][LocationY+2] == 0)
				 a = true;
		 case 3 :
			 if(MAP.map[LocationX+2][LocationY] == 0&&MAP.map[LocationX+2][LocationY+2] == 0&&MAP.map[LocationX+3][LocationY+1] == 0)
				 a = true;
		 }
		 return a;
	 }
	 
	 boolean turnUp(){
		boolean a = judgerTurn(0);
		if(UP&&a){
			clear();
			Direction = 0;
			UP = false;DOWN = true;LEFT = true;RIGHT = true;//jUP = true;
		}
		return a;
	}
	 boolean turnDown(){
		boolean a = judgerTurn(1);
		if(DOWN&&a){
			clear();
			Direction = 1;
			UP = true;DOWN = false;LEFT = true;RIGHT = true;//jDOWN = true;
		}
		return a;
	}
	
	 boolean turnLeft(){
		boolean a = judgerTurn(2);
		if(LEFT&&a){
			clear();
			Direction = 2;
			UP = true;DOWN = true;LEFT = false;RIGHT = true;//jLEFT = true;
		}
		return a;
	}
	
	 boolean turnRight(){
		boolean a = judgerTurn(3);
		if(RIGHT&&a){
			clear();
			Direction = 3;
			UP = true;DOWN = true;LEFT = true;RIGHT = false;//jRIGHT = true;
		}
		return a;
	}
	
	boolean moveUp(){
		boolean a = turnUp();
		if(MAP.map[LocationX][LocationY] == 0&&MAP.map[LocationX+1][LocationY-1] == 0&&MAP.map[LocationX+2][LocationY] == 0)
		{
			{
				clear();
				
					LocationY--;
				for(int i = 0;i<3;i++)
					for(int j = 0;j<3;j++)
						if(Up[i][j] == 1)
							MAP.map[LocationX+j][LocationY+i] = ID;
			}
			return true;
		}
		return false;
	}
	
	boolean moveDown(){
		boolean a = turnDown();
		if(MAP.map[LocationX][LocationY+2] == 0&&MAP.map[LocationX+2][LocationY+2] == 0&&MAP.map[LocationX+1][LocationY+3] == 0)
		{
			{
				clear();
				LocationY++;
				for(int i = 0;i<3;i++)
					for(int j = 0;j<3;j++)
						if(Down[i][j] == 1)
							MAP.map[LocationX+j][LocationY+i] = ID;
			}
			return true;
		}
		return false;
	}
	boolean moveLeft(){
		boolean a = turnLeft();
		if(MAP.map[LocationX][LocationY] == 0&&MAP.map[LocationX-1][LocationY+1] == 0&&MAP.map[LocationX][LocationY+2] == 0)
		{
			{
				clear();
				LocationX--;
				for(int i = 0;i<3;i++)
					for(int j = 0;j<3;j++)
						if(Left[i][j] == 1)
							MAP.map[LocationX+j][LocationY+i] = ID;
			}
			return true;
		}
		return false;
	}
	
	boolean moveRight(){
		boolean a = turnRight();
		if(MAP.map[LocationX+2][LocationY] == 0&&MAP.map[LocationX+2][LocationY+2] == 0&&MAP.map[LocationX+3][LocationY+1] == 0)
		{
			{
				clear();
				LocationX++;
				for(int i = 0;i<3;i++)
					for(int j = 0;j<3;j++)
						if(Right[i][j] == 1)
							MAP.map[LocationX+j][LocationY+i] = ID;
			}
			return true;
		}
		return false;
	}
	void openFire(Graphics gBuffer,MyFrame mf){
		Ammo ammo1 = new Ammo(this,mf);
		ammo.addElement(ammo1);
		 ammo.lastElement() .setGraphics(gBuffer);
		ammo.lastElement() .move(LocationX,LocationY,Direction);
		ammo.lastElement() .ammothread.start();
		ammo.lastElement() .judgeSpace = true;
	}
}

class Ammo{
	static int publicID = 0;
	int ID = 0;
	int LX = 0;
	int LY = 0;
	int wide = 30;
	int height = 30;
	boolean judgeSpace = false;
	Graphics g ;
	int Direction = 0;
	MyFrame mf = null;
	ammoThread ammothread = null;
	Ammo(BasicTank bt,MyFrame mf){
		ID = publicID ;
		Direction = bt.Direction;
		this.mf = mf;
		ammothread = new ammoThread(this,mf,bt);
		publicID++;
		System.out.println(""+ID);
	}
	void setGraphics (Graphics g){
		this.g = g;
	}
	void move(int _LX,int _LY,int Direction){
		LX = _LX;
		LY = _LY;
		switch(Direction){
		case 0 : 
			LY--;
			break;
		case 1 : 
			LY++;
			break;
		case 2 :
			LX--;
			break;
		case 3 : 
			LX++;
			break;
		}
	}
	
	void ammoPain(){
		switch(Direction){
		case 0 :
			g.setColor(Color.red);
			g.fillRect(wide+wide*(LX+1)+LX+1, 40+height*LY+LY, wide, height);
			break;
		case 1 :
			g.setColor(Color.red);
			g.fillRect(wide+wide*(LX+1)+LX+1, 40+height*(LY+2)+LY+2, wide, height);
			break;
		case 2 :
			g.setColor(Color.red);
			g.fillRect(wide+wide*(LX)+LX, 40+height*(LY+1)+(LY+1), wide, height);
			break;
		case 3 :
			g.setColor(Color.red);
			g.fillRect(wide+wide*(LX+2)+LX+2, 40+height*(LY+1)+LY+1, wide, height);
			break;
		}
	}
}

class ammoThread extends Thread{
	Ammo ammo = null;
	MyFrame mf = null;
	int Direction = 0;
	BasicTank bt = null;
	ammoThread(Ammo _ammo,MyFrame mf,BasicTank bt){
		ammo = _ammo;
		Direction = ammo.Direction;
		this.mf = mf;
		this.bt = bt;
	}
	public void run(){
		switch(Direction)
		{
		case 0 :
				while(true){
					if(MAP.map[ammo.LX+1][ammo.LY] == 0)
					{
				            try{
				                sleep(100); //线程休眠600ms
				                }catch(InterruptedException e){}
						ammo.move(ammo.LX,ammo.LY,Direction);
						mf.repaint();
					}
				else
				{
					
					for(int i = 0;i enemy = new Vector(0,1);
}


效果图:


敌方坦克开炮还没有实现,主要是在写就没意思了.

这个程序是在学习java两个星期后写出来的,一共用了3天的时间.感觉不太满意,主要是该程序的结构不好,可扩展性较差!希望在以后的学习中能不断提高自己架构程序的能力.


你可能感兴趣的:(我的小项目)