坦克大戰源碼

package com.test1;

import java.util.Vector;

 class EnemyTank extends tank implements Runnable
 {
	 
	 public Vector<EnemyTank> getEts() {
		return ets;
	}
	public void setEts(Vector<EnemyTank> ets) {
		this.ets = ets;
	}


	Vector<EnemyTank> ets=new Vector<EnemyTank>();
	 Vector<Shot> ss=new Vector<Shot>(); 
	 
	 int times=0;
	 
	//構造函數
	public EnemyTank(int x,int y,int type) 
	{
		super(x, y,type);
	}
	//得到坦克位置
	public void dedaoentank(Vector<EnemyTank> vv)
	{
		this.ets=vv;
	}
	public boolean   Pudao()
	{
		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==1)
					{
						//判斷敵人當前坦克左輪與同伴坦克(向上或向下)的位置比較
						if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
						{
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
						{
							return true;
						}
					}
					//如果敵人同伴的坦克向左或向右
					if(et.direct==2||et.direct==3)
					{
						//判斷敵人當前坦克右輪與同伴坦克(向左或向右)的位置比較
						if(this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
						{
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
						{
							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==1)
					{
						if(this.x>=et.x&&this.x<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)
						{
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)
						{
							return true;
						}
					}
					if(et.direct==2||et.direct==3)
					{
						if(this.x>=et.x&&this.x<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
						{
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
						{
							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==1)
					{
						if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
						{
							return true;
						}
						if(this.x>=et.x&&this.x<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
						{
							return true;
						}
					}
					if(et.direct==2||et.direct==3)
					{
						if(this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
						{
							return true;
						}
						if(this.x>=et.x&&this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
						{
							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==1)
					{
						if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
						{
							return true;
						}
						if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
						{
							return true;
						}
					}
					if(et.direct==2||et.direct==3)
					{
						if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
						{
							return true;
						}
						if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
						{
							return true;
						}
					}
				}
			}
			break;
		}
		return b;
	}


	@Override
	public void run() 
	{
      while(true)
      {
	switch (this.direct)
	{
	case 0:
		for(int i=0;i<=50;i++)
		{
			if(y>0&&!this.Pudao())
			{
			y-=speed;
			}
			try 
			{
				Thread.sleep(50);
			}
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		break;
	case 1:
		for(int i=0;i<=50;i++)
		{
			if(x<500&&!this.Pudao())
			{
		          x+=speed;
		    }
		try {
			Thread.sleep(50);
		} catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	break;
	case 2:
		for(int i=0;i<=50;i++)
		{
			if(y<500&&!this.Pudao())
			{
		        y+=speed;
		        }
		try 
		{
			Thread.sleep(50);
		} catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	break;
	case 3:
		for(int i=0;i<=50;i++)
		{
			if(x>0&&!this.Pudao())
			{
		x-=speed;
		}
		try 
		{
			Thread.sleep(50);
		} catch (InterruptedException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	break;
	}
	this.times++;
	if(times%2==0)
	{
		if(islive)
		{
			if(ss.size()<5)
			{
				Shot s=null;
				//沒有子彈,添加子彈
				switch(direct)
				
				{
				case 0:
					s=new Shot(x+10, y, 0);
					ss.add(s);
					break;
				case 1:
					s=new Shot(x+10, y+29, 1);
					ss.add(s);
					break;
				case 2:
					s=new Shot(x, y+10, 2);
					ss.add(s);
					break;
				case 3:
					s=new Shot(x+29, y+10, 3);
					ss.add(s);
					break;
				}
				//啓動敵人子彈線程
				Thread t=new Thread(s);
				t.start();
			}
		}
	} 
	//隨機生成方向
	this.direct=(int)(Math.random()*4);
	if(this.islive==false)
	{
		break;
	}
	
    }
	}
	
}


package com.test1;

import java.util.Vector;

class Hero extends tank
{
	Vector<Shot> ss=new Vector<Shot>();
	Shot s=null;
	int speed=5;
	//構造方法
	public Hero(int x,int y,int type) 
	{
		super(x, y,type);
	}
public void moveup() 
{
	y-=speed;
}
public void movedown()
{
	y+=speed;
}
public void moveright() 
{
	x+=speed;
}
public void moveleft() 
{
	x-=speed;
}

public void Shotem()
{
	
	switch (direct)
	{
	
case 0://向上
	//創建一顆子彈
	s=new Shot(x+10, y, 0);
	//將子彈加入到向量集中
	ss.add(s);
	break;
case 1://向下
	s=new Shot(x+10, y+29, 1);
	ss.add(s);
	break;
case 2://向左
	s=new Shot(x, y+10, 2);
	ss.add(s);
	break;
case 3://向右
	s=new Shot(x+29, y+10, 3);
	ss.add(s);
	break;
}

//啓動子彈線程

Thread t=new Thread(s);
t.start();

}
}

package com.test1;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.Vector;


import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;


@SuppressWarnings("serial")

public class MyTank extends JFrame implements ActionListener {

// 定義組件

jpanel mp = null;

// 定義一個開始面板

MyStartPanel msp = null;

// 做出菜單

JMenuBar jmb = null;

// 開始遊戲

JMenu jm1 = null;

JMenuItem jmi1 = null;

// 退出遊戲

JMenuItem jmi2 = null;

// 存盤退出

JMenuItem jmi3 = null;

// 接上局

JMenuItem jmi4 = null;


// 構造函數

public MyTank() {


jmb = new JMenuBar();

jm1 = new JMenu("遊戲(G)");

// 設置快捷方式

jm1.setMnemonic('G');

jmi1 = new JMenuItem("開始新遊戲(N)");

jmi1.setMnemonic('N');

// 對jmi1相應

jmi1.addActionListener(this);

jmi1.setActionCommand("newgame");


jmi2 = new JMenuItem("退出遊戲(E)");

jmi2.setMnemonic('E');

jmi2.addActionListener(this);

jmi2.setActionCommand("exit");


jmi3 = new JMenuItem("存盤退出(S)");

jmi3.setMnemonic('S');

jmi3.addActionListener(this);

jmi3.setActionCommand("save");


jmi4 = new JMenuItem("繼續遊戲(C)");

jmi4.setMnemonic('C');

jmi4.addActionListener(this);

jmi4.setActionCommand("congame");


jm1.add(jmi1);

jm1.add(jmi2);

jm1.add(jmi3);

jm1.add(jmi4);

jmb.add(jm1);

this.setJMenuBar(jmb);


msp = new MyStartPanel();

Thread t = new Thread(msp);

t.start();

// 添加組件

this.add(msp);

// 設置窗體

this.setTitle("坦克大戰");// 窗體標籤

this.setSize(600, 600);

this.setLocationRelativeTo(null);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}


public void actionPerformed(ActionEvent e) {

// 對用戶不同的點擊,做出不同的處理

if (e.getActionCommand().equals("newgame")) {

// 創建戰場面板

mp = new jpanel("newgame");

// 啓動mp線程

Thread ta = new Thread(mp);

ta.start();

// 先刪除舊的開始面板

this.remove(msp);

// 加入組件

this.add(mp);

// 監聽

this.addKeyListener(mp);

// 顯示,刷新JFrame

this.setVisible(true);

} else if (e.getActionCommand().equals("exit")) {

// 用戶點擊了退出菜單

// 保存用戶遊戲記錄數據

Record.keepRecorder();

System.exit(0);

} else if (e.getActionCommand().equals("save")) {

// 用戶點擊保存菜單

// 保存擊毀敵人的數量和敵人的座標

Record.setEts(mp.ets);

Record.keepRecAndEnemyTank();

Record.keepRecorder();

System.exit(0);

} else if (e.getActionCommand().equals("congame")) {

// 繼續上次遊戲

// 創建戰場面板

mp = new jpanel("congame");


// 啓動mp線程

Thread ta = new Thread(mp);

ta.start();

// 先刪除舊的開始面板

this.remove(msp);

// 加入組件

this.add(mp);

// 監聽

this.addKeyListener(mp);

// 顯示,刷新JFrame

this.setVisible(true);

}


}


@SuppressWarnings("unused")

public static void main(String[] args) {


MyTank win = new MyTank();


}

}


// 開始面板--起提示作用

@SuppressWarnings("serial")

class MyStartPanel extends JPanel implements Runnable {

int times = 0;


public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.black);

g.fillRect(0, 0, 500, 500);

// 提示信息

if (times % 2 == 0) {

g.setColor(Color.red);

// 開關信息的字體

Font myFont = new Font("宋體", Font.CENTER_BASELINE, 30);

g.setFont(myFont);

g.drawString("Stage: 1", 200, 250);

}

}


public void run() {

while (true) {

// 休眠

try {

Thread.sleep(500);

} catch (Exception e) {

e.printStackTrace();

}


times++;

// 重畫

this.repaint();

}

}

}


@SuppressWarnings("serial")

class jpanel extends JPanel implements KeyListener, Runnable {

// 創建英雄

Hero hero = null;

Vector<EnemyTank> ets = new Vector<EnemyTank>();

Vector<Node> nodes=new Vector<Node>();

int enside = 3;


// 構造函數

@SuppressWarnings("static-access")

public jpanel(String flag) {

Record.GetRecord();

// 初始化位置

hero = new Hero(10, 10, 2);

if(flag.equals("newgame")){

// 初始化敵人坦克

for (int i = 0; i < enside; i++) {

EnemyTank et = new EnemyTank((i + 1) * 50, 0, 1);

et.dedaoentank(ets);

Thread t1 = new Thread(et);

t1.start();

// 子彈

Shot enshot = new Shot(et.x + 10, et.y + 10, 2);

et.ss.add(enshot);

Thread t2 = new Thread(enshot);

t2.start();

ets.add(et);

}

}

else if(flag.equals("congame")){

nodes=new Record().getNodesAndEnNums();

//初始化敵人的坦克

for(int i=0;i<nodes.size();i++){

Node node=nodes.get(i);

//創建一輛敵人的坦克對象

EnemyTank et=new EnemyTank(node.x, node.y,node.direct);

//將MyPanel的敵人向量交給該敵人坦克

et.setEts(ets);

et.setType(1);

//啓動敵人坦克進程

Thread t=new Thread(et);

t.start();

//給敵人坦克添加一顆子彈

Shot s=new Shot(et.x+10, et.y+30, 1);

//把子彈加入給敵人

et.ss.add(s);

Thread t2=new Thread(s);

t2.start();

//加入敵人坦克

ets.add(et);

}

}

}


// 繪製

public void paint(Graphics g) {

super.paint(g);

g.fillRect(0, 0, 530, 530);

// 畫出提示信息

ShowInfo(g);

// 畫出我的坦克

if (hero.islive) {

this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct,this.hero.type);

}

// 畫出敵人坦克

for (int i = 0; i <ets.size(); i++) {

EnemyTank et = ets.get(i);


if (et.islive) {


this.drawTank(et.getX(), et.getY(), g, et.direct, et.type);


for (int j = 0; j < et.ss.size(); j++) {

Shot enShot = et.ss.get(j);


if (enShot.isLive) {

g.draw3DRect(enShot.x, enShot.y, 1, 1, false);

} else {

// 如果敵人的坦克死亡了就從向量Vector中去掉

et.ss.remove(enShot);

}

}

}

}

// 畫出子彈

for (int i = 0; i < hero.ss.size(); i++) {

Shot myShot = hero.ss.get(i);


if (myShot != null && myShot.isLive == true) {

g.draw3DRect(myShot.x, myShot.y, 1, 1, false);

}

if (myShot.isLive == false) {

hero.ss.remove(myShot);

}

}

}


// 畫出提示坦克

public void ShowInfo(Graphics g) {

// 畫出提示坦克

this.drawTank(530, 500, g, 0, 1);

g.drawString(Record.getEnnum() + "", 550, 520);


this.drawTank(530, 460, g, 0, 2);

g.drawString(Record.getMynum() + "", 550, 480);


// 畫出玩家的總成績

g.setColor(Color.black);

g.setFont(new Font("宋體", Font.BOLD, 20));

g.drawString("成績", 530, 20);


this.drawTank(530, 30, g, 1, 0);

g.setColor(Color.black);

g.setFont(new Font("宋體", Font.BOLD, 20));

g.drawString(Record.getAllEnNum() + "", 560, 50);

}


// 打到我的坦克

public void hitmytank() {

for (int i = 0; i < this.ets.size(); i++) {

EnemyTank eTank = ets.get(i);


for (int j = 0; j < eTank.ss.size(); j++) {

Shot enShot = eTank.ss.get(j);


if (this.hittank(hero, enShot)) {

Record.reducemyNum();

}


}

}

}


// 毀滅敵人坦克

public void hitentank() {

for (int i = 0; i < hero.ss.size(); i++) {

Shot myShot = hero.ss.get(i);


if (myShot.isLive) {

for (int j = 0; j < ets.size(); j++) {

EnemyTank et = ets.get(j);


if (et.islive) {

if (this.hittank(et, myShot)) {

Record.reduceEnNum();

Record.addEnNumRec();

}

}

}

}

}

}


public boolean hittank(tank et, Shot s) {

boolean b = false;

switch (et.direct) {

case 0:

case 2:

if (s.x > et.x && s.x < et.x + 20 && s.y > et.y && s.y < et.y + 30) {

s.isLive = false;

et.islive = false;

b = true;


}

break;

case 1:

case 3:

if (s.x > et.x && s.x < et.x + 30 && s.y > et.y && s.y < et.y + 20) {

s.isLive = false;

et.islive = false;

b = true;


}

break;

}

return b;

}


// keyPressed代表鍵被按下,控制函數方向

public void keyPressed(KeyEvent e) {

System.out.println("按下了" + e.getKeyChar() + "鍵");

if (e.getKeyCode() == KeyEvent.VK_DOWN) {

System.out.println("向下");

this.hero.setDirect(1);

this.hero.movedown();

} else if (e.getKeyCode() == KeyEvent.VK_UP) {

System.out.println("向上");

this.hero.setDirect(0);

this.hero.moveup();

} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {

System.out.println("向左");

this.hero.setDirect(2);

this.hero.moveleft();


} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

System.out.println("向右");

this.hero.setDirect(3);

this.hero.moveright();

}

if (e.getKeyCode() == KeyEvent.VK_SPACE) {

if (this.hero.ss.size() < 5) {

this.hero.Shotem();

}

}

// 調用repaint()函數,來重繪界面

this.repaint();

}


// 繪製坦克函數

public void drawTank(int x, int y, Graphics g, int direct, int type) {

// 設置類型

switch (type) {

case 1:

g.setColor(Color.cyan);

break;

case 0:

g.setColor(Color.red);

break;

case 2:

g.setColor(Color.yellow);

break;

}

// 設置方向

switch (direct) {

case 0:

g.fill3DRect(x, y, 5, 30, false);

g.fill3DRect(x + 15, y, 5, 30, false);

g.fill3DRect(x + 5, y + 5, 10, 20, false);

g.fillOval(x + 5, y + 10, 10, 10);

g.drawLine(x + 5, y + 15, x + 10, y);

break;

case 1:

g.fill3DRect(x, y, 5, 30, false);

g.fill3DRect(x + 15, y, 5, 30, false);

g.fill3DRect(x + 5, y + 5, 10, 20, false);

g.fillOval(x + 5, y + 10, 10, 10);

g.drawLine(x + 10, y + 15, x + 10, y + 29);

break;

case 2:

g.fill3DRect(x, y, 30, 5, false);

g.fill3DRect(x, y + 15, 30, 5, false);

g.fill3DRect(x + 5, y + 5, 20, 10, false);

g.fillOval(x + 10, y + 5, 10, 10);

g.drawLine(x + 15, y + 10, x, y + 10);


break;

case 3:

g.fill3DRect(x, y, 30, 5, false);

g.fill3DRect(x, y + 15, 30, 5, false);

g.fill3DRect(x + 5, y + 5, 20, 10, false);

g.fillOval(x + 10, y + 5, 10, 10);

g.drawLine(x + 15, y + 10, x + 29, y + 10);


break;

}

}


@Override

public void keyTyped(KeyEvent e) {

// TODO Auto-generated method stub


}


@Override

public void keyReleased(KeyEvent e) {

// TODO Auto-generated method stub


}


public void run() {

while (true) {

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

this.hitmytank();

this.hitentank();

this.repaint();

}


}

}



package com.test1;


public class Node {

int x;

int y;

int direct;


public Node(int x, int y, int direct) {

// TODO Auto-generated constructor stub

this.x = x;

this.y = y;

this.direct = direct;


}

}


package com.test1;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Vector;


class Record {


private static int ennum = 20;

private static int mynum = 3;

    private static int allEnNum = 0;

    private static FileReader fr = null;

private static FileWriter fw = null;

private static BufferedReader br = null;

private static BufferedWriter bw = null;


private static Vector<EnemyTank> ets = new Vector<EnemyTank>();


// 從文件中恢復記錄點

private static Vector<Node> nodes = new Vector<Node>();


public static Vector<EnemyTank> getEts() {

return ets;

}


public static void setEts(Vector<EnemyTank> ets) {

Record.ets = ets;

}


// 保存擊毀敵人的數量和敵人坦克座標、方向

public static void keepRecAndEnemyTank() {

try {

// 創建

fw = new FileWriter("e:\\tanksave1.txt");

bw = new BufferedWriter(fw);


// 保存當前還活着的敵人坦克座標、方向

for (int i = 0; i < ets.size(); i++) {

// 取出第一個坦克

EnemyTank et = ets.get(i);

if (et.islive) {

// 活的保存

String recode = et.getX() + " " + et.getY() + " "

+ et.getDirect();// 得到座標x,y和方向direct

// 寫入到文件

bw.write(recode + "\r\n");

}

}


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

// 關閉流,先開後關,後開先關

try {

bw.close();

fw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}


// 保存擊中坦克數量

public static void keepRecorder() {


try {

fw = new FileWriter("e:\\tanksave.txt");

bw = new BufferedWriter(fw);

bw.write(allEnNum + "");


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {


try {

bw.close();

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

//完成讀取任務點任務

public static Vector<Node> getNodesAndEnNums(){

try {

fr=new FileReader("e:\\tanksave1.txt");

br=new BufferedReader(fr);

String n;

while((n=br.readLine())!=null){

String []Recovery=n.split(" ");//split方法可以按一行字符中有多少個空間來返回數組

Node node=new Node(Integer.parseInt(Recovery[0]),Integer.parseInt(Recovery[1]),Integer.parseInt(Recovery[2]));

nodes.add(node);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

br.close();

fr.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

return nodes;

}

// 讀取記錄

public static void GetRecord() {

try {

fr = new FileReader("e:\\tanksave.txt");

br = new BufferedReader(fr);

String n=br.readLine();

// while (br.readLine()!=null)

// {

allEnNum = Integer.parseInt(n);

//}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

br.close();

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static int getAllEnNum() {

return allEnNum;

}


public static void setAllEnNum(int allEnNum) {

Record.allEnNum = allEnNum;

}



public static int getEnnum() {

return ennum;

}


public static void setEnnum(int ennum) {

Record.ennum = ennum;

}


public static int getMynum() {

return mynum;

}


public static void setMynum(int mynum) {

Record.mynum = mynum;

}


// 敵人坦克死亡就減少坦克數

public static void reduceEnNum() {

ennum--;

}


public static void reducemyNum() {

mynum--;

}


// 當消滅敵人的時候

public static void addEnNumRec() {

allEnNum++;

}

}


package com.test1;


class Shot implements Runnable {

int x;

int y;

// 速度

int speed = 1;

int direct;

boolean isLive = true;


public Shot(int x, int y, int direct) {

this.x = x;

this.y = y;

this.direct = direct;

}


public void run() {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

while (true) {

switch (direct) {

case 0:// 向上

y -= speed;

break;

case 1:// 向下

y += speed;

break;

case 2:// 向 左

x -= speed;

break;

case 3:// 向右

x += speed;

break;

}

if (x < 0 || x > 500 || y < 0 || y > 500) {

this.isLive = false;

break;

}

System.out.println("子彈座標(" + x + "," + y + ")");

}


}

}


package com.test1;


class tank {

int x;

int y;

Shot s = null;

// 坦克的速度

int speed = 1;

boolean islive = true;

// 設置類型

int type = 0;


// 0表示上2表示左

int direct = 0;


public int getType() {

return type;

}


public void setType(int type) {

this.type = type;

}


public int getSpeed() {

return speed;

}


public void setSpeed(int speed) {

this.speed = speed;

}


public int getDirect() {

return direct;

}


public void setDirect(int direct) {

this.direct = direct;

}


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;

}


public tank(int x, int y, int type) {

this.x = x;

this.y = y;

this.type = type;

}

}


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