坦克大战(java)

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class GamePanel extends JFrame {
    Image offScreemImage=null;
    //窗口长宽
    int wide=800;
    int height=610;
    Image select= Toolkit.getDefaultToolkit().getImage("src/image/爱心.png");
    int y=150;
    int state=0;
    int a=1;
    int count=0;
    int enemyCount=0;
    ArrayList<Bullet>bulletList=new ArrayList<Bullet>();
    ArrayList<Bot>botList=new ArrayList<Bot>();
    ArrayList<Bullet>removeList=new ArrayList<Bullet>();
    ArrayList<Tank>playerList=new ArrayList<Tank>();
    ArrayList<Wall>wallList=new ArrayList<Wall>();
    ArrayList<Base>baseList=new ArrayList<Base>();
    ArrayList<Blast>blastList=new ArrayList<Blast>();
    PlayerOne playerOne=new PlayerOne("src/image/hero1U.gif",125,500,this,
            "src/image/hero1U.gif","src/image/hero1L.gif","src/image/hero1R.gif","src/image/hero1D.gif");

    Base base=new Base("src/image/基地.png",375,520,this);
//    Bot bot=new Bot("src/image/hero1U(1).png",600,50,this,
//            "src/image/hero1U(1).png","src/image/hero1L(1).png","src/image/hero1R(1).png","src/image/hero1D(1).png");
    //窗口的启动方法
    public void launch(){
        //标题
        setTitle("Tank Battle");
        //窗口初始大小
        setSize(wide,height);
        //使屏幕居中
        setLocationRelativeTo(null);
        //添加关闭实践
        setDefaultCloseOperation(3);
        //用户不能调整大小
        setResizable(false);
        //使窗口可见
        setVisible(true);
        //添加键盘监视器
        this.addKeyListener(new GamePanel.KeyMonitor());

        for(int i=0;i<14;i++)
        {
            wallList.add(new Wall("src/image/遮挡物.png",i*70,170,this));
        }
        wallList.add(new Wall("src/image/遮挡物.png",305,520,this));
        wallList.add(new Wall("src/image/遮挡物.png",305,450,this));
        wallList.add(new Wall("src/image/遮挡物.png",375,450,this));
        wallList.add(new Wall("src/image/遮挡物.png",435,450,this));
        wallList.add(new Wall("src/image/遮挡物.png",435,520,this));
        baseList.add(base);
        while(true){
            //游戏胜利判定
            if(botList.size()==0&&enemyCount==10){
                state=5;
            }
            if((playerList.size()==0&&(state==1||state==2))||baseList.size()==0)
            {
                state=4;
            }
            if(count%100==1&&enemyCount<10)
            {
                Random random=new Random();
                int rnum=random.nextInt(800);
                botList.add(new Bot("src/image/hero1U(1).png",rnum,50,this,
                        "src/image/hero1U(1).png","src/image/hero1L(1).png","src/image/hero1R(1).png",
                        "src/image/hero1D(1).png"));
                enemyCount++;
            }

            repaint();
            try{
                Thread.sleep(25);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    @Override
    public void paint(Graphics g)
    {
        System.out.println(bulletList.size());
        if(offScreemImage==null)
        {
            offScreemImage=this.createImage(wide,height);
        }
        Graphics gimage=offScreemImage.getGraphics();
        gimage.setColor(Color.pink);
        gimage.fillRect(0,0,wide,height);
        gimage.setColor(Color.red);
        gimage.setFont(new Font("正楷",Font.BOLD,50));
        if(state==0) {
            gimage.drawString("Game Mode", 220, 100);
            gimage.drawString("Single Mode", 220, 200);
            gimage.drawString("Tow-person Mode", 220, 300);
            gimage.drawString("Press 1,2 to select the mode,",50,400);
            gimage.drawString("Press Enter to start the game.",50,500);
            gimage.drawImage(select, 160, y, null);
        }
        else if(state==1||state==2)
        {
            gimage.drawString("Game Start",220,300);
            if(state==1)
            {
                gimage.drawString("Single Mode",220,200);
            }
            else if(state==2)
            {
                gimage.drawString("Tow-person Mode",220,200);
            }
            for(Tank player:playerList)
            {
                player.paintSelf(gimage);
            }
            for(Bullet bullet:bulletList)
            {
                bullet.paintSelf(gimage);
            }
            bulletList.removeAll(removeList);
            for(Bot bot:botList)
            {
                bot.paintSelf(gimage);
            }
            for(Wall wall:wallList)
            {
                wall.paintSelf(gimage);
            }
            for(Base base:baseList)
            {
                base.paintSelf(gimage);
            }
            for(Blast blast:blastList)
            {
                blast.paintSelf(gimage);
            }
            count++;
        }
        else if(state==5)
        {
            gimage.drawString("WIN!",220,300);
        }
        else if(state==4)
        {
            gimage.drawString("LOSE~",220,300);
        }
        else if(state==3)
        {
            gimage.drawString("Game Pause~",220,300);
        }

        g.drawImage(offScreemImage,0,0,null);
    }
    //键盘监视器
    class KeyMonitor extends KeyAdapter {
       @Override
        public void keyPressed(KeyEvent e)
        {
            //返回键值
           // System.out.println(e.getKeyChar());
            int key=e.getKeyCode();
            switch (key)
            {
                case KeyEvent.VK_1:
                    a=1;
                    y=150;
                    break;
                case KeyEvent.VK_2:
                    a=2;
                    y=250;
                    break;
                case KeyEvent.VK_ENTER:
                    state=a;
                    playerList.add(playerOne);
                    break;
                case KeyEvent.VK_P:
                    if(state!=3)
                    {
                        a=state;
                        state=3;
                    }
                    else
                    {
                        state=a;
                        if(a==0)a=1;
                    }
                default:
                    playerOne.keyPressed(e);
            }
        }
        @Override
        public void keyReleased(KeyEvent e)
        {
            playerOne.keyReleased(e);
        }
    }
    //main
    public static void main(String[] args){
        GamePanel g=new GamePanel();
        g.launch();
    }
}
import java.awt.*;

public class Blast extends GameObject{

//    static Image[] imgs=new Image[10];
//    int explodeCount=0;
//    static {
//        for(int i=0;i<10;i++)
//        {
//            imgs[i]=Toolkit.getDefaultToolkit().getImage("src/image/"+i+".gif");
//        }
//    }
    public Blast(String img, int x, int y, GamePanel gamePanel) {
        super(img, x, y, gamePanel);
    }

    @Override
    public void paintSelf(Graphics g) {
//        if(explodeCount<10)
//        {
//            //g.drawImage(imgs[explodeCount],x,y,null);
//            //explodeCount++;
//        }
    }

    @Override
    public Rectangle getRec() {
        return null;
    }
}
import java.awt.*;

public class Base extends GameObject{

    int length=50;
    public Base(String img, int x, int y, GamePanel gamePanel) {
        super(img, x, y, gamePanel);
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,length,length);
    }
}
import java.awt.*;

public class Wall extends GameObject{


    int length=50;
    public Wall(String img, int x, int y, GamePanel gamePanel) {
        super(img, x, y, gamePanel);
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,gamePanel);
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,length,length);
    }
}
import com.sun.javafx.scene.traversal.Direction;

