坦克大战

来源:互联网 发布:java 8 获取当前时区 编辑:程序博客网 时间:2024/06/10 04:46
 package com.TankWar;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TankMap extends Frame
{
    //地图大小
public static final int MAPWIDTH = 600;
public static final int MAPHEIGHT = 400;
//我方坦克
Tank t = null;
//把炮弹放在一个集合中
public static java.util.List <Shell> shells = new ArrayList<Shell>();
//敌方坦克集合
public static java.util.List <EnemyTank> enemys = new ArrayList <EnemyTank>();
//敌方坦克数量
public int enemyCount = 5;
//爆炸集合
public static java.util.List <Explor> explors = new ArrayList <Explor>();

    
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        TankMap tv = new TankMap();
        tv.init();

    }
//初始化地图
    public void init()
    {
        this.setSize(MAPWIDTH, MAPHEIGHT);
        this.setTitle("TankWar");
        this.setVisible(true);
        //添加键盘监听器
        this.addKeyListener(new DirectionHandler());
        //添加关闭窗口监听器
        this.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent arg0)
            {
                System.exit(0);
            }
        });
        //初始化我方坦克
        t = new Tank(this);
        //初始化敌方坦克
        for(int i = 0;i < enemyCount;i++)
        {
            enemys.add(new EnemyTank(40 + 30 * i,80,Color.YELLOW));
        }
        //启动绘画线程
        new  Thread(new PaintThread()).start();
    }
    //画地图
        public void paint(Graphics g)
        {
            Color c = g.getColor();
            g.setColor(Color.GREEN);
            g.fillRect(0, 0, MAPWIDTH, MAPHEIGHT);
            g.setColor(Color.RED);
            g.drawString("当前炮弹数目:"+shells.size(), 20,40);
            g.drawString("生命值:", 20,60);
            g.fillRect(65, 55, t.getLife(), 5);
            g.setColor(c);
            // 画坦克
            t.draw(g);
            for(int i = 0; i < enemys.size();i++)
            {
                EnemyTank et = enemys.get(i);
                et.draw(g);
            }
            //画炮弹
            for(int i = 0;i<shells.size();i++)
            {
                Shell s = shells.get(i);
                if(s.islive && s.isgood)
                {
                    s.hitTanks(enemys);
                    
                }
                else if(s.islive && !s.isgood)
                {
                    s.hitTank(t);
                }
                s.draw(g);
            }
            //画爆炸
            for(int i =0 ;i < explors.size();i++)
            {
                Explor e = explors.get(i);
                e.draw(g);
            }
        }
        public void update(Graphics g)
        {
            paint(g);
            
        }
        //实现绘图功能
        class PaintThread implements Runnable
        {
            public void run ()
            {
                while(true)
                {
                    repaint();
                    try
                    {
                        Thread.sleep(260);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        //内部类,键盘监听器
        class DirectionHandler extends KeyAdapter
        {
            public void keyPressed(KeyEvent arg0)
            {
                t.checkDirection(arg0);
            }
        }
    }



package com.TankWar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Tank
{
    //坦克大小和初始的移动速度
public static final int WIDTH = 20,HEIGHT = 20,xspeed = 5,yspeed = 5;
//坦克的初始位置
public int x = 400, y = 300;
//坦克的初始方向,STOP是停止
public Direction  direction  = Direction.STOP;
//地图
TankMap tm ;
//坦克是不是存活
boolean isLive = true;
//坦克是哪一方,true为用户方,false为敌方
boolean isgood = true;
//设置坦克的生命值
int life = 100;
//获取和设置状态
public boolean isLive()
{
return isLive;    
}
public void setLive(boolean isLive)
{
this.isLive = isLive;    
}
//获取和设置生命值
public int getLife()
{
return life;    
}
public void setLife(int life)
{
this.life = life;    
}
//构造方法
public Tank(){};
public Tank(TankMap t)
{
tm = t;    
}
//绘制坦克
public void draw(Graphics g)
{
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move(direction);
}
//限制坦克的移动范围,不能移出地图
public void move(Direction d)
{
if (d == Direction.STOP)
{    
}
else if(d == Direction.UP)
{
y-=yspeed;
if(y<=HEIGHT)
    y=HEIGHT;

}
else if(d == Direction.RIGHT)
{
x+= xspeed;
if(x>=800-WIDTH)
    x = 800-WIDTH;

}
else if(d==Direction.DOWN)
{
y+=yspeed;
if(y>=600-HEIGHT)
    y=600-HEIGHT;

}
else if(d == Direction.LEFT)
{
x-=xspeed;
if(x<=0)
    x=0;
}
}
//确定用户单机的方向键,修整坦克即将移动的方向
public void checkDirection(KeyEvent k)
{
if(k.getKeyCode() == KeyEvent.VK_UP)
{
direction = Direction.UP;
}
else if(k.getKeyCode() == KeyEvent.VK_RIGHT)
{
direction = Direction.RIGHT;
}
else if(k.getKeyCode() == KeyEvent.VK_DOWN)
{
direction = Direction.DOWN;    
}
else if(k.getKeyCode() == KeyEvent.VK_LEFT)
{
direction = Direction.LEFT;    
}
    if(k.getKeyChar() == KeyEvent.VK_F)
    {
        fire();
    }
}
//发射炮弹的方法
public void fire()
{
    //产生一个炮弹,指定其属性:炮弹的产生位置,移动方向和颜色,属于哪一方
tm.shells.add(new Shell(this.x + WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.red,true));
}
//返回坦克当前位置矩形,便于判断是否与敌方坦克或炮弹重叠
public Rectangle getRec()
{
return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);    
}
}

package com.TankWar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class EnemyTank extends Tank
{
    //初始化敌方坦克属性
public static final int WIDTH = 20,HEIGHT = 20,xspeed = 5,yspeed = 5;
public int x = 400,y = 300;
public Direction direction = Direction.DOWN;
public boolean islive = true;
Color color = Color.YELLOW;
TankMap tm;
//随机数产生器,供敌方坦克产生随机前进的方向
Random r  = new Random();
//方向更改限制
int randomCount = r.nextInt(10)+5;

public EnemyTank()
{}
public EnemyTank(int wx,int wy,Color c)
{
x = wx;
y = wy;
color = c;
}
public void draw(Graphics g)
{
Color c = g.getColor();
g.setColor(color);
g.fillOval(x, y,WIDTH, HEIGHT);
g.setColor(c);
move();
}
//敌方坦克的移动
public void move()
{
    //当方向限制为0,随即产生一个方向
if(randomCount == 0)
{
Direction dirs[] = Direction.values();
direction = dirs[r.nextInt(dirs.length-1)];
randomCount = r.nextInt(10) +5;

}
//方向更改限制减1
randomCount --;
//随机产生随机数,当随机数大于35,发射炮弹
if(r.nextInt(40) > 35)
{
fire();    
}
//确定地方的坦克移动
 if(direction == Direction.UP)
{
y-=yspeed;
if(y<=HEIGHT)
    y=HEIGHT;

}
else if(direction == Direction.RIGHT)
{
x+= xspeed;
if(x>=800-WIDTH)
    x = 800-WIDTH;

}
else if(direction==Direction.DOWN)
{
y+=yspeed;
if(y>=600-HEIGHT)
    y=600-HEIGHT;

}
else if(direction == Direction.LEFT)
{
x-=xspeed;
if(x<=0)
    x=0;
}
}
 public void fire()
 {
    tm.shells.add(new Shell(this.x+WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.blue,false));
 }
 public Rectangle getRec()
 {
     return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);
 }
}

    

package com.TankWar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import javax.swing.JOptionPane;

public class Shell {

    /**
     * @param args
     */
    //炮弹的基本属性
public final int WIDTH = 5,HEIGHT = 5,xspeed = 14,yspeed=14;
public int x,y;
Direction dir = Direction.STOP;
public boolean islive = true;
TankMap tm = null;
Color color = Color.BLUE;
boolean isgood = true;
boolean isLive = true;
public  Shell()
{    
}
public Shell(int xd,int yd,Direction d,TankMap t)
{
x = xd;
y = yd;
dir = d;
tm =t;

}
public Shell(int xd,int yd,Direction d,TankMap t,Color c,boolean g)
{
x= xd;
y = yd;
dir = d;
tm = t;
color = c;

isgood = g;

}
//绘制炮弹
public void draw(Graphics g)
{
    //如果炮弹存活,绘制
if(islive)
{
Color c = g.getColor();
g.setColor(color);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move(dir);

}
//否则从炮弹的集合中消除该炮弹
else
{
tm.shells.remove(this);    
}
}
//炮弹的移动,如果炮弹移动出了地图的尺寸,则该炮弹死亡
public void move(Direction d)
{
    if(d == Direction.UP)
    {
    y -=yspeed;
    if(y<=0)
        islive = false;
    
    }
    else if(d == Direction.RIGHT)
    {
        x+=xspeed;
    if(x>=800)
    islive = false;
    }
    else if (d == Direction.DOWN)
    {
        y+=yspeed;
    if(y>=600)
        islive=false;
    
    }
    else if(d == Direction.LEFT)
    {
        x -=xspeed;
        if(x<=0)
            islive = false;
        
    }
}
public Rectangle getRec()
{
return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);

}
//打击坦克的方法
public boolean hitTank(Tank t)
{
    //如果“存活” 的敌方炮弹“碰到”“存活”的用户坦克
if(this.isLive && t.isLive && this.getRec().intersects(t.getRec()))    
{
    //用户坦克生命值减20
    t.setLife(t.getLife()-20);
    //如果用户坦克生命值《《=0.游戏结束
    if(t.getLife()<=0)
    {
        t.setLive(false);
        t.direction = Direction.STOP;
        JOptionPane.showMessageDialog(tm,"Game Over");
        System.exit(0);
        
    }
    //炮弹死亡
    this.islive = false;
    //产生爆炸
    Explor e = new Explor(x-3,y-3,this.tm);
    tm.explors.add(e);
    return true;    
}
return false;
}
// 用户坦克打击敌方坦克的方法
public boolean hitTanks(List<EnemyTank>enemyTanks)
{
EnemyTank e;
for(int i =0 ;i < enemyTanks.size();i++)
{
    e = enemyTanks.get(i);
    if(this.getRec().intersects(e.getRec()))
    {
        System.out.println("hittanks");
        e.setLive(false);
        tm.enemys.remove(e);
        this.islive = false;
        Explor ex = new Explor(x-3,y-3,this.tm);
        tm.explors.add(ex);
        return true;
    }
}
return false;
}
}


package com.TankWar;

import java.awt.*;

public class Explor
{
    //初始化爆炸属性
    int x,y;
    TankMap tm;
    boolean islive = true;
    int diameter[] = {5,9,13,18,25,31,21,12,6};
    int step = 0;
    
    public Explor()
    {    
    }
    public Explor(int x,int y,TankMap t)
    {
        this.x = x;
        this.y = y;
        this.tm = t;
    }
    //绘制爆炸
public void draw(Graphics g)
{
    //如果爆炸已经发生,从地图的爆炸集合中移除该爆炸
if(!this.islive)
{
tm.explors.remove(this);
return ;
}
//判断爆炸 是否发生完
if(step == diameter.length)
{
this.islive = false;
step = 0;
return ;

}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, diameter[step],diameter[step]);
step++;
g.setColor(c);
}
}



package com.TankWar;
//定义枚举
public enum Direction
{

    UP,RIGHT,DOWN,LEFT,STOP
}


原创粉丝点击