坦克大战1.3

坦克大战1.3_第1张图片



import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 许泽鑫
 * @data 2019/11/9 - 21:13
 */
public class TankClient extends JFrame {
    public static final  int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT = 600;

    public TankClient(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(GAME_WIDTH,GAME_HEIGHT);
        setLocationRelativeTo(null);
        setResizable(false);

        setBackground(Color.GREEN);
        addKeyListener(new KeyMonitor());
        setVisible(true);
        new Thread(new PaintThread()).start();

    }
    Tank t1 = new Tank(50,50,this);
    List<PaoDan> paodans = new ArrayList<>();

    @Override
    public void paint(Graphics g){

        t1.draw(g);
        g.drawString("已发射炮弹数量:" + paodans.size(),10,50);
        for(int i = 0;i < paodans.size();i++){
            PaoDan m = paodans.get(i);
            m.draw(g);
        }
    }
    private  class  PaintThread implements Runnable{

        @Override
        public void run() {
            while (true){
                repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    Image a = null;
    @Override
    public void update(Graphics g){
        if(a == null){
            a = this.createImage(GAME_WIDTH,GAME_HEIGHT);

        }
        Graphics g1 = a.getGraphics();
        Color c = g1.getColor();
        g1.setColor(Color.green);
        g1.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        g1.setColor(c);

        paint(g1);
        g.drawImage(a,0,0,null);
    }
    @Override
    public void repaint(){
        super.repaint();
        update(getGraphics());

    }
    private class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e){
            t1.tankRun(e);

//            if(key == KeyEvent.VK_RIGHT){
//                x += 5;
//            }
//            if(key == KeyEvent.VK_LEFT){
//                x -= 5;
//            }
//            if(key == KeyEvent.VK_UP){
//                y -= 5;
//            }
//            if(key == KeyEvent.VK_DOWN){
//                y += 5;
//            }


        }

        @Override
        public void keyReleased(KeyEvent e) {
            super.keyReleased(e);
            t1.keyReleased(e);
        }


    }


    public static void main(String[] args) {

        new TankClient();
    }
}

import java.awt.*;

/**
 * @author 许泽鑫
 * @data 2019/11/10 - 16:07
 */
public class PaoDan {
    static final int PAODAN_SPEED = 10;
    static final int WIDTH = 10;
    static final int HEIGHT = 10;

    int x;
    int y;
    Tank.Direction dir;
    public PaoDan(int x,int y ,Tank.Direction dir){
        this.x = x;
        this.y = y;
        this.dir = dir;
    }
    public void draw(Graphics g){



        Color c = g.getColor();


        g.setColor(Color.BLUE);
        g.fillOval(x + Tank.WIDTH/2 - this.WIDTH/2,y + Tank.HEIGHT/2 - this.HEIGHT/2, WIDTH, HEIGHT);
        g.setColor(c);
        move();
    }

    private void move() {
        switch (dir){
            case u: y -= PAODAN_SPEED;
                break;
            case d: y += PAODAN_SPEED;
                break;
            case l: x -= PAODAN_SPEED;
                break;
            case r: x += PAODAN_SPEED;
                break;
            case lu: x -= PAODAN_SPEED;y -= PAODAN_SPEED;
                break;
            case ru: y -= PAODAN_SPEED; x += PAODAN_SPEED;
                break;
            case ld: y += PAODAN_SPEED; x -= PAODAN_SPEED;
                break;
            case rd: x += PAODAN_SPEED; y += PAODAN_SPEED;
                break;


        }
    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.lang.invoke.SwitchPoint;

/**
 * @author 许泽鑫
 * @data 2019/11/10 - 11:08
 */
public class Tank   {
    private static final int TANK_SPEED = 5;
    boolean bu = false,bd = false,bl = false,br = false;
     static final int WIDTH = 50;
    static final int HEIGHT = 50;

    TankClient tc;

    enum Direction {u,d,l,r,lu,ld,ru,rd,stop;};
    private Direction ptdir = Direction.r;
    private Direction dir = Direction.stop;
    int x;

    int y;
    public void move(){
        switch (dir){
            case u: y -= TANK_SPEED;
                break;
            case d: y += TANK_SPEED;
                break;
            case l: x -= TANK_SPEED;
                break;
            case r: x += TANK_SPEED;
                break;
            case lu: x -= TANK_SPEED;y -= TANK_SPEED;
                break;
            case ru: y -= TANK_SPEED; x += TANK_SPEED;
                break;
            case ld: y += TANK_SPEED; x -= TANK_SPEED;
                break;
            case rd: x += TANK_SPEED; y += TANK_SPEED;
                break;
            case stop:
                break;

        }
        if(this.dir != Direction.stop){
            this.ptdir = this.dir;
        }
    }

    public Tank(int x, int y,TankClient tc) {
        this.x = x;
        this.y = y;
         this.tc = tc;


    }


    public void draw(Graphics g) {
        Color c = g.getColor();


        g.setColor(Color.RED);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(c);
        switch (ptdir){
            case u: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x + WIDTH/2,y-10);
                break;
            case d: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x + WIDTH/2,y + HEIGHT+10);
                break;
            case l: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x-10,y+HEIGHT/2);
                break;
            case r: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x + WIDTH+10,y+HEIGHT/2);
                break;
            case lu: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x,y);
                break;
            case ru: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x+WIDTH,y);
                break;
            case ld: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x,y+HEIGHT);
                break;
            case rd: g.drawLine(x + WIDTH /2,y +HEIGHT/2,x+WIDTH,y+HEIGHT);
                break;


        }


        move();

    }

    public void tankRun(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_RIGHT: br = true;
                break;
            case KeyEvent.VK_LEFT: bl = true;
                break;
            case KeyEvent.VK_UP: bu = true;
                break;
            case KeyEvent.VK_DOWN: bd = true;
                break;
        }

    locateDirection();


    }
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_CONTROL:
                tc.paodans.add(fire());
                break;
            case KeyEvent.VK_RIGHT:
                br = false;
                break;
            case KeyEvent.VK_LEFT:
                bl = false;
                break;
            case KeyEvent.VK_UP:
                bu = false;
                break;
            case KeyEvent.VK_DOWN:
                bd = false;
                break;
        }
        locateDirection();
    }
    public void locateDirection(){
        if(!br && !bl && !bd && bu){
            dir = Direction.u;

        }
        else if(br && !bl && !bd && !bu){
            dir = Direction.r;

        }
        else if(!br && bl && !bd && !bu){
            dir = Direction.l;

        }
        else if(!br && !bl && bd && !bu){
            dir = Direction.d;

        }
        else if(br && !bl && bd && !bu){
            dir = Direction.rd;

        }
        else if(!br && bl && bd && !bu){
            dir = Direction.ld;

        }
        else if(br && !bl && !bd && bu){
            dir = Direction.ru;

        }
        else if(!br && bl && !bd && bu){
            dir = Direction.lu;

        }
        else  if(!br && !bl && !bd && !bu){
            dir = Direction.stop;
        }
    }
    public PaoDan fire(){
        PaoDan m =  new PaoDan(this.x,this.y,ptdir);
        return m;
    }

}

你可能感兴趣的:(坦克大战)