import java.awt.*;
import java.util.ArrayList;

public class EnemyBullet extends Bullet{

    public EnemyBullet(String img, int x, int y, GamePanel gamePanel, Direction direction) {
        super(img, x, y, gamePanel, direction);
    }
    public void hitPlayer()
    {
        ArrayList<Tank> players=this.gamePanel.playerList;
        for(Tank player: players){
            if(this.getRec().intersects(player.getRec()))
            {
                //this.gamePanel.blastList.add(new Blast("", player.x-34, player.y-14,this.gamePanel ));
                this.gamePanel.playerList.remove(player);
                this.gamePanel.removeList.add(this);
                break;
            }
        }

    }
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        this.go();
        this.hitPlayer();
    }
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height);
    }
}
import com.sun.javafx.scene.traversal.Direction;

import java.awt.*;
import java.util.Random;

public class Bot extends Tank{
    int moveTime=0;
    public Bot(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, gamePanel, upImg, leftImg, rightImg, downImg);
    }

    public Direction getRandomDirection(){
        Random random=new Random();
        int rnum=random.nextInt(4);
        switch (rnum)
        {
            case 0:
                return Direction.UP;
            case 1:
                return Direction.DOWN;
            case 2:
                return Direction.LEFT;
            case 3:
                return Direction.RIGHT;
            default:
                return null;
        }
    }
    public void go()
    {
        attack();
        if(moveTime>=20)
        {
            direction=getRandomDirection();
            moveTime=0;
        }
        else moveTime++;
        switch (direction)
        {
            case LEFT:
                leftward();
                break;
            case RIGHT:
                rightward();
                break;
            case UP:
                upward();
                break;
            case DOWN:
                downward();
                break;
        }
    }
    public void attack()
    {
        Point p=getHeadPoint();
        Random random=new Random();
        int rnum=random.nextInt(400);
        if(rnum<10){
            this.gamePanel.bulletList.add(new EnemyBullet("src/image/炸弹.png", p.x, p.y, this.gamePanel, direction));
        }
    }
    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        go();
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height);
    }
}
import com.sun.javafx.scene.traversal.Direction;

