您的位置:首页 > 编程语言 > Java开发

用java实现的坦克大战

2012-04-24 21:55 393 查看
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<bt.ammo.size();i++)
if(bt.ammo.get(i).judgeSpace)
bt.ammo.get(i).ammoPain();//画出炮弹
gBuffer.setColor(c);
g.drawImage(iBuffer,0,0,this);
}
public void update(Graphics g){
paint(g);
}
}

class BasicTank{
static byte publicID = 1;
byte ID = publicID;
boolean UP = true,DOWN = true,LEFT = true,RIGHT = true ;
int Direction = 0;
int LocationX = 6;
int LocationY = 6;
int [][] Up = {
{0,1,0},
{1,1,1},
{1,0,1}};
int [][] Down = {{1,0,1},{1,1,1},{0,1,0}};
int [][] Left = {{0,1,1},{1,1,0},{0,1,1}};
int [][] Right = {{1,1,0},{0,1,1},{1,1,0}};
Vector<Ammo> 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<bt.ammo.size();i++)
if(bt.ammo.get(i).ID == ammo.ID)
bt.ammo.remove(i) ;
for(int i = 0;i<EnemyVector.enemy.size();i++)
if(EnemyVector.enemy.get(i).ID == MAP.map[ammo.LX+1][ammo.LY])
{EnemyVector.enemy.get(i).clear();EnemyVector.enemy.remove(i);}
mf.repaint();
ammo.publicID--;

break;
}
}
break;
case 1 :
while(true){
if(MAP.map[ammo.LX+1][ammo.LY+2] == 0)
{
try{
sleep(100); //线程休眠600ms
}catch(InterruptedException e){}
ammo.move(ammo.LX,ammo.LY,Direction);
mf.repaint();
}
else
{

for(int i = 0;i<bt.ammo.size();i++)
if(bt.ammo.get(i).ID == ammo.ID)
bt.ammo.remove(i) ;
for(int i = 0;i<EnemyVector.enemy.size();i++)
if(EnemyVector.enemy.get(i).ID == MAP.map[ammo.LX+1][ammo.LY+2])
{EnemyVector.enemy.get(i).clear();EnemyVector.enemy.remove(i);}
mf.repaint();
ammo.publicID--;

break;
}
}
break;
case 2 :
while(true){
if(MAP.map[ammo.LX][ammo.LY+1] == 0)
{
try{
sleep(100); //线程休眠600ms
}catch(InterruptedException e){}
ammo.move(ammo.LX,ammo.LY,Direction);
mf.repaint();
}
else
{

for(int i = 0;i<bt.ammo.size();i++)
if(bt.ammo.get(i).ID == ammo.ID)
bt.ammo.remove(i) ;
for(int i = 0;i<EnemyVector.enemy.size();i++)
if(EnemyVector.enemy.get(i).ID == MAP.map[ammo.LX][ammo.LY+1])
{EnemyVector.enemy.get(i).clear();EnemyVector.enemy.remove(i);}
mf.repaint();
ammo.publicID--;

break;
}
}
break;
case 3 :
while(true){
if(MAP.map[ammo.LX+2][ammo.LY+1] == 0)
{
try{
sleep(100); //线程休眠600ms
}catch(InterruptedException e){}
ammo.move(ammo.LX,ammo.LY,Direction);
mf.repaint();
}
else
{
for(int i = 0;i<bt.ammo.size();i++)
if(bt.ammo.get(i).ID == ammo.ID)
bt.ammo.remove(i) ;
for(int i = 0;i<EnemyVector.enemy.size();i++)
if(EnemyVector.enemy.get(i).ID == MAP.map[ammo.LX+2][ammo.LY+1])
{EnemyVector.enemy.get(i).clear();EnemyVector.enemy.remove(i);}
mf.repaint();
ammo.publicID--;

break;
}
}
break;
}
}
}

class EnemyTank extends BasicTank{
EnemyTank(){
super();
EnemyVector.enemy.add(this);
}
EnemyTank(int x,int y,int direction){
super(x,y,direction);
EnemyVector.enemy.add(this);
}

}

class EnemyThread extends  Thread{// 用于控制敌方坦克的移动
MyFrame mf = null;
EnemyThread(MyFrame mf){
this.mf = mf;
}
public void run(){

while(true)
{
try{
sleep(600); //线程休眠600ms
}catch(InterruptedException e){}
for(int i = 0;i< EnemyVector.enemy.size();i++)
if(EnemyVector.enemy.get(i).judgeMove())
{
switch(EnemyVector.enemy.get(i).Direction){
case 0 :
EnemyVector.enemy.get(i).moveUp();
break;
case 1 :
EnemyVector.enemy.get(i).moveDown();
break;
case 2 :
EnemyVector.enemy.get(i).moveLeft();
break;
case 3 :
EnemyVector.enemy.get(i).moveRight();
break;
}
}
else
{

Random rand = new Random();
int num = rand.nextInt(4);
System.out.println(""+num);
int count = 0;
while(true)
{
if(!EnemyVector.enemy.get(i).judgerTurn(num))
{
num = rand.nextInt(4);
System.out.println(""+num);
if(count == 3)
{
count = 0;
break;
}
count++;
}
else
{
break;
}
}
switch(num)
{
case 0 :
EnemyVector.enemy.get(i).turnUp();
EnemyVector.enemy.get(i).moveUp();
//  mf.repaint();
break;
case 1 :
EnemyVector.enemy.get(i).turnDown();
EnemyVector.enemy.get(i).moveDown();
// mf.repaint();
break;
case 2 :
EnemyVector.enemy.get(i).turnLeft();
EnemyVector.enemy.get(i).moveLeft();
// mf.repaint();
break;
case 3 :
EnemyVector.enemy.get(i).turnRight();
EnemyVector.enemy.get(i).moveRight();
// mf.repaint();
break;
}

}
///////////////////// 上面代码实现碰到障碍后改变方向 //////////////////////下面代码实现随机改变方向
Random rand1 = new Random();
Random rand = new Random();
Random rand2 = new Random();
int i = rand2.nextInt(EnemyVector.enemy.size());
int num = 0;
int num1 = rand1.nextInt(3);
if(num1 == 2)
{
num = rand.nextInt(4);
System.out.println(""+num);
int count1 = 0;
{
while(true)
{
if(!EnemyVector.enemy.get(i).judgerTurn(num))
{
num = rand.nextInt(4);
System.out.println(""+num);
if(count1 == 3)
{
count1 = 0;
break;
}
count1++;
}
else
{
break;
}
}
switch(num)
{
case 0 :
EnemyVector.enemy.get(i).turnUp();
EnemyVector.enemy.get(i).moveUp();
//  mf.repaint();
break;
case 1 :
EnemyVector.enemy.get(i).turnDown();
EnemyVector.enemy.get(i).moveDown();
// mf.repaint();
break;
case 2 :
EnemyVector.enemy.get(i).turnLeft();
EnemyVector.enemy.get(i).moveLeft();
// mf.repaint();
break;
case 3 :
EnemyVector.enemy.get(i).turnRight();
EnemyVector.enemy.get(i).moveRight();
// mf.repaint();
break;
}
}
}

mf.repaint();
}

}
}

class EnemyVector{
static Vector<EnemyTank> enemy = new Vector(0,1);
}

效果图:



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

这个程序是在学习java两个星期后写出来的,一共用了3天的时间.感觉不太满意,主要是该程序的结构不好,可扩展性较差!希望在以后的学习中能不断提高自己架构程序的能力.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java random class bt up import