[ java ] 坦克大战 2.0 ~

介绍

坦克大战2.0
内容:

  • 加入多线程内容–>按键使我方坦克发射子弹

3.0版本已上传

画框

package com.hspedu.tankgame03;

import javax.swing.*;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-14-18:24
 * @Version
 *///第三版优化:我方发射子弹
public class ZxkTankGame03 extends JFrame {//画框
    //定义画板
    Mypanel mp = null;

    public static void main(String[] args) {
        ZxkTankGame03 zxkTankGame01 = new ZxkTankGame03();//新建画框
    }
    public ZxkTankGame03(){
        mp = new Mypanel();//新建画板
        new Thread(mp).start();//开启画板重绘线程
        this.add(mp);//添加画板到画框
        setSize(1000, 750);//画板大小
        this.addKeyListener(mp);//监听画板的键盘
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭画框的同时关闭jvm
        setVisible(true);//可视化

    }
}

画板

package com.hspedu.tankgame03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-14-18:17
 * @Version
 *///画板
public class Mypanel extends JPanel implements KeyListener,Runnable {//方法的参数坐标都在左上角,除了画字符串在左下角
    //定义自己的坦克
    Hero hero = null;
    Vector<EnemyTank> enemyTanks = new Vector<EnemyTank>();//新建敌人坦克集合
    public Mypanel(){//画板构造器内部新建坦克
        hero = new Hero(100, 100,'w',20);
        for (int i = 0; i < 3; i++) {
            enemyTanks.add(new EnemyTank(200 + i * 100, 0, 's', 5));
        }
    }

    @Override
    public void paint(Graphics g) {//重写父类画画方法
        super.paint(g);
        System.out.println("重画过");
        g.fillRect(0,0,1000,750);//画板填充颜色默认黑色

        //绘制敌方坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            drawTank(enemyTanks.get(i).getX(),enemyTanks.get(i).getY(),g,enemyTanks.get(i).getDirection(),false);
        }
        //绘制我方坦克,后创建的坦克可以显示在先创建的上层
        drawTank(hero.getX(), hero.getY(),g,hero.getDirection(),true);
        //绘制子弹
        if (hero.bullet != null && hero.bullet.alive) {
            g.draw3DRect(hero.bullet.x,hero.bullet.y,2,2,false);
        }
    }
    public void drawTank(int x,int y,Graphics g,char direct,boolean type){
        //坦克类型不同,颜色不同
        if (type) {
            g.setColor(Color.cyan);
        } else {
            g.setColor(Color.yellow);
        }
        //四种方向
        switch (direct) {
            case 'w'://坦克朝上
                g.fill3DRect(x, y, 10, 60, false);//左车轮
                g.fill3DRect(x + 30, y, 10, 60, false);//右车轮
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//车中间部分
                g.fillOval(x + 10, y + 20, 20, 20);//车盖
                g.drawLine(x + 20, y + 30, x + 20, y);//炮筒
                break;
            case 's':
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y+60);
                break;
            case 'a':
                g.fill3DRect(x-10, y+10, 60, 10, false);
                g.fill3DRect(x-10, y+40, 60, 10, false);
                g.fill3DRect(x, y + 20, 40, 20, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x-10, y+30);
                break;
            case 'd':
                g.fill3DRect(x-10, y+10, 60, 10, false);
                g.fill3DRect(x-10, y+40, 60, 10, false);
                g.fill3DRect(x, y + 20, 40, 20, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x+50, y+30);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override

    public void keyPressed(KeyEvent e) {//键盘事件
        if (e.getKeyCode() == KeyEvent.VK_D) {//坦克车头朝右且位置往右挪
            hero.moveRight();
            hero.setDirection('d');
        }else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.moveLeft();
            hero.setDirection('a');
        }else if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.moveUp();
            hero.setDirection('w');
        }else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.moveDown();
            hero.setDirection('s');
        }
        //监听器调用Hero的射击方法
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shotEnemy();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    //每休眠100ms画板重绘
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}

Tank类

package com.hspedu.tankgame03;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-14-18:13
 * @Version
 *///坦克类
public class Tank {
    private int x;
    private int y;
    private char direction;
    private int speed;
    public Tank(int x, int y,char direction,int speed) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.speed = speed;
    }

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

    public char getDirection() {
        return direction;
    }

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

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

Hero类

package com.hspedu.tankgame03;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-14-18:15
 * @Version
 *///自己的坦克
public class Hero extends Tank{
    //构造器指明Hero的初始位置
    public Hero(int x, int y,char direction,int speed) {
        super(x, y,direction,speed);
    }

    //填弹(空弹)
    Bullet bullet = null;
    //射击方法(空弹加芯):根据坦克的当前位置(炮筒头部)填入子弹的初始位置
    public void shotEnemy(){
        switch (this.getDirection()) {
            case 'w':
                bullet = new Bullet(getX() + 20, getY(), 'w');
                break;
            case 's':
                bullet = new Bullet(getX() + 20, getY()+60, 's');
                break;
            case 'a':
                bullet = new Bullet(getX() -10, getY()+30, 'a');
                break;
            case 'd':
                bullet = new Bullet(getX() + 50, getY()+30, 'd');
                break;
        }
        //启动子弹线程
        new Thread(bullet).start();
    }
}

EnemyTank类

package com.hspedu.tankgame03;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-14-22:19
 * @Version
 *///敌人坦克
public class EnemyTank extends Tank{
    public EnemyTank(int x, int y, char direction, int speed) {
        super(x, y, direction, speed);
    }
}

Bullet类

package com.hspedu.tankgame03;

import java.io.File;

/**
 * @ClassName
 * @Description
 * @Author zxk
 * @DateTime 2022-01-19-14:39
 * @Version
 *///子弹类
public class Bullet implements Runnable{
    int x;
    int y;
    int speed = 5;
    char direction;
    boolean alive = true;

    //构造器指定子弹初始初始位置和方向
    public Bullet(int x, int y, char direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    //根据方向改变子弹射出后的位置(子弹轨迹)
    @Override
    public void run() {
        while (true) {
            //每休眠50ms改变子弹位置
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch (direction) {
                case 'w':
                    y-=speed;
                    break;
                case 's':
                    y+=speed;
                    break;
                case 'a':
                    x-=speed;
                    break;
                case 'd':
                    x+=speed;
                    break;
            }
            //子弹脱离画框当即销毁
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750)) {
                alive = false;
                System.out.println("子弹线程终止");
                break;
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值