import java.awt.*;
import java.util.ArrayList;

public class Bullet extends GameObject{
    int width=10;
    int height=10;
    int speed=7;
    Direction direction;

    public Bullet(String img, int x, int y, GamePanel gamePanel, Direction direction) {
        super(img, x, y, gamePanel);
        this.direction = direction;
    }
    public void leftword()
    {
        x-=speed;
    }
    public void upword()
    {
        y-=speed;
    }
    public void rightword()
    {
        x+=speed;
    }
    public void downword()
    {
        y+=speed;
    }
    public void go()
    {
        switch (direction)
        {
            case LEFT:
                leftword();
                break;
            case UP:
                upword();
                break;
            case DOWN:
                downword();
                break;
            case RIGHT:
                rightword();
                break;
        }
        this.hitWall();
        this.hitBase();
        this.moveToBorder();
    }
    public void hitBot()
    {
        ArrayList<Bot>bots=this.gamePanel.botList;
        for(Bot bot:bots){
            if(this.getRec().intersects(bot.getRec()))
            {
                //this.gamePanel.blastList.add(new Blast("", bot.x-34, bot.y-14,this.gamePanel ));
                this.gamePanel.botList.remove(bot);
                this.gamePanel.removeList.add(this);
                break;
            }
        }

    }
    public void hitWall()
    {
        ArrayList<Wall> walls=this.gamePanel.wallList;
        for(Wall wall:walls)
        {
            if(this.getRec().intersects(wall.getRec()))
            {
                this.gamePanel.wallList.remove(wall);
                this.gamePanel.removeList.add(this);
                break;

            }
        }
    }
    public void hitBase()
    {
        ArrayList<Base>bases=this.gamePanel.baseList;
        for(Base base:bases){
            if(this.getRec().intersects(base.getRec()))
            {
                this.gamePanel.baseList.remove(base);
                this.gamePanel.removeList.add(this);
                break;
            }
        }

    }
    public void moveToBorder()
    {
        if(x<0||x+width>this.gamePanel.getWidth())this.gamePanel.removeList.add(this);
        if(y<0||y+height>this.gamePanel.getHeight())this.gamePanel.removeList.add(this);

    }
    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        this.go();
        this.hitBot();
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height);
    }
}
import java.awt.*;

public abstract class GameObject {
    //图片
    public Image img;
    //坐标
    public int x;
    public int y;
    //游戏界面
    public GamePanel gamePanel;
    public GameObject(String img,int x,int y,GamePanel gamePanel)
    {
        this.img=Toolkit.getDefaultToolkit().getImage(img);
        this.x=x;
        this.y=y;
        this.gamePanel=gamePanel;
    }
    public abstract void paintSelf(Graphics g);
    public abstract Rectangle getRec();

}
import com.sun.javafx.scene.traversal.Direction;

import java.awt.*;
import java.util.ArrayList;

