黑馬_blog2_坦克大戰遊戲代碼

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com"target="blank">.Net培訓</a>、期待與您交流! ----------------------

坦克大戰遊戲思路及代碼:

/**
 * 1.畫出坦克
 * 2.坦克能移動
 * 3.畫出3個敵人的坦克
 * 4.我的坦克能夠發射一個子彈,並且碰到邊緣的時候停止,子彈可以連發(最多5顆子彈)
 * 5.我方坦克集中敵人的坦克時,敵人的坦克就消失,做出爆炸效果最好
 * 6.讓敵人的坦克可以自由隨機的上下左右移動 
 * 7.控制敵人坦克在一定範圍內移動
 * 8.讓敵人的坦克也能發射子彈
 * 9.當敵人的坦克擊中我的坦克時,我的坦克就爆炸
 * 10.可以分關
 * 11.可以玩遊戲的時候暫停和繼續
 *    將坦克和子彈的速度設爲0,並且坦克的方向不變化
 * 12.可以記錄玩家的成績
 *    先保存共擊毀多少坦克
 *    可以記錄敵人坦克座標並可以恢復
 * 13.java對聲音的處理
 */
import java.awt.*;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.*;

import java.awt.event.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("serial")
public class MyTankGame extends JFrame implements ActionListener{

	private MyPanel mp;
	private FirstPanel fp;
	private MenuBar mb;
	private Menu m;
	private MenuItem mi1,mi2,mi3,mi4;
	public MyTankGame()
	{
		//界面
		fp=new FirstPanel();
		new Thread(fp).start();
		this.add(fp);
		
		//菜單
		mb=new MenuBar();
		m=new Menu("菜單");
		mi1=new MenuItem("開始遊戲");
		mi2=new MenuItem("退出遊戲");
		mi3=new MenuItem("存盤退出");
		mi4=new MenuItem("續上一局");
		
		mi1.addActionListener(this);
		mi1.setActionCommand("開始遊戲");
		
		mi2.addActionListener(this);
		mi2.setActionCommand("退出遊戲");
		
		mi3.addActionListener(this);
		mi3.setActionCommand("存盤退出");
		
		mi4.addActionListener(this);
		mi4.setActionCommand("續上一局");
		
		m.add(mi1);
		m.add(mi2);
		m.add(mi3);
		m.add(mi4);
		mb.add(m);
		this.setMenuBar(mb);
		//
		this.setSize(600,500);
		this.setLocation(300,300);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyTankGame();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("開始遊戲"))
		{
			mp=new MyPanel("newgame");
			new Thread(mp).start();
			this.addKeyListener(mp);
			this.setSize(600,500);
			//先移除原來的,再增加現在的
			this.remove(fp);	
			this.add(mp);
			//設置可見爲true
			this.setVisible(true);
		}
		else if(e.getActionCommand().equals("退出遊戲"))
		{
			//保存之前的存儲記錄,再退出
			Recorder.keepRecording();
			System.exit(0);
		}
		else if(e.getActionCommand().equals("存盤退出"))
		{
			Recorder rd=new Recorder();
			rd.setEts(mp.ets);
			rd.keepEnemyRecording();
			System.exit(0);
		}
		else if(e.getActionCommand().equals("續上一局"))
		{
			mp=new MyPanel("lastgame");
			new Thread(mp).start();
			this.addKeyListener(mp);
			this.setSize(600,500);
			//先移除原來的,再增加現在的
			this.remove(fp);	
			this.add(mp);
			//設置可見爲true
			this.setVisible(true);
		}
	}
}

