標題:Java游戲開發之坦克大戰代碼
關鍵詞:Java坦克大戰,坦克大戰Java代碼
這是一個Java坦克大戰的源碼,實現了大部分功能,是學習Java繪圖技術的好例子
主類:
package com.qq.TankGame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
//
///**
// * 功能:java繪圖
// * @author Administrator
// *
// */
public class TankGame extends JFrame implements ActionListener{
MyPanel mp = null;
myStartPanel msp = null;
EnemyTask em = null;
JMenuBar jmb = null;
JMenu jm = null;
JMenuItem jmi1 = null;
JMenuItem jmi2 = null;
JMenuItem jmi3 = null;
public static void main(String[] args) {
TankGame tankGame = new TankGame();
}
public TankGame(){
jmb = new JMenuBar();
jm = new JMenu("游戲(G)");
jm.setMnemonic('G');
jmi1 = new JMenuItem("開始游戲(S)");
jmi1.setMnemonic('S');
jmi1.addActionListener(this);
jmi1.setActionCommand("newGame");
jmi2 = new JMenuItem("繼續游戲(O)");
jmi2.setMnemonic('O');
jmi3 = new JMenuItem("退出游戲(X)");
jmi3.setMnemonic('X');
jmi3.addActionListener(this);
jmi3.setActionCommand("exit");
//啟動mp線程
//em = new EnemyTask();
//mp = new MyPanel();
msp = new myStartPanel();
this.setJMenuBar(jmb);
this.add(msp);
//this.add(mp);
jmb.add(jm);
jm.add(jmi1);
jm.add(jmi2);
jm.add(jmi3);
this.setSize(600,500);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//this.addKeyListener(mp);
Thread t1 = new Thread(msp);
t1.start();
// Thread t = new Thread(mp);
// t.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("newGame")){
mp = new MyPanel();
Thread t = new Thread(mp);
t.start();
this.remove(msp);
this.remove(mp);
this.add(mp);
this.setVisible(true);
this.addKeyListener(mp);
//Recoder.getRecording();
}else if(arg0.getActionCommand()== "exit"){
//Recoder.KeepRecording();
Recoder.KeepExit();
//KeepExit();
System.exit(0);
}
}
}
class myStartPanel extends JPanel implements Runnable{
int times = 0;
public void paint(Graphics g ){
super.paint(g);
g.fillRect(0, 0, 400, 300);
if(times%2 ==0){
g.setColor(Color.BLUE);
Font f = new Font("華文新魏", Font.BOLD, 30);
g.setFont(f);
g.drawString("stage:1", 150, 150);
}
}
@Override
public void run() {
while(true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
times ++;
}
}
}
class MyPanel extends JPanel implements KeyListener,Runnable{
int [] TanksX = new int[20];
int [] TanksY = new int[20];
int [] ShotsX = new int[100];
int [] ShotsY = new int[100];
// int [][]Tanks = new int[20][2];
// int [][]Shots = new int[20][2];
int [] ETdirects = new int[20];
int [] HeroShotsX = new int[10];
int [] HeroShotsY = new int[10];
static Hero hero = null;
EnemyTask et = null;
shot s = null;
boolean isPaintShot = true;
int a = 0, b = 0 , c = 0;
int [] directs = new int [10];
static //敵人坦克集合
Vector<EnemyTask> ets = new Vector<EnemyTask>();
int enSize = 3; //敵人坦克初始數量
int enSizes = 5; //敵人坦克畫面總數量
int hintEnemyTanks = 0;//擊中敵人坦克數量
public void showInfo(Graphics g ){//畫提示信息
//畫出提出坦克
this.drawTank(80, 330, g, 0, 1);
this.drawTank(160, 330, g, 0, 0);
g.setColor(Color.BLACK);
g.drawString(Recoder.getEnNum() + "", 105, 350);
g.drawString(Recoder.getMyLife() + "", 185, 350);
//畫出成績
g.setColor(Color.BLACK);
Font f = new Font("宋體", Font.BOLD, 15);
g.setFont(f);
g.drawString("你的總成績:", 400, 20);
this.drawTank(410, 30, g, 0, 1);
g.drawString((20 - Recoder.getEnNum()) + "", 435, 50);
}
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.showInfo(g);//畫出提示信息
//自己坦克
if(hero.isLive == true){
this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type);
}else{
if(Recoder.getMyLife() > 0){
hero = new Hero(30, 270 ,0,0,10 , true);
hero.isLive = true;
}
}
//畫出敵人坦克
for(int i = 0; i <ets.size(); i++){
EnemyTask et = ets.get(i);
if(!et.isLive){
ets.remove(i);
if(Recoder.getEnNum() >0){
this.drawTank(50, 0, g, 1, 1);
ets.add(et);
//et.isLive = true;
Thread t = new Thread(et);
t.start();
}
// if(Recoder.getEnNum() > 0){
// et.isLive = true;
// }
}
if(et.isLive){
this.drawTank(et.getX(), et.getY(),
g, et.direct, et.type);
System.out.println("et.ss.size()"+et.ss.size());
for(int j = 0; j < et.ss.size(); j++){//敵人坦克子彈
s = et.ss.get(j);
if(s.isLive){
if(isPaintShot){
g.fill3DRect(s.x, s.y, 5, 10, false);
}
}else{
et.ss.remove(j);
}
}
}
// if(!et.isLive){
// ets.remove(i);
// }
}
//畫出子彈
for(int i = 0; i< hero.ss.size(); i++){
if(hero.ss.get(i)!= null && hero.ss.get(i).isLive == true){
g.fill3DRect(hero.ss.get(i).x, hero.ss.get(i).y, 5, 10, false);
}
if(hero.ss.get(i).isLive == false){
hero.ss.remove(hero.ss.get(i));
}
}
}
// public void hintTank2(shot s , Hero et){
// switch (et.direct) {
// case 0:
// case 1:
// if(s.x < et.x + 20 && s.x > et.x
// && s.y < et.y + 30 && s.y > et.y){
// s.isLive = false;
// et.isLive = false;
// }
// //break;
// case 2:
// case 3:
// if(s.x < et.x + 30 && s.x > et.x
// && s.y < et.y + 20 && s.y > et.y){
// s.isLive = false;
// et.isLive = false;
// }
// //break;
//
//// default:
//// break;
// }
//
// }
public boolean hintTank(shot s , Tank et){
boolean b = false;
switch (et.direct) {
case 0:
case 1:
if(s.x < et.x + 20 && s.x > et.x
&& s.y < et.y + 30 && s.y > et.y){
s.isLive = false;
et.isLive = false;
b = true;
}
break;
case 2:
case 3:
if(s.x < et.x + 30 && s.x > et.x
&& s.y < et.y + 20 && s.y > et.y){
s.isLive = false;
et.isLive = false;
b = true;
}
break;
// default:
// break;
}
return b;
}
public void drawTank (int x , int y , Graphics g , int direct , int type){
switch(type){//坦克類型
case 0:
g.setColor(Color.RED);
break;
case 1:
g.setColor(Color.BLUE);
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 + 10, y,x + 10, y + 15);
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 + 30 );
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 , y + 10,x + 15,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 + 30 , y + 10);
break;
default:
break;
}
}
public MyPanel (){
hero = new Hero(30, 270 ,0,0,10 , true);
//tanks = new tank(100,100, 2 ,1);
//初始化敵人坦克
Recoder.setHero(hero);
for(int i = 0; i < enSize; i++){
//創建敵人坦克的對象
Recoder.getRecording();
EnemyTask et = new EnemyTask((i + 1)*50, 0, 1, 1, 5 ,true);
et.setEts(ets);
Recoder.setEtss(ets);//傳ets到Recoder
Thread t = new Thread(et);
t.start();
shot s = new shot(et.x + 10 , et.y + 30, et.direct);
et.ss.add(s);
Thread t2 = new Thread(s);
t2.start();
ets.add(et);//加入到集合中
//et.setColor(1);
}
Recoder.setEnSize(enSize);
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_S){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getY();
// i += hero.speed;
// hero.setY(i);
this.hero.MoveDown();
hero.setD(1);
}
if(arg0.getKeyCode() == KeyEvent.VK_W){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getY();
// i -= hero.speed;
// hero.setY(i);
hero.MoveUp();
hero.setD(0);
}
if(arg0.getKeyCode() == KeyEvent.VK_D){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getX();
// i += hero.speed;
// hero.setX(i);
hero.MoveRight();
hero.setD(3);
}
if(arg0.getKeyCode() == KeyEvent.VK_A){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getX();
// i -= hero.speed;
// hero.setX(i);
hero.MoveLeft();
hero.setD(2);
}
//判斷是否按下J
if(arg0.getKeyCode() == KeyEvent.VK_J){
if(hero.ss.size() <= 4){
this.hero.shotEnemy();
//System.out.println("j");
}
}
if(arg0.getKeyCode() == KeyEvent.VK_SPACE){//空格暫停
if(hero.speed != 0){
a = hero.speed ;
hero.speed = 0;
for(int i = 0; i < ets.size(); i ++){
//System.out.println("aaaaaaaaaaaaaa");
EnemyTask et = ets.get(i);
//System.out.println("speed" + et.speed);
b = et.speed;
et.speed = 0;
directs[i] = et.direct;
//System.out.println(et.direct);
//System.out.println(directs[i]);
//et.direct = 0;
et.isdirects = false;
for(int j = 0 ; j < et.ss.size(); j ++){
shot s = et.ss.get(j);
c = s.speed;
s.speed = 0;
System.out.println("aa");
}
}//System.out.println("b1 =" + b);
isPaintShot = false;
}
else{
hero.speed = a;
//System.out.println("a2 =" + a);
for(int i = 0; i < ets.size(); i ++){
//System.out.println("bbbbbbbbbbbbbbbbbbb");
EnemyTask et = ets.get(i);
et.isdirects = true;
et.speed = b;
et.direct = directs[i];
for(int j = 0 ; j < et.ss.size(); j ++){
shot s = et.ss.get(j);
s.speed = c;
isPaintShot = true;
}
}s.isShot = true;
}
}
if(arg0.getKeyCode() == KeyEvent.VK_K){
this.KeepExit();
System.exit(0);
}
if(arg0.getKeyCode() == KeyEvent.VK_L){
this.GetKeeper();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
//每隔100ms
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.hintEnemyTank();
this.hintMyTank();
// for(int i = 0; i < et.ss.size(); i ++){
// shot myshot = et.ss.get(i);
// if(et.isLive == true){
//
// if(hero.isLive = true){
// this.hintTank2(myshot, hero);
// }
//
// }
// }
this.repaint();
}
}
private void hintMyTank() {
for(int i = 0; i < ets.size(); i++){
EnemyTask et = ets.get(i);
for(int j = 0; j < et.ss.size(); j++ ){
shot s = et.ss.get(j);
if(s != null){
if(hero.isLive){
this.hintTank(s, hero);
if(this.hintTank(s, hero)){//判斷是否擊中,返回true則hero數量減一
Recoder.setMyLife(Recoder.getMyLife() - 1 );
}
}
}
}
}
}
private void hintEnemyTank() {
for(int i = 0; i < hero.ss.size(); i ++){
shot myshot = hero.ss.get(i);
if(myshot.isLive == true){
for(int j = 0; j < ets.size(); j ++){
EnemyTask et = ets.get(j);
if(et.isLive = true){
this.hintTank(myshot, et);
if(this.hintTank(myshot, et)){
hintEnemyTanks ++;
Recoder.setEnNum(Recoder.getEnNum() - 1);
}
}
}
}
}
}
private void KeepExit(){
int [] heros = new int[]{
hero.getX(),
hero.getY(),
hero.direct,
};
for(int i = 0 ; i < ets.size(); i ++){
EnemyTask et = ets.get(i);
TanksX[i] = et.getX();
TanksY[i] = et.getY();
ETdirects [i] = et.direct;
for(int j = 1 ; j < et.ss.size(); j++){
shot s = et.ss.get(j);
ShotsX[j] = s.x;
ShotsY[j] = s.y;
}
}
for(int i = 0 ; i < hero.ss.size(); i++ ){
shot s = hero.ss.get(i);
HeroShotsX[i] = s.x ;
HeroShotsY[i] = s.y;
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("d:\\aa\\TanksX.txt"));
for(int i = 0 ; i < enSize ; i ++){
bw.write(TanksX[i] + "\r\n");
bw.write(TanksY[i] + "\r\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void GetKeeper (){//繼續游戲時還原數據
BufferedReader br = null ;
try {
br = new BufferedReader(new FileReader("d:\\aa\\TanksX.txt"));
for(int i = 0; i < enSize*2 ; i ++){
if(i % 2 !=0){
TanksY[i] = Integer.parseInt(br.readLine());
System.out.println("TanksY" + TanksY[i]);
}else{
TanksX[i] = Integer.parseInt(br.readLine());
System.out.println("TanksX" + TanksX[i]);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//class tank extends Tank{
//
// public tank(int x, int y, int direct,int type) {
// super(x, y, direct,type);
// // TODO Auto-generated constructor stub
// }
//
//}
下面是工具類:
package com.qq.TankGame;
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 Recoder{
/**
*
*/
private static Vector<EnemyTask> ets = new Vector<EnemyTask>();
public static Vector<EnemyTask> getEts() {
return ets;
}
public static void setEtss(Vector<EnemyTask> ets) {
Recoder.ets = ets;
}
private static int enSize ;
public static int getEnSize() {
return enSize;
}
public static void setEnSize(int enSize) {
Recoder.enSize = enSize;
}
private static Hero hero ;
public static Hero getHero() {
return hero;
}
public static void setHero(Hero hero) {
Recoder.hero = hero;
}
public static void KeepExit(){
//System.out.println(hero.getX());
int [] TanksX = new int[20];
int [] TanksY = new int[20];
int [] ShotsX = new int[100];
int [] ShotsY = new int[100];
int [] ETdirects = new int[20];
int [] HeroShotsX = new int[10];
int [] HeroShotsY = new int[10];
int [] heros = new int[]{
hero.getX(),
hero.getY(),
hero.direct
};
for(int i = 0 ; i < ets.size(); i ++){
EnemyTask et = ets.get(i);
TanksX[i] = et.getX();
TanksY[i] = et.getY();
ETdirects [i] = et.direct;
for(int j = 1 ; j < et.ss.size(); j++){
shot s = et.ss.get(j);
ShotsX[j] = s.x;
ShotsY[j] = s.y;
}
}
for(int i = 0 ; i < hero.ss.size(); i++ ){
shot s = hero.ss.get(i);
HeroShotsX[i] = s.x ;
HeroShotsY[i] = s.y;
}
BufferedWriter bw = null;
BufferedWriter bw1 = null ;
try {
bw = new BufferedWriter(new FileWriter("d:\\aa\\TanksX.txt"));
for(int i = 0 ; i < enSize ; i ++){
bw.write(TanksX[i] + "\r\n");
bw.write(TanksY[i] + "\r\n");
}
bw1 = new BufferedWriter(new FileWriter("d:\\aa\\KeepRecoding.txt"));
bw1.write((20 - getEnNum()) + "\r\n" );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bw1.close();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static int enNum = 5;
public static int getEnNum() {
return enNum;
}
public static void setEnNum(int enNum) {
Recoder.enNum = enNum;
}
public static int getMyLife() {
return myLife;
}
public static void setMyLife(int myLife) {
Recoder.myLife = myLife;
}
private static int myLife = 3;
private static BufferedReader br = null;
public static void getRecording(){
try {
br = new BufferedReader(new FileReader("d:\\aa\\KeepRecoding.txt"));
String n ;
while((n = br.readLine()) != null ){
String a = n;
enNum = 20 - Integer.parseInt(n);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static BufferedWriter bw = null;
public static void KeepRecording (){
try {
bw = new BufferedWriter(new FileWriter("d:\\aa\\KeepRecoding.txt"));
bw.write((20 - getEnNum()) + "\r\n" );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();//先開後關
//new FileWriter("d:\\aa\\KeepRecoding.txt").close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Tank {
int x = 0 ;
int y = 0;
int direct = 0; //方向
int type = 0 ;//類型
int speed = 30;//速度
int color;
boolean isLive;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getS() {
return speed;
}
public void setS(int speed) {
this.speed = speed;
}
public Tank(int x , int y , int direct
, int type, int speed, boolean isLive){
this.x = x;
this.y = y;
this.direct = direct;
this.type= type;
this.speed = speed;
this.isLive = isLive;
}
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 int getD() {
return direct;
}
public void setD(int direct) {
this.direct = direct;
}
public int getT() {
return type;
}
public void setT(int type) {
this.type = type;
}
}
class EnemyTask extends Tank implements Runnable{
Vector<shot> ss = new Vector<shot>();
Vector<EnemyTask> ets = new Vector<EnemyTask>();
boolean isdirects = true;
public EnemyTask(int x, int y, int direct
, int type, int speed ,boolean isLive) {
super(x, y, direct, type, speed, isLive);
// TODO Auto-generated constructor stub
}
public boolean isTouchOhterTank (){
boolean b = false;
switch (this.direct) {
case 0://此坦克向上
for(int i = 0; i < ets.size(); i++){
EnemyTask et = ets.get(i);
if(et != this){
if(et.direct == 0 || et.direct == 1){//上或下
if((this.x>=et.x && this.x <= et.x + 20//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 30)//第一個點y在內
||(this.x + 20>=et.x && this.x+ 20 <= et.x + 20//第二個點x在內
&& this.y >=et.y && this.y <= et.y + 30)//第二個點y在內
){
b = true;
}
}
if(et.direct == 2 || et.direct == 3){//左或右
if((this.x>=et.x && this.x <= et.x + 30//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 20)//第一個點y在內
||(this.x + 20>=et.x && this.x+ 20 <= et.x + 30//第二個點x在內
&& this.y >=et.y && this.y <= et.y + 20)//第二個點y在內
){
b = true;
}
}
}
}
break;
case 1://向下
for(int i = 0; i < ets.size(); i++){
EnemyTask et = ets.get(i);
if(et != this){
if(et.direct == 0 || et.direct == 1){//上或下
if((this.x>=et.x && this.x <= et.x + 20//第一個點x在內
&& this.y + 30>=et.y && this.y + 30 <= et.y + 30)//第一個點y在內
||(this.x + 20>=et.x && this.x+ 20 <= et.x + 20//第二個點x在內
&& this.y + 30>=et.y && this.y + 30 <= et.y + 30)//第二個點y在內
){
b = true;
}
}
if(et.direct == 2 || et.direct == 3){//左或右
if((this.x>=et.x && this.x <= et.x + 30//第一個點x在內
&& this.y + 30 >=et.y && this.y + 30 <= et.y + 20)//第一個點y在內
||(this.x + 20 >=et.x && this.x+ 20 <= et.x + 30//第二個點x在內
&& this.y + 30 >=et.y && this.y + 30 <= et.y + 20)//第二個點y在內
){
b = true;
}
}
}
}
break;
case 2:
for(int i = 0; i < ets.size(); i++){
EnemyTask et = ets.get(i);
if(et != this){
if(et.direct == 0 || et.direct == 1){//上或下
if((this.x>=et.x && this.x <= et.x + 20//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 30)//第一個點y在內
||(this.x >=et.x && this.x<= et.x + 20//第二個點x在內
&& this.y + 20 >=et.y && this.y + 20 <= et.y + 30)//第二個點y在內
){
b = true;
}
}
if(et.direct == 2 || et.direct == 3){//左或右
if((this.x>=et.x && this.x <= et.x + 30//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 20)//第一個點y在內
||(this.x >=et.x && this.x<= et.x + 30//第二個點x在內
&& this.y + 20>=et.y && this.y + 20 <= et.y + 20)//第二個點y在內
){
b = true;
}
}
}
}
break;
case 3:
for(int i = 0; i < ets.size(); i++){
EnemyTask 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//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 30)//第一個點y在內
||(this.x + 30 >=et.x && this.x+ 30 <= et.x + 20//第二個點x在內
&& this.y + 20 >=et.y && this.y + 20 <= et.y + 30)//第二個點y在內
){
b = true;
}
}
if(et.direct == 2 || et.direct == 3){//左或右
if((this.x + 30>=et.x && this.x + 30 <= et.x + 30//第一個點x在內
&& this.y>=et.y && this.y <= et.y + 20)//第一個點y在內
||(this.x + 30>=et.x && this.x+ 30 <= et.x + 30//第二個點x在內
&& this.y + 20>=et.y && this.y + 20 <= et.y + 20)//第二個點y在內
){
b = true;
}
}
}
}
break;
default:
break;
}
return b ;
}
@Override
public void run() {
int a = (int) (Math.random() * 10 + 2);//連動步數
int times = 0;
while(true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if(x > 0 && x < 400 - 30 & y > 0 && y < 300 - 30){
//if(!this.isTouchOhterTank()){
switch(this.direct){
case 0:
//System.out.println(isTouchOhterTank());
for(int i = 0 ;i < a ; i++){
if(y - this.speed > 0 && !this.isTouchOhterTank()){
y -= this.speed;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 1:
for(int i = 0 ;i < a ; i++){
if(y + speed < 300 - 30 && !this.isTouchOhterTank()){
y += this.speed;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 2:
for(int i = 0 ;i < a ; i++){
if(x - speed > 0 && !this.isTouchOhterTank()){
x -= this.speed;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 3:
for(int i = 0 ;i < a ; i++){
if(x + speed < 400 - 30 && !this.isTouchOhterTank()){
x += this.speed;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
//}
times ++;
if(times % 2 == 0){
if(isLive){
if(ss.size() < 3){
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 + 30 , 1);
ss.add(s);
break;
case 2://向左
s = new shot(x , y + 10 , 2);
ss.add(s);
break;
case 3://右
s = new shot(x + 30 , y + 10 , 3);
ss.add(s);
break;
default:
break;
}
Thread t = new Thread(s);
t.start();
}
}
}
//}
// //隨機改變方向
// public void changeDirect(){
//
// }
if(isdirects){
this.direct = (int) (Math.random() * 4);
}
if(this.isLive == false){
break;
}
if(ss.size() < 3){
}
}
}
public void setEts(Vector<EnemyTask> ets) {
this.ets = ets;
}
// @Override
// public void run() {
// while(true){
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// y ++ ;
// System.out.println(y);
// }
//
// }
}
class Hero extends Tank{
//
public Hero(int x , int y, int direct
, int type,int speed, boolean isLive){
super(x, y, direct, type,speed , isLive);
}
Vector<shot> ss = new Vector<shot>();
shot s = null;
//開火
public void shotEnemy(){
//System.out.println("shotEnemy");
if(Recoder.getMyLife() > 0){//生命沒有了,不能發子彈
switch (this.direct) {
case 0://向上
s = new shot(x + 10 , y ,0);
ss.add(s);
break;
case 3://右
s = new shot(x + 30 , y + 10 , 3);
ss.add(s);
break;
case 1://向下
s = new shot(x + 10 , y + 30 , 1);
ss.add(s);
break;
case 2://向左
s = new shot(x , y + 10 , 2);
ss.add(s);
break;
default:
break;
}
Thread t = new Thread(s);
t.start();
}
}
//向上,下,左,右
public void MoveUp(){
if(y - this.speed > 0){
y -= speed;
}
}
public void MoveDown(){
if(y + this.speed < 300 -30){
y += speed;
}
}
public void MoveLeft(){
if(x - this.speed > 0){
x -= speed;
}
}
public void MoveRight(){
if(x + this.speed < 400 -30){
x += speed;
}
}
}
class shot implements Runnable{
int x ,y;
int direct;
int speed = 20; //子彈速度
boolean isShot = true;
boolean isLive = true;//子彈是否活著
public shot(int x,int y, int direct){
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run() {
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isShot){
switch (direct) {
case 0://向上
y -= speed;
break;
case 1://下
y += speed;
break;
case 2://右
x -= speed;
break;
case 3://左
x += speed;
break;
default:
break;
}
}
//System.out.println(x + " " + y);
if(x < 0 || x > 400 || y < 0 || y > 300){
//System.out.println("this.islive = false");
this.isLive = false;
break;
}
}
}
}