public abstract class Tank extends GameObject{
    //尺寸
    public int width=40;
    public int height=50;
    private int speed=3;
    public Direction direction =Direction.UP;
    private String upImg;
    private String leftImg;
    private String rightImg;
    private String downImg;
    public Tank(String img,int x,int y,GamePanel gamePanel,
                String upImg,String leftImg,String rightImg,String downImg)
    {
        super(img,x,y, gamePanel);
        this.upImg=upImg;
        this.leftImg=leftImg;
        this.rightImg=rightImg;
        this.downImg=downImg;
    }
    //攻击冷却时间
    private boolean attackCoolDown=true;
    //攻击冷却时间毫秒间隔1000ms
    private int attackCoolDownTime =1000;
    public void leftward(){
        setImg(leftImg);
        direction=Direction.LEFT;
        if(!hitWall(x-speed,y)&&!moveToBorder(x-speed,y))this.x-=speed;
    }
    public void upward(){
        setImg(upImg);
        direction=Direction.UP;
        if(!hitWall(x,y-speed)&&!moveToBorder(x,y-speed))this.y-=speed;
    }
    public void rightward(){
        setImg(rightImg);
        direction=Direction.RIGHT;
        if(!hitWall(x+speed,y)&&!moveToBorder(x+speed,y))this.x+=speed;
    }
    public void downward(){
        setImg(downImg);
        direction=Direction.DOWN;
        if(!hitWall(x,y+speed)&&!moveToBorder(x,y+speed))this.y+=speed;
    }
    private void setImg(String img) {
        this.img=Toolkit.getDefaultToolkit().getImage(img);
    }
    public void attack()
    {
        if(attackCoolDown) {
            Point p = this.getHeadPoint();
            Bullet bullet = new Bullet("src/image/爱心(1).png", p.x, p.y, this.gamePanel, direction);
            this.gamePanel.bulletList.add(bullet);
            new AttackCD().start();
        }
    }
    class AttackCD extends Thread{
        public void run()
        {
            attackCoolDown=false;
            try{
                Thread.sleep(attackCoolDownTime);
            }catch (Exception e){
                e.printStackTrace();
            }
            attackCoolDown=true;
            this.stop();
        }
    }
    public Point getHeadPoint(){
        switch (direction){
            case LEFT:
                return new Point(x,y+height/2);
            case RIGHT:
                return new Point(x+width,y+height/2);
            case UP:
                return new Point(x+width/2,y);
            case DOWN:
                return new Point(x+width/2,y+height);
            default:
                return null;

        }
    }
    public boolean hitWall(int x,int y)
    {
        ArrayList<Wall> walls=this.gamePanel.wallList;
        Rectangle next=new Rectangle(x,y,width,height);
        for(Wall wall:walls)
        {
            if(next.intersects(wall.getRec()))
            {
                return true;
            }

        }
        return false;
    }
    public boolean moveToBorder(int x,int y)
    {
        if(x<0)return true;
        else if(x+width>this.gamePanel.getWidth())return true;
        if(y<0)return true;
        else if(y+height>this.gamePanel.getHeight())return true;
        return false;
    }
    @Override
    public abstract void paintSelf(Graphics g);
    @Override
    public abstract Rectangle getRec();

}
import java.awt.*;
import java.awt.event.KeyEvent;

public  class PlayerOne extends Tank{
     boolean left;
     boolean down;
     boolean right;
     boolean up;

    public PlayerOne(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, gamePanel, upImg, leftImg, rightImg, downImg);
    }

    public void keyPressed(KeyEvent e)
    {
        int key=e.getKeyCode();
        switch(key){
            case KeyEvent.VK_A:
                left = true;
                break;
            case KeyEvent.VK_S:
                down=true;
                break;
            case KeyEvent.VK_D:
                right=true;
                break;
            case KeyEvent.VK_W:
                up=true;
                break;
            case KeyEvent.VK_SPACE:
                attack();
            default:
                break;

        }
    }
    public void keyReleased(KeyEvent e)
    {
        int key=e.getKeyCode();
        switch(key){
            case KeyEvent.VK_A:
                left=false;
                break;
            case KeyEvent.VK_S:
                down=false;
                break;
            case KeyEvent.VK_D:
                right=false;
                break;
            case KeyEvent.VK_W:
                up=false;
                break;
            default:
                break;
        }
    }
    public void move()
    {
        if(left)leftward();
        else if (right)rightward();
        else if(up)upward();
        else if(down)downward();
    }
    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        move();
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height);
    }
}