美文网首页
JAVA实现坦克大战小游戏——坦克动起来

JAVA实现坦克大战小游戏——坦克动起来

作者: 让你变好的过程从来都不会很舒服 | 来源:发表于2021-08-18 10:25 被阅读0次

    坦克实体

    package com.tank.tankgame2;
    
    /**
     * 坦克实体类
     */
    public class Tank {
    
        private int x;
        private int y;
        private int direct;
        private int speed =1; // 坦克速度
        // 上下左右移动方法
        public void moveUp(){
            y-=speed;
        }
        public void moveRight(){
            x+=speed;
        }
        public void moveDown(){
            y+=speed;
        }
        public void moveLeft(){
            x-=speed;
        }
    
        public int getSpeed() {
            return speed;
        }
    
        public void setSpeed(int speed) {
            this.speed = speed;
        }
    
        public Tank(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public int getDirect() {
            return direct;
        }
    
        public void setDirect(int direct) {
            this.direct = direct;
        }
    
        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 class Hero extends Tank {
    
    
        public Hero(int x, int y) {
            super(x, y);
        }
    }
    

    坦克大战的绘图区域

    package com.tank.tankgame2;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    /**
     * 坦克大战的绘图区域
     */
    public class MyPanel extends JPanel implements KeyListener {
    
        Hero hero = null;
    
        public MyPanel(){
            hero =  new Hero(100,100); // 初始化自己的坦克
            hero.setSpeed(5);
    
        }
    
        @Override
        public void  paint (Graphics g){
            super.paint(g);
    
            g.fillRect(0,0,1000,750); // 填充矩形,默认黑色
            // 画出坦克-封装方法
            drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),1);
        }
    
        /**
         *  封装的坦克方法
         * @param x 坦克的左上角 x坐标
         * @param y 坦克的左上角 y坐标
         * @param g 画笔
         * @param direct 坦克方向 上下左右
         * @param type 坦克类型
         */
        public void drawTank(int x,int y,Graphics g,int direct ,int type){
    
            switch (type){
                case 0: // 敌人的坦克
                    g.setColor(Color.cyan);
                    break;
                case 1: // 我们的坦克
                    g.setColor(Color.YELLOW);
                    break;
            }
            // 根据坦克的方向绘制坦克
            switch (direct){
                case 0: // 0表示向上
                    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 1: // 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+30,y+20,x+60,y+20); // 画出炮筒
                    break;
                case 2: // 2表示向下
                    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 3: // 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+30,y+20,x,y+20); // 画出炮筒
                    break;
                default:
                    System.out.println("暂时没有处理");
            }
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
    
        }
        // 当按下某个键时、该方法会触发
        @Override
        public void keyPressed(KeyEvent e) {
            // 改变坦克方向
            if(e.getKeyCode() == KeyEvent.VK_W){// w 键
                hero.setDirect(0);
                hero.moveUp();
            }else if(e.getKeyCode() == KeyEvent.VK_D){// d 键
                hero.setDirect(1);
                hero.moveRight();
            }else if(e.getKeyCode() == KeyEvent.VK_S){// s 键
                hero.setDirect(2);
                hero.moveDown();
            }else if(e.getKeyCode() == KeyEvent.VK_A){// a 键
                hero.setDirect(3);
                hero.moveLeft();
            }
            this.repaint();
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
    
        }
    }
    
    

    项目启动主函数

    package com.tank.tankgame2;
    
    import javax.swing.*;
    
    public class JqTankGame02 extends JFrame {
    
        // 定义Mypanel
        MyPanel mp = null;
        public static void main(String[] args) {
            JqTankGame02 jqTankGame01 = new JqTankGame02(); // 游戏区域生成
    
        }
        public JqTankGame02(){
            mp = new MyPanel();
            this.add(mp);
            this.setSize(1000,750);
            this.addKeyListener(mp);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:JAVA实现坦克大战小游戏——坦克动起来

          本文链接:https://www.haomeiwen.com/subject/cotavltx.html