//播放聲音的類
class AePlayWave extends Thread 
{
	private String filename;
	public AePlayWave(String wavfile) 
	{
		filename = wavfile;
	}
	public void run() 
	{
		File soundFile = new File(filename);
		AudioInputStream audioInputStream = null;
		try 
		{
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		} catch (Exception e1) 
		{
			e1.printStackTrace();
			return;
		}
		AudioFormat format = audioInputStream.getFormat();
		SourceDataLine auline = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		
		try 
		{
			auline = (SourceDataLine) AudioSystem.getLine(info);
			auline.open(format);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		auline.start();
		int nBytesRead = 0;
		//這是緩衝
		byte[] abData = new byte[512];

		try 
		{
			while (nBytesRead != -1) 
			{
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0)
					auline.write(abData, 0, nBytesRead);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
			return;
		} finally 
		{
			auline.drain();
			auline.close();
		}
	}	
}

//節點類,將從硬盤中讀出的敵人的坦克座標和方向放到節點中保存
class Node
{
	int x;
	int y;
	int direct;
	public Node(int x,int y,int direct)
	{
		this.x=x;
		this.y=y;
		this.direct=direct;
	}
}
//記錄類
class Recorder
{  
	private static int enNum=20;	
	private static int myNum=3;
	private static int allTankNum=0;
	
	private static FileWriter fw=null;
	private static BufferedWriter bw=null;
	private static FileReader fr=null;
	private static BufferedReader br=null;
	
	private Vector<EnemyTank> ets=new Vector<EnemyTank>();
	private Vector<Node> nodes=new Vector<Node>();
	public Vector<EnemyTank> getEts() {
		return ets;
	}
	public void setEts(Vector<EnemyTank> ets) {
		this.ets = ets;
	}
	public static int getEnNum() {
		return enNum;
	}
	public static void setEnNum(int enNum) {
		Recorder.enNum = enNum;
	}
	public static int getMyNum() {
		return myNum;
	}
	public static void setMyNum(int myNum) {
		Recorder.myNum = myNum;
	}
	public static int getAllTankNum() {
		return allTankNum;
	}
	
	public static void setAllTankNum(int allTankNum) {
		Recorder.allTankNum = allTankNum;
	}
	public static void reduceEnNum()
	{
		enNum--;
	}
	public static void reduceMyNum()
	{
		myNum--;
	}
	public static void addAllTankNum()
	{
		allTankNum++;
	}
	//將坦克的座標和方向寫入硬盤中
	public  void keepEnemyRecording()
	{
		try
		{
			fw=new FileWriter("Recorder.txt");
			bw=new BufferedWriter(fw);
			bw.write(allTankNum+"\r\n");
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et=ets.get(i);
				String n=et.getX()+" "+et.getY()+" "+et.getDirect()+"\r\n";
				bw.write(n);
			}
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(bw!=null)
					bw.close();
			}catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
	}
	//將坦克的位置和方向讀入到內存中
	public Vector<Node> getEnemyRecording()
	{
		try
		{
			fr=new FileReader("Recorder.txt");
			br=new BufferedReader(fr);
			allTankNum=Integer.parseInt(br.readLine());
			String n="";
			while((n=br.readLine())!=null)
			{
				String [] xyz=n.split(" ");
				Node node=new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1]),Integer.parseInt(xyz[2]));
				nodes.add(node);
			}
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(br!=null)
				br.close();
			}catch(IOException e)
			{
				e.printStackTrace();
			}
			
		}
		return nodes;
	}
	//從硬盤中將坦克數量讀入到內存中
	public static void getRecording()
	{
		try
		{
			File f=new File("Recorder.txt");
			if(!f.exists())
			{
				allTankNum=0;
			}
			else
			{
				fr=new FileReader(f);
			    br=new BufferedReader(fr);
				allTankNum=Integer.parseInt(br.readLine());
			}
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(br!=null)
				{
					br.close();
			    }
			}catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
	}
	//將擊毀的坦克數量存到硬盤中
	public static void keepRecording()
	{
		try
		{
			fw=new FileWriter("recorder.txt");
			bw=new BufferedWriter(fw);
			bw.write(allTankNum+"\r\n");
			
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(bw!=null)
				{
					bw.close();
				}
			}catch(IOException e)
			{
				e.printStackTrace();
			}
		}
	}

}
//炸彈類
class Bomb
{
	int x;
	int y;
	int life=6;
	boolean isAlive=true;
	public Bomb(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	public void lifeDown()
	{
		if(life>0)
		{
			life--;
		}
		else
		{
			this.isAlive=false;
		}
	}
}
//子彈類
class Shot implements Runnable
{
	int x;
	int y;
	int direct;
	int speed=5;
	boolean isAlive=true;
	public Shot(int x,int y,int direct)
	{
		this.x=x;
		this.y=y;
		this.direct=direct;
	}
	public void run()
	{
		while(true)
		{
			try
			{
				Thread.sleep(50);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			switch(this.direct)
			{
			case 0:
				y-=speed;
				break;
			case 1:
				x+=speed;
				break;
			case 2:
				y+=speed;
				break;
			case 3:
				x-=speed;
				break;
			}
			if(this.x<0||this.x>400||this.y<0||this.y>300)
			{
				this.isAlive=false;
				break;
			}
		}
	}
}
//坦克類
class Tank
{
	int x;	
	int y;	
	int direct=0;
	int speed=1;
	Color color;
	boolean isAlive=true;
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public int getDirect() {
		return direct;
	}
	public void setDirect(int direct) {
		this.direct = direct;
	}
	public int getSpeed() {
		return speed;
	}
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Tank(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	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;
	}
}
//我的坦克
class Hero extends Tank
{
	Vector<Shot> ss=new Vector<Shot>();
	Shot s;
	public Hero(int x,int y)
	{
		super(x,y);
	}
	public void moveUp()
	{
		this.direct=0;
		this.y-=this.speed;
	}
	public void moveRight()
	{
		this.direct=1;
		this.x+=this.speed;
	}
	public void moveDown()
	{
		this.direct=2;
		this.y+=this.speed;
	}
	public void moveLeft()
	{
		this.direct=3;
		this.x-=this.speed;
	}
	public void shotEnemy()
	{
		  switch(this.direct)
		  {
		  case 0:
			  s=new Shot(this.x,this.y-15,0);
			  break;
		  case 1:
			  s=new Shot(this.x+15,this.y,1);
			  break;
		  case 2:
			  s=new Shot(this.x,this.y+15,2);
			  break;
		  case 3:
			  s=new Shot(this.x-15,this.y,3);
			  break;  
		  }
		  ss.add(s);
		  new Thread(s).start();
	}
}
//敵人的坦克
class EnemyTank extends Tank implements Runnable
{
	Vector<Shot> ss=new Vector<Shot>();
	Shot s=null;
	int time=1;
	Vector<EnemyTank> ets=new Vector<EnemyTank>();
	public EnemyTank(int x,int y)
	{
		super(x,y);
	}
	public void setEts(Vector<EnemyTank> ets)
	{
		this.ets=ets;
	}
	//判斷敵人的坦克是否重疊
	public boolean isTouched()
	{
		boolean b=false;
		switch(this.direct)
		{
		case 0:
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et=ets.get(i);
				if(et!=this)
				{
					if(et.direct==0||et.direct==2)
					{
						if(this.x-10<=et.x+10&&this.x-10>=et.x-10&&this.y-15<=et.y+15&&this.y-15>=et.y-15)
						{
							return true;
						}
						if(this.x+10<=et.x+10&&this.x+10>=et.x-10&&this.y-15<=et.y+15&&this.y-15>=et.y-15)
						{
							return true;
						}
					}
					if(et.direct==1||et.direct==3)
					{
						if(this.x-10<=et.x+15&&this.x-10>=et.x-15&&this.y-15<=et.y+10&&this.y-15>=et.y-10)
						{
							return true;
						}
						if(this.x+10<=et.x+15&&this.x+10>=et.x-15&&this.y-15<=et.y+15&&this.y-10>=et.y-10)
						{
							return true;
						}
					}
				}
			}
			break;
		case 1:
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et=ets.get(i);
				if(et!=this)
				{
					if(et.direct==0||et.direct==2)
					{
						if(this.x+15<=et.x+10&&this.x+15>=et.x-10&&this.y-10<=et.y+15&&this.y-10>=et.y-15)
						{
							return true;
						}
						if(this.x+15<=et.x+10&&this.x+15>=et.x-10&&this.y+10<=et.y+15&&this.y+10>=et.y-15)
						{
							return true;
						}
					}
					if(et.direct==1||et.direct==3)
					{
						if(this.x+15<=et.x+15&&this.x+15>=et.x-15&&this.y-10<=et.y+10&&this.y-10>=et.y-10)
						{
							return true;
						}
						if(this.x+15<=et.x+15&&this.x+15>=et.x-15&&this.y+10<=et.y+10&&this.y+10>=et.y-10)
						{
							return true;
						}
					}
				}
			}
			break;
		case 2:
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et=ets.get(i);
				if(et!=this)
				{
					if(et.direct==0||et.direct==2)
					{
						if(this.x-10<=et.x+10&&this.x-10>=et.x-10&&this.y+15<=et.y+15&&this.y+15>=et.y-15)
						{
							return true;
						}
						if(this.x+10<=et.x+10&&this.x+10>=et.x-10&&this.y+15<=et.y+15&&this.y+15>=et.y-15)
						{
							return true;
						}
					}
					if(et.direct==1||et.direct==3)
					{
						if(this.x-10<=et.x+15&&this.x-10>=et.x-15&&this.y+15<=et.y+10&&this.y+15>=et.y-10)
						{
							return true;
						}
						if(this.x+10<=et.x+15&&this.x+10>=et.x-15&&this.y+15<=et.y+10&&this.y+15>=et.y-10)
						{
							return true;
						}
					}
				}
			}
			break;
		case 3:
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et=ets.get(i);
				if(et!=this)
				{
					if(et.direct==0||et.direct==2)
					{
						if(this.x-15<=et.x+10&&this.x-15>=et.x-10&&this.y-10<=et.y+15&&this.y-10>=et.y-15)
						{
							return true;
						}
						if(this.x-15<=et.x+10&&this.x-15>=et.x-10&&this.y+10<=et.y+15&&this.y+10>=et.y-15)
						{
							return true;
						}
					}
					if(et.direct==1||et.direct==3)
					{
						if(this.x-15<=et.x+15&&this.x-15>=et.x-15&&this.y-10<=et.y+10&&this.y-10>=et.y-10)
						{
							return true;
						}
						if(this.x-15<=et.x+15&&this.x-15>=et.x-15&&this.y+10<=et.y+10&&this.y+10>=et.y-10)
						{
							return true;
						}
					}
				}
			}
			break;
		}
		return b;
	}
	//敵人的坦克是一個線程
	public void run()
	{
		while(true)
		{
			switch(this.direct)
			{
			case 0:
				for(int i=0;i<30;i++)
				{
					if(y>16&&!this.isTouched())
						y-=speed;
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
			case 1:
				for(int i=0;i<30;i++)
				{
					if(x<384&&!this.isTouched())
						x+=speed;
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
			case 2:
				for(int i=0;i<30;i++)
				{
					if(y<286&&!this.isTouched())
						y+=speed;
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
			case 3:
				for(int i=0;i<30;i++)
				{
					if(x>16&&!this.isTouched())
						x-=speed;
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
			}
			//隨機生成敵人的坦克的方向
			this.direct=(int)(Math.random()*4);
			if(this.isAlive==false)
			{
				break;
			}		
			time++;
			if(time%2==0)
			{
				if(this.isAlive==true)
				{
					if(ss.size()<5)
					{
						switch(this.direct)
						{
						case 0:
							s=new Shot(this.x,this.y-15,0);
							break;
						case 1:
							s=new Shot(this.x+15,this.y,1);
							break;
						case 2:
							s=new Shot(this.x,this.y+15,2);
							break;
						case 3:
							s=new Shot(this.x-15,this.y,3);
							break;
						}
						this.ss.add(s);
						new Thread(s).start();
					}
				}
			}
		}
	}
}
//第一個面板,文字閃爍的效果,做成線程
@SuppressWarnings("serial")
class FirstPanel extends JPanel  implements Runnable
{
	int time=0;
	public void paint(Graphics g)
	{
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		if(time%2==0)
		{
			g.setFont(new Font("華文舒體",Font.PLAIN,20));
			g.setColor(Color.cyan);
			g.drawString("Stage 1", 150, 150);
		}
	}
	public void run()
	{
		while(true)
		{
			try {
				Thread.sleep(400);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			time++;
			this.repaint();
		}
	}
}
//我的面板
@SuppressWarnings("serial")
class MyPanel extends JPanel implements KeyListener,Runnable
{
	Hero hero;
	Vector<EnemyTank> ets;
	Vector<Bomb> bombs=new Vector<Bomb>();
	Vector<Node> nodes=new Vector<Node>();
	Image image1=null;
	Image image2=null;
	Image image3=null;
	public MyPanel(String flag)
	{
		Recorder.getRecording();
		
		hero=new Hero(80,200);
		ets=new Vector<EnemyTank>();
		int etsize=4;
		//根據構造函數傳入的字符串判斷是開始遊戲,還是續上一局
		if(flag.equals("newgame"))
		{
			for(int i=0;i<etsize;i++)
			{
				EnemyTank et=new EnemyTank(50*i+50,50);
				et.setColor(Color.yellow);
				et.setDirect(2);
				new Thread(et).start();
				ets.add(et);
			}
		}
		else
		{
			nodes=new Recorder().getEnemyRecording();
			for(int i=0;i<nodes.size();i++)
			{
				EnemyTank et=new EnemyTank(nodes.get(i).x,nodes.get(i).y);
				et.setColor(Color.yellow);
				et.setDirect(nodes.get(i).direct);
				new Thread(et).start();
				ets.add(et);
			}
		}
		//播放聲音
		AePlayWave aw=new AePlayWave("111.wav");
		aw.start();
		//炸彈爆炸效果,三個圖片的切換
		try {
			image1=ImageIO.read(new File("bomb_1.gif"));
			image2=ImageIO.read(new File("bomb_2.gif"));
			image3=ImageIO.read(new File("bomb_3.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//顯示玩家的成績
	public void showResult(Graphics g)
	{
		this.drawTank(50, 370, g, 0, 1);
		g.setColor(Color.BLACK);
		g.drawString(Recorder.getEnNum()+"", 70, 375);
		
		this.drawTank(130, 370, g, 0, 0);
		g.setColor(Color.BLACK);
		g.drawString(Recorder.getMyNum()+"", 150, 375);
		
		g.setFont(new Font("宋體",Font.BOLD,20));
		g.drawString("您的總成績", 420, 60);
		this.drawTank(450, 100, g, 0, 1);
		g.setColor(Color.BLACK);
		g.setFont(new Font("宋體",Font.PLAIN,13));
		g.drawString(Recorder.getAllTankNum()+"", 470, 105);
	}
	public void paint(Graphics g)
	{
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		
		this.showResult(g);
		//畫出我的坦克 
		if(hero.isAlive)
		{
			drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0);
		}
		//畫出子彈
		if(hero.isAlive)
		{
		    for(int i=0;i<hero.ss.size();i++)
			{
				if(hero.ss.get(i)!=null&&hero.ss.get(i).isAlive==true)
				{
					g.fillRect(hero.ss.get(i).x, hero.ss.get(i).y, 2, 2);
				}
				if(hero.ss.get(i).isAlive==false)
				{
					hero.ss.remove(hero.ss.get(i));
				}
			}
		}
		//畫出敵人的坦克
		for(int i=0;i<ets.size();i++)
		{
			EnemyTank et=ets.get(i);
			if(et.isAlive)
			{
				this.drawTank(et.getX(), et.getY(), g, et.getDirect(), 1);
				//令敵人的坦克知道所有的坦克的信息
				et.setEts(ets);
				//畫出子彈
				
				for(int j=0;j<et.ss.size();j++)
				{				
					Shot s=et.ss.get(j);
					if(s.isAlive)
					{
						g.fillRect(s.x, s.y, 2, 2);
					}
					else
					{
						et.ss.remove(s);
					}
				}
			}
				
		}
		//畫出炸彈
		for(int i=0;i<bombs.size();i++)
		{
			Bomb b=bombs.get(i);
			if(b.life>4) 
			{
				g.drawImage(image1, b.x-13, b.y-13, 30, 30, this);
			}
			else if(b.life>2)
			{
				g.drawImage(image2, b.x-13, b.y-13, 30, 30, this);
			}
			else 
			{
				g.drawImage(image3, b.x-13, b.y-13, 30, 30, this);
			}
			b.lifeDown();	
			if(b.isAlive==false) 
			{
				bombs.remove(b);
			}
		}
	}
	//判斷我的子彈是否擊中敵人的坦克
	public void hitEnemyTank()
	{
		for(int i=0;i<this.hero.ss.size();i++)
		{
			if(this.hero.ss.get(i).isAlive)
			{
				for(int j=0;j<ets.size();j++)
				{
					if(ets.get(j).isAlive)
					{
						if(this.hitTank(hero.ss.get(i), ets.get(j)))
						{
							Recorder.reduceEnNum();
							Recorder.addAllTankNum();
						}
					}
					else
					{
						ets.remove(j);
					}
				}
			}
		}
	}
	//判斷敵人的子彈是否擊中我的坦克
	public void hitMe()
	{
		for(int i=0;i<ets.size();i++)
		{
			EnemyTank et=ets.get(i);
			for(int j=0;j<et.ss.size();j++)
			{
				Shot s=et.ss.get(j);
				if(hero.isAlive)
				{
					if(this.hitTank(s, hero))
					{
						Recorder.reduceMyNum();
					}
				}
				
			}
		}
	}
	//判斷子彈是否擊中坦克
	public boolean hitTank(Shot s,Tank t)
	{
		boolean b=false;
		 switch(t.direct)
		 {
		 case 0:
		 case 2:
			 if(s.x>t.x-10&&s.x<t.x+10&&s.y>t.y-15&&s.y<t.y+15)
			 {
				 s.isAlive=false;
				 t.isAlive=false;
				 b=true;
				 
				 bombs.add(new Bomb(t.x,t.y));
			 }
			 break;
		 case 1:
		 case 3:
			 if(s.y>t.y-10&&s.y<t.y+10&&s.x>t.x-15&&s.x<t.x+15)
			 {
				 s.isAlive=false;
				 t.isAlive=false;
				 b=true;
				 bombs.add(new Bomb(t.x,t.y));
			 }
			 break;
		 }
		 return b;
	}
	//畫出坦克
	public void drawTank(int x,int y,Graphics g,int direct,int type)
	{
		switch(type)
		{
		case 0:
			g.setColor(Color.cyan);
			break;
		case 1:
			g.setColor(Color.pink);
			break;
			
		}
		switch(direct)
		{
		case 0:
			g.fill3DRect(x-10, y-15, 5, 30, false);
			g.fill3DRect(x+5, y-15, 5, 30, false);
			g.fill3DRect(x-5, y-10, 10, 20, false);
			g.fillOval(x-6, y-5, 10, 10);
			g.drawLine(x, y, x, y-15);
			break;
		case 1:
			g.fill3DRect(x-15, y-10,30 , 5, false);
			g.fill3DRect(x-15, y+5, 30, 5, false);
			g.fill3DRect(x-10, y-5, 20, 10, false);
			g.fillOval(x-5, y-6, 10, 10);
			g.drawLine(x, y, x+15, y);
			break;
		case 2:
			g.fill3DRect(x-10, y-15, 5, 30, false);
			g.fill3DRect(x+5, y-15, 5, 30, false);
			g.fill3DRect(x-5, y-10, 10, 20, false);
			g.fillOval(x-6, y-5, 10, 10);
			g.drawLine(x, y, x, y+15);
			break;
		case 3:
			g.fill3DRect(x-15, y-10,30 , 5, false);
			g.fill3DRect(x-15, y+5, 30, 5, false);
			g.fill3DRect(x-10, y-5, 20, 10, false);
			g.fillOval(x-5, y-6, 10, 10);
			g.drawLine(x, y, x-15, y);
			break;
		}
	}
	//事件處理
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_UP)
		{
			hero.moveUp();
		}
		else if(e.getKeyCode()==KeyEvent.VK_DOWN)
		{
			hero.moveDown();
		}
		else if(e.getKeyCode()==KeyEvent.VK_LEFT)
		{
			hero.moveLeft();
		}
		else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
		{
			hero.moveRight();
		}
		if(e.getKeyCode()==KeyEvent.VK_SPACE)
		{
			if(this.hero.ss.size()<=4)
				this.hero.shotEnemy();
		}
		this.repaint();
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
	}
	//MyPanel是一個線程
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true)
		{
			try
			{
				Thread.sleep(50);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			//判斷我的子彈是否擊中敵人的坦克
			this.hitEnemyTank();
			//判斷敵人的子彈是否擊中我的坦克
			this.hitMe();
			//刷新
			this.repaint();
		}
	}
}


運行結果截圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章