Java学习Day15 坦克大战ver0.3

Java学习Day15 坦克大战ver0.3

新增功能 当玩家按下j键就发射一颗子弹

import javax.swing.*;

/*
 *@author   yuhuw
 *@date     2023/10/9 19:45
 *@version  1.0
 */
/*
当玩家按下j键时就发射一颗子弹
 */
public class TankGame03 extends JFrame {
    //定义一个面板
    MyPanel mp = null;

    public static void main(String[] args) {
        TankGame03 tankGame03 = new TankGame03();

    }

    public TankGame03() {
        mp = new MyPanel();
        Thread thread = new Thread(mp);
        thread.start();
        //启动mp
        this.add(mp);//加入游戏的绘图区域

        this.setSize(1000, 750);
        this.addKeyListener(mp);//增加一个监听
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

具体实现

在MyPanel类监听键盘是否传入j

/*
 *@author   yuhuw
 *@date     2023/10/9 19:54
 *@version  1.0
 */
//绘图
//为了不停地重绘子弹 需要让MyPanel成为线程
public class MyPanel extends JPanel implements KeyListener, Runnable {
    //定义p1 tank
    P1 p1 = null;

    //    Bullet bullet = null;
    //定义enemy tank
    Vector<EnemyTank> enemyTanks = new Vector<>();

    int enemyTankSize = 3;

    public MyPanel() {

        p1 = new P1(100, 100);//初始化自己坦克
        p1.setSpeed(40);
//        bullet = new Bullet(105, 95);
//        bullet.setSpeed(40);


        for (int i = 0; i < enemyTankSize; i++) {
            enemyTanks.add(new EnemyTank(300 * (i + 1), 0));
            //也可以在这里初始化tank方向
            /*
            EnemyTank enemyTank = new EnemyTank((300*(i+1),0));
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
             */
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.fillRect(0, 0, 1000, 750);//填充矩形 默认是黑色
        //画出p1的tank

        drawTank(p1.getX(), p1.getY(), g, p1.getDirection(), 0);

        //画出enemy的tank
        /*
        1.enemy tank可以单开一个类继承tank并且有自己的属性和方法
        2.敌人tank数量多且多线程考虑安全使用vector
        3.遍历取出所有集合的tank
         */
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
        }
        //画出P1的bullet
        if (p1.shot != null && p1.shot.isLive == true) {
            System.out.println("子弹绘制");
            g.setColor(Color.yellow);
            g.fill3DRect(p1.shot.x, p1.shot.y, 2, 2, false);

        }
//        drawBullet(bullet.getX(), bullet.getY(),g,bullet.getDirection(),0 );


    }

    //编写画出tank的方法
    /*
    1.x y 左上角坐标
    2.g 画笔
    3.direction 方向
    4.type 类型
    */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        switch (type) {
            case 0://我们的tank
                g.setColor(Color.yellow);
                break;
            case 1://敌人的tank
                g.setColor(Color.red);
                break;
        }

        //根据tank的方向绘制不同的tank
        switch (direction) {
            case 0://表示向上
                //填充当前颜色的高亮矩形   宽    高       表面色泽
                g.fill3DRect(x, y, 10, 60, false);//画出tank左轮
                g.fill3DRect(x + 30, y, 10, 60, false);//tank右轮
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//tank身体
                g.fillOval(x + 10, y + 20, 20, 20);//tank出口盖
                g.drawLine(x + 20, y + 20, x + 20, y - 20);
                //画出tank炮筒
                break;
            case 1://表示向右
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 20, y + 20, x + 80, y + 20);
                break;
            case 2://向下
                g.fill3DRect(x, y, 10, 60, false);//画出tank左轮
                g.fill3DRect(x + 30, y, 10, 60, false);//tank右轮
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//tank身体
                g.fillOval(x + 10, y + 20, 20, 20);//tank出口盖
                g.drawLine(x + 20, y + 20, x + 20, y + 80);
                break;
            case 3://向左
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 20, y + 20, x - 20, y + 20);
                break;
            default:
                System.out.println("没有处理");
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //读取键盘的wasd改变tank的方向
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {//键盘按键 0w 2s 3a 1d
            p1.setDirection(0);
            //p1.setY(getY()-1);不优雅
//            bullet.setDirection(0);
            p1.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            p1.setDirection(2);
//            bullet.setDirection(2);
            p1.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            p1.setDirection(3);
//            bullet.setDirection(3);
            p1.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            p1.setDirection(1);
//            bullet.setDirection(1);
            p1.moveRight();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            p1.shotEnemyTank();
        }
//        if (e.getKeyCode() == KeyEvent.VK_J) {
//            bullet.setSpeed(4000);
//            bullet.move(bullet.getDirection());
//
//        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

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

创建一个方法Shot类发射子弹

public class Shot implements Runnable {
    int x;
    int y;
    int direction = 0;
    int speed = 2;
    boolean isLive = true;//子弹是否存活

    public Shot(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    @Override
    public void run() {
        while (true) {
            switch (direction) {
                case 0:
                    y -= speed;
                    break;
                case 1:
                    x += speed;
                    break;
                case 2:
                    y += speed;
                    break;
                case 3:
                    x -= speed;
                    break;
            }
            System.out.println(x+" "+y);
            //离开面板就退出
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750)) {
                isLive = false;
                break;
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

其余类

Tank

public class Tank {
    private int x;//横纵坐标
    private int y;

    private int direction = 0;//坦克方向

    private int speed = 1;

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    //移动方法
    public void moveUp(){
        y-=speed;
    }
    public void moveDown(){
        y+=speed;
    }
    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }
    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

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

    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;
    }
}

P1

public class P1 extends Tank {

    //定义一个shot对象
    Shot shot = null;

    public P1(int x, int y) {
        super(x, y);
    }

    public void shotEnemyTank() {
        //创建shot对象 根据P1的位置确定
        switch (getDirection()) {
            case 0:
                shot = new Shot(getX() + 20, getY() - 20, 0);
                break;
            case 1:
                shot = new Shot(getX() + 80, getY() +20, 1);
                break;
            case 2:
                shot = new Shot(getX() + 20, getY() + 80, 2);
                break;
            case 3:
                shot = new Shot(getX() - 20, getY() + 20, 3);
                break;
        }
        //启动shot
        Thread thread = new Thread(shot);
        thread.start();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值