• java小游戏-java小游戏-黄金矿工


    连接视频

    1 窗口绘制

    创建GameWin类

    public class GameWin extends JFrame {
        public void launch(){
            this.setVisible(true);
            this.setSize(500,500);
            this.setLocationRelativeTo(null);
            this.setTitle("黄金矿工");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            GameWin gameWin = new GameWin();
            gameWin.launch();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2 绘制图片

    先导入图片到项目中img文件夹中

    创建Bg类

    //背景类
    public class Bg {
        Image bg = Toolkit.getDefaultToolkit().getImage("img/bg.jpg");
        Image bg1 = Toolkit.getDefaultToolkit().getImage("img/bg1.jpg");
        Image peo = Toolkit.getDefaultToolkit().getImage("img/peo.png");
        
        public void paintSelf(Graphics g){
            g.drawImage(bg1,0,0,null);
            g.drawImage(bg,0,200,null);
            g.drawImage(peo,310,50,null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在GameWin中绘制

    Bg bg = new Bg();
    
    @Override
    public void paint(Graphics g) {
        bg.paintSelf(g);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3 红线绘制

    创建Line类

    //红线类
    public class Line {
        //起点坐标
        int x = 380,y=180;
        //终点坐标
        int endX = 500,endY = 500;
    
        public void paintSelf(Graphics g){
            g.setColor(Color.red);
            g.drawLine(x,y,endX,endY);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在GameWin类中引用

    Line line = new Line();
    
    @Override
    public void paint(Graphics g) {
        bg.paintSelf(g);
        line.paintSelf(g);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4 红线摇摆

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    在Line类添加参数

    //线长
    double length = 100;
    double n = 0;
    //方向
    int dir = 1;
    
    public void paintSelf(Graphics g){
        if(n < 0.1){
            dir = 1;
        }else if(n > 0.9){
            dir = -1;
        }
        n = n + 0.005 * dir;
    
        endX = (int) (x + length*Math.cos(n * Math.PI));
        endY = (int) (y + length*Math.sin(n * Math.PI));
    
        g.setColor(Color.red);
        g.drawLine(x,y,endX,endY);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在GameWin类中launch方法添加死循环

    public void launch(){
       ...
    
        while (true){
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5 红线抓取

    在Line类添加参数

    //状态 0 左右摇摆 1 抓取 2 收回
    int state;
    
    public void paintSelf(Graphics g){
        switch (state){
            case 0:
                if(n < 0.1){
                    dir = 1;
                }else if(n > 0.9){
                    dir = -1;
                }
                n = n + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if(length< 500){
                    length = length + 10;
                    lines(g);
                }else { state = 2; }
                break;
            case 2:
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                }else { state = 0; }
                break;
            default:
        }
    }
    
    void lines(Graphics g){
        endX = (int) (x + length*Math.cos(n * Math.PI));
        endY = (int) (y + length*Math.sin(n * Math.PI));
        g.setColor(Color.red);
        g.drawLine(x,y,endX,endY);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在GameWin类中launch方法添加鼠标监听事件

    public void launch(){
      ...
    
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton() == 1){//鼠标左键点击
                    line.state = 1;
                }
            }
        });
    
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6 创建金块

    创建Object父类

    //金矿、石头
    public class Object {
        //坐标
        int x,y;
        //宽高
        int width,height;
        //图片
        Image img;
    
        public void paintSelf(Graphics g){
            g.drawImage(img,x,y,null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建Gold子类

    //金矿类
    public class Gold extends Object{
        public Gold(){
            this.x = 300;
            this.y = 500;
            this.width = 52;
            this.height = 52;
            this.img = Toolkit.getDefaultToolkit().getImage("img/gold1.gif");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在GameWin类引用

    Gold gold = new Gold();
    
    @Override
    public void paint(Graphics g) {
        bg.paintSelf(g);
        line.paintSelf(g);
        gold.paintSelf(g);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7 双缓存技术

    在GameWin类添加画布

    Image offScreenImage;
    
    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768,1000);
        Graphics graphics = offScreenImage.getGraphics();
    
        bg.paintSelf(graphics);
        line.paintSelf(graphics);
        gold.paintSelf(graphics);
    
        g.drawImage(offScreenImage,0,0,null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8 抓取判断

    在Line类添加判断

    GameWin frame;
    
    public Line(GameWin frame) {
        this.frame = frame;
    }
    
    //碰撞检测,检测物体是否被抓取
    void logic() {
        if (endX > this.frame.gold.x && endX < this.frame.gold.x + this.frame.gold.width
                && endY > this.frame.gold.y && endY < this.frame.gold.y + this.frame.gold.height) {
            System.out.println("11111111");
        }
    }
    
    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
        ....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在GameWin类修改Line对象

    Line line = new Line(this);
    
    • 1

    9 抓取返回

    在Line类添加判断

    //状态 0 左右摇摆 1 抓取 2 收回 3 抓取返回
    int state;
    
    //碰撞检测,检测物体是否被抓取
    void logic() {
        if (endX > this.frame.gold.x && endX < this.frame.gold.x + this.frame.gold.width
                && endY > this.frame.gold.y && endY < this.frame.gold.y + this.frame.gold.height) {
           state = 3;
        }
    }
    
    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
           ...
            case 3:
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                    this.frame.gold.x = endX - 26;
                    this.frame.gold.y = endY;
                } else {
                    this.frame.gold.x = -150;
                    this.frame.gold.y = -150;
                    state = 0;
                }
                break;
            default:
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    10 多个金块

    在GameWin类添加集合

    //    Gold gold = new Gold();
    //存储金块,石块
    List<Object> objectList = new ArrayList<>();
    {
        for (int i = 0; i < 3; i++) {
            objectList.add(new Gold());
        }
    }
    
    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768,1000);
        Graphics graphics = offScreenImage.getGraphics();
    
        bg.paintSelf(graphics);
        line.paintSelf(graphics);
        //gold.paintSelf(graphics);
        for (Object obj : objectList) {
            obj.paintSelf(graphics);
        }
    
        g.drawImage(offScreenImage,0,0,null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在Line类修改

    //碰撞检测,检测物体是否被抓取
    void logic() {
        for (Object gold : this.frame.objectList) {
            if (endX > gold.x && endX < gold.x + gold.width
                    && endY > gold.y && endY < gold.y + gold.height) {
                state = 3;
            }
        }
    
    }
    
    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
            ...
            case 3:
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        gold.x = endX - 26;
                        gold.y = endY;
                        if(length <= 100){
                            gold.x = -150;
                            gold.y = -150;
                            state = 0;
                        }
                    }
                }
                break;
            default:
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在Gold类修改

    public Gold(){
        this.x = (int)(Math.random()*700);
        this.y =  (int)(Math.random()*550 + 300);
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    11 Bug金块消失

    在Object类添加参数

    //标记是否可以移动 false 不可移动 true 可以移动
    boolean flag = false;
    
    • 1
    • 2

    在Gold类设置参数

    public Gold(){
        this.x = (int)(Math.random()*700);
        this.y =  (int)(Math.random()*550 + 300);
        this.width = 52;
        this.height = 52;
        this.flag = false;
        ..
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在Line类添加设置

    //碰撞检测,检测物体是否被抓取
    void logic() {
        for (Object gold : this.frame.objectList) {
            if (endX > gold.x && endX < gold.x + gold.width
                    && endY > gold.y && endY < gold.y + gold.height) {
                state = 3;
                gold.flag = true;
            }
        }
    
    }
    
    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
            ...
            case 3:
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        if(gold.flag){
                            gold.x = endX - 26;
                            gold.y = endY;
                            if(length <= 100){
                                gold.x = -150;
                                gold.y = -150;
                                gold.flag = false;
                                state = 0;
                            }
                        }
                    }
                }
                break;
            default:
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    12 石块创建

    创建Rock

    //石块类
    public class Rock extends Object{
        public Rock(){
            this.x = (int)(Math.random()*700);
            this.y =  (int)(Math.random()*550 + 300);
            this.width = 71;
            this.height = 71;
            this.flag = false;
            this.img = Toolkit.getDefaultToolkit().getImage("img/rock1.png");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在GameWin类中创建

    {
        for (int i = 0; i < 3; i++) {
            objectList.add(new Gold());
        }
        for (int i = 0; i < 3; i++) {
            objectList.add(new Rock());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在Object类添加方法

    public int getWidth() {
        return width;
    }
    
    • 1
    • 2
    • 3

    在Line类中调用

    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
           ...
            case 3:
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        if(gold.flag){
                            gold.x = endX - gold.getWidth()/2;
                            gold.y = endY;
                            if(length <= 100){
                                gold.x = -150;
                                gold.y = -150;
                                gold.flag = false;
                                state = 0;
                            }
                        }
                    }
                }
                break;
            default:
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    13 拉取速度

    在Object类添加参数

    //质量
    int m;
    
    • 1
    • 2

    在Gold类设值

    public Gold(){
        this.m = 80;
        ...
    }
    
    • 1
    • 2
    • 3
    • 4

    在Rock类设值

    public Rock(){
        this.m = 150;
        ...
    }
    
    • 1
    • 2
    • 3
    • 4

    在Linel类修改判断

    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
            case 0:
                if (n < 0.1) {
                    dir = 1;
                } else if (n > 0.9) {
                    dir = -1;
                }
                n = n + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if (length < 500) {
                    length = length + 5;
                    lines(g);
                } else {
                    state = 2;
                }
                break;
            case 2:
                if (length > 100) {
                    length = length - 5;
                    lines(g);
                } else {
                    state = 0;
                }
                break;
            case 3:
                int m = 1;
                if (length > 100) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        if(gold.flag){
                            m = gold.m;
                            gold.x = endX - gold.getWidth()/2;
                            gold.y = endY;
                            if(length <= 100){
                                gold.x = -150;
                                gold.y = -150;
                                gold.flag = false;
                                state = 0;
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    14 多种金块

    在Gold类创建GoldMini外部类和GoldPlus外部类

    public class Gold extends Object{
    	...
    }
    class GoldMini extends Gold{
        public GoldMini(){
            this.width = 36;
            this.height = 36;
            this.flag = false;
            this.m = 30;
            this.img = Toolkit.getDefaultToolkit().getImage("img/gold0.gif");
        }
    }
    class GoldPlus extends Gold{
        public GoldPlus(){
            this.x = (int)(Math.random()*650);
            this.width = 105;
            this.height = 105;
            this.flag = false;
            this.m = 120;
            this.img = Toolkit.getDefaultToolkit().getImage("img/gold2.gif");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在GameWin类随机生成多种金块

    {
        for (int i = 0; i < 11; i++) {
            double random = Math.random();
            if (random <0.3 ) {
                objectList.add(new GoldMini());
            }else if(random < 0.7 ){
                objectList.add(new Gold());
            } else {
                objectList.add(new GoldPlus());
            }
        }
        for (int i = 0; i < 3; i++) {
            objectList.add(new Rock());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    15 物体堆叠

    在Objectl类添加方法

    //获取矩形,用于检测判断物体重叠
    public Rectangle getRec(){
        return new Rectangle(x,y,width,height);
    }
    
    • 1
    • 2
    • 3
    • 4

    在GameWin类修改生成金块代码

    {
        //是否可以放置
        boolean ifPlace = true;
        for (int i = 0; i < 11; i++) {
            double random = Math.random();
            Gold gold;//存放当前生成的金块
            if (random <0.3 ) {
                gold = new GoldMini();
            }else if(random < 0.7 ){
                gold = new Gold();
            } else {
                gold = new GoldPlus();
            }
            for (Object obj : objectList) {
                if(gold.getRec().intersects(obj.getRec())){
                    //不能放置,需要重新生成
                    ifPlace = false;
                }
            }
            if(ifPlace){
                objectList.add(gold);
            }else { ifPlace = true; i--; }
        }
        for (int i = 0; i < 5; i++) {
            Rock rock = new Rock();
            for (Object obj : objectList) {
                if(rock.getRec().intersects(obj.getRec())){
                    //不能放置,需要重新生成
                    ifPlace = false;
                }
            }
            if(ifPlace){
                objectList.add(rock);
            }else { ifPlace = true; i--; }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    16 红线完善

    在Line类中修改

    //线长最小值
    double min_length = 100;
    //线长最小值
    double max_length = 750;
    
    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
            case 0:
                if (n < 0.1) {
                    dir = 1;
                } else if (n > 0.9) {
                    dir = -1;
                }
                n = n + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if (length < max_length) {
                    length = length + 5;
                    lines(g);
                } else {
                    state = 2;
                }
                break;
            case 2:
                if (length > min_length) {
                    length = length - 5;
                    lines(g);
                } else {
                    state = 0;
                }
                break;
            case 3:
                int m = 1;
                if (length > min_length) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        if(gold.flag){
                            m = gold.m;
                            gold.x = endX - gold.getWidth()/2;
                            gold.y = endY;
                            if(length <= min_length){
                                gold.x = -150;
                                gold.y = -150;
                                gold.flag = false;
                                state = 0;
                            }
                        }
                    }
                }
                ...
                break;
            default:
        }
    }
        
    //绘制方法
    void lines(Graphics g) {
        endX = (int) (x + length * Math.cos(n * Math.PI));
        endY = (int) (y + length * Math.sin(n * Math.PI));
        g.setColor(Color.red);
        g.drawLine(x-1, y, endX-1, endY);
        g.drawLine(x, y, endX, endY);
        g.drawLine(x+1, y, endX+1, endY);
        g.drawImage(hook,endX-36,endY-2,null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    17 积分设置

    在Object类添加参数

     //积分
    int count;
    
    • 1
    • 2

    在Gold类设值

     public Gold(){
            this.count = 4;
            ...
    public GoldMini(){
            this.count = 2;
    		...
    public GoldPlus(){
            this.count = 8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在Rock类设值

    public Rock(){
            this.count = 1;
            ...
    
    • 1
    • 2
    • 3

    在Line类加分

    public void paintSelf(Graphics g) {
          logic();
          switch (state) {
              ...
              case 3:
                  int m = 1;
                  if (length > min_length) {
                      length = length - 10;
                      lines(g);
                      for (Object gold : this.frame.objectList) {
                          if(gold.flag){
                              m = gold.m;
                              gold.x = endX - gold.getWidth()/2;
                              gold.y = endY;
                              if(length <= min_length){
                                  gold.x = -150;
                                  gold.y = -150;
                                  gold.flag = false;
                                  state = 0;
                                  //加分
                                  Bg.count+=gold.count;
                              }
                          }
                      }
                  }
                  ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在Bg类绘制

    //总分
    static int count = 0;
    
    //绘制
    public void paintSelf(Graphics g){
       g.drawImage(bg1,0,0,null);
       g.drawImage(bg,0,200,null);
       g.drawImage(peo,310,50,null);
       drawWord(g,30,Color.black,"积分:" + count,30,150);
    }
    
    //绘制字符串
    public static void drawWord(Graphics g,int size,Color color,String str,int x,int y){
       g.setColor(color);
       g.setFont(new Font("仿宋", Font.BOLD,size));
       g.drawString(str,x,y);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    18 力量与爆破

    在这里插入图片描述

    在Bg类添加参数

    //药水图片
    Image water = Toolkit.getDefaultToolkit().getImage("img/water.png");
    //药水数量
    static int waterNmu = 3;
    //药水状态 默认false 正在使用true
    static boolean waterFlag = false;
    
    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        g.drawImage(peo,310,50,null);
        drawWord(g,30,Color.black,"积分:" + count,30,150);
        //药水组件
        g.drawImage(water,450,40,null);
        drawWord(g,30,Color.black,"*" + waterNmu,510,70);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在Object添加参数

    //类型 1金块 2石块
    int type;
    
    • 1
    • 2

    在Gold类设值

    public Gold(){
            this.type = 1;
            ...
    
    • 1
    • 2
    • 3

    在Rock类设值

    public Rock(){
            this.type = 1;
            ...
    
    • 1
    • 2
    • 3

    在GameWin添加鼠标点击判断

    public void launch(){
       ...
    
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //左右摇摆,点击左键
                if(e.getButton() == 1 && line.state == 0){//鼠标左键点击
                    line.state = 1;
                }
                //抓取返回,点击右键
                if(e.getButton() == 3 && line.state == 3){//鼠标右键点击
                    if(Bg.waterNmu > 0){
                        Bg.waterFlag = true;
                        Bg.waterNmu--;
                    }
                }
            }
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在Line类添加修改判断

    public void paintSelf(Graphics g) {
        logic();
        switch (state) {
            ...
            case 3:
                int m = 1;
                if (length > min_length) {
                    length = length - 10;
                    lines(g);
                    for (Object gold : this.frame.objectList) {
                        if(gold.flag){
                            m = gold.m;
                            gold.x = endX - gold.getWidth()/2;
                            gold.y = endY;
                            if(length <= min_length){
                                gold.x = -150;
                                gold.y = -150;
                                gold.flag = false;
                                state = 0;
                                //加分
                                Bg.count+=gold.count;
                                Bg.waterFlag= false;
                            }
                            if(Bg.waterFlag){
                                if(gold.type == 1){
                                    m=1;
                                }
                                if(gold.type == 2){
                                    gold.x = -150;
                                    gold.y = -150;
                                    gold.flag = false;
                                    Bg.waterFlag= false;
                                    state = 2;
                                }
                            }
                        }
                    }
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    19 关卡设置

    在Bg类添加参数

    //关卡数
    static int level = 1;
    //目标得分
    int goal = level * 15;
    
    //绘制
    public void paintSelf(Graphics g){
        ...
    
        //关卡数
        drawWord(g,20,Color.black,"第" + level + "关",30,60);
        //目标积分
        drawWord(g,30,Color.black,"目标:" + goal,30,110);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在GameWin类添加判断

    public void launch(){
       ...
        while (true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //下一关
    public void nextLevel(){
        if (Bg.count >= bg.goal) {
            Bg.level++;
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    20 游戏状态

    在GameWin类添加状态

    //0 未开始 1 运行中 2 商店 3 失败 4 胜利
    static int state;
    
    public void launch(){
       ...
    
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        if(e.getButton() == 3){
                            state = 1;
                        }
                        break;
                    case 1:
                        //左右摇摆,点击左键
                        if(e.getButton() == 1 && line.state == 0){//鼠标左键点击
                            line.state = 1;
                        }
                        //抓取返回,点击右键
                        if(e.getButton() == 3 && line.state == 3){//鼠标右键点击
                            if(Bg.waterNmu > 0){
                                Bg.waterFlag = true;
                                Bg.waterNmu--;
                            }
                        }
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    default:
                }
    
            }
        });
    
       ...
    }
    
    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768,1000);
        Graphics graphics = offScreenImage.getGraphics();
    
        bg.paintSelf(graphics);
    
        if(state == 1){
            //绘制物体
            for (Object obj : objectList) {
                obj.paintSelf(graphics);
            }
            line.paintSelf(graphics);
        }
        g.drawImage(offScreenImage,0,0,null);
    }
    
    //下一关
    public void nextLevel(){
        if (state == 1) {
            if (Bg.count >= bg.goal) {
                Bg.level++;
                dispose();
                GameWin gameWin1 = new GameWin();
                gameWin1.launch();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    在Bg类添加判断绘制

    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        switch (GameWin.state){
            case 0:
                drawWord(g,80,Color.green,"准备开始",200,400);
                break;
            case 1:
                g.drawImage(peo,310,50,null);
                drawWord(g,30,Color.black,"积分:" + count,30,150);
                //药水组件
                g.drawImage(water,450,40,null);
                drawWord(g,30,Color.black,"*" + waterNmu,510,70);
    
                //关卡数
                drawWord(g,20,Color.black,"第" + level + "关",30,60);
                //目标积分
                drawWord(g,30,Color.black,"目标:" + goal,30,110);
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            default:
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    21 计时效果

    在Bg类添加参数

    //开始时间
    long startTime;
    //结束时间
    long endTime;
    
    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        switch (GameWin.state){
            case 0:
                drawWord(g,80,Color.green,"准备开始",200,400);
                break;
            case 1:
                g.drawImage(peo,310,50,null);
                drawWord(g,30,Color.black,"积分:" + count,30,150);
                //药水组件
                g.drawImage(water,450,40,null);
                drawWord(g,30,Color.black,"*" + waterNmu,510,70);
    
                //关卡数
                drawWord(g,20,Color.black,"第" + level + "关",30,60);
                //目标积分
                drawWord(g,30,Color.black,"目标:" + goal,30,110);
                //时间组件
                endTime = System.currentTimeMillis();
                long time = 20 -  (endTime - startTime)/1000;
                drawWord(g,30,Color.black,"时间:" + (time > 0 ? time : 0) ,520,150);
                break;
            case 2:
           ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在GameWin类初始值

    public void launch(){
       ...
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        if(e.getButton() == 3){
                            state = 1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    22 失败状态

    在Bg添加方法

    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        switch (GameWin.state){
           ...
            case 3:
                drawWord(g,80,Color.cyan,"失败",250,350);
                drawWord(g,80,Color.cyan,"目标:" + count,200,450);
                break;
            case 4:
                break;
            default:
        }
    
    }
    
    
    //true倒计时完成 false正在倒计时
    public boolean gameTime(){
        long time =  (endTime - startTime)/1000;
        if(time > 20){
            return true;
        }
        return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在GameWin添加判断

    //下一关
    public void nextLevel(){
       if (state == 1 && bg.gameTime()) {
           if (Bg.count >= bg.goal) {
               Bg.level++;
               dispose();
               GameWin gameWin1 = new GameWin();
               gameWin1.launch();
           }else {
               state = 3;
           }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    23 成功状态

    在GameWin类添加

    //下一关
    public void nextLevel() {
        if (state == 1 && bg.gameTime()) {
            if (Bg.count >= bg.goal) {
                if (Bg.level == 5) {
                    state = 4;
                } else {
                    Bg.level++;
                }
                dispose();
                GameWin gameWin1 = new GameWin();
                gameWin1.launch();
            } else {
                state = 3;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在Bg类绘制

    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        switch (GameWin.state){
            ..
            case 4:
                drawWord(g,80,Color.red,"成功",250,350);
                drawWord(g,80,Color.red,"目标:" + count,200,450);
                break;
            default:
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    24 游戏重置

    在Bg类添加方法

    //重置元素
    void reGame(){
        level = 1;
        goal = level *15;
        count = 0;
        waterNmu = 3;
        waterFlag = false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在Line类添加方法

    //重置线元素
    void reGame() {
        n =0;
        length = 100;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在GameWin类添加判断

    public void launch() {
       ...
    
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state) {
                    case 0:
                        if (e.getButton() == 3) {
                            state = 1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        //左右摇摆,点击左键
                        if (e.getButton() == 1 && line.state == 0) {//鼠标左键点击
                            line.state = 1;
                        }
                        //抓取返回,点击右键
                        if (e.getButton() == 3 && line.state == 3) {//鼠标右键点击
                            if (Bg.waterNmu > 0) {
                                Bg.waterFlag = true;
                                Bg.waterNmu--;
                            }
                        }
                        break;
                    case 2:
                        break;
                    case 3:
                    case 4:
                        if(e.getButton() == 1){
                            state = 0;
                            bg.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }
    
            }
        });
    
        ...
    }
    
    
    //下一关
    public void nextLevel() {
        if (state == 1 && bg.gameTime()) {
            if (Bg.count >= bg.goal) {
                if (Bg.level == 5) {
                    state = 4;
                } else {
                    Bg.level++;
                    bg.startTime = System.currentTimeMillis();
                }
    
            } else {
                state = 3;
            }
    
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    25 商店购物

    在Bg类添加绘制

    //药水价格
    int price = (int)(Math.random()*10);
    //是否进入商店 false 不购买
    boolean shop = false;
    
    //绘制
    public void paintSelf(Graphics g){
        g.drawImage(bg1,0,0,null);
        g.drawImage(bg,0,200,null);
        switch (GameWin.state){
            。。。
            case 2:
                g.drawImage(water,300,400,null);
                drawWord(g,30,Color.black,"价格:" + price,300,500);
                drawWord(g,30,Color.black,"是否购买?",300,550);
                if(shop){
                    count = count - price;
                    waterNmu++;
                    shop = false;
                    GameWin.state = 1;
                    startTime = System.currentTimeMillis();
                }
                break;
            case 3:
               ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在GameWin类修改

    public void launch() {
      ...
    
        //鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state) {
                    ..
                    case 2:
                        if(e.getButton() == 1){
                            bg.shop = true;
                        }
                        if(e.getButton() == 3){
                            state =1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        if(e.getButton() == 1){
                            state = 0;
                            bg.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }
    
            }
        });
    
       ...
    }
    
    
    
    //下一关
    public void nextLevel() {
        if (state == 1 && bg.gameTime()) {
            if (Bg.count >= bg.goal) {
                if (Bg.level == 5) {
                    state = 4;
                } else {
                    state =2;
                    Bg.level++;
                }
            } else {
                state = 3;
            }
    
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
  • 相关阅读:
    01年前端程序的副业之路
    一个人就是一本书
    软件过程与管理_期末复习知识点回顾总结
    create® 3入门教程-Create3 ROS 2 API
    C#-使用Harmony库实现DLL文件反射调用
    网络安全(一):信息收集之玩转nmap(理论篇)
    自注意力机制(Self-attention)【第四章】
    【Selenium】提高测试&爬虫效率:Selenium与多线程的完美结合
    C#面试题: 寻找中间值
    处理streamlit库上传的图片文件
  • 原文地址:https://blog.csdn.net/weixin_42469070/article/details/127971528