基于JAVA实现的推箱子小游戏

2024-04-26 18:28
文章标签 java 实现 箱子 小游戏

本文主要是介绍基于JAVA实现的推箱子小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


Java推箱子小游戏实现:

     推箱子小游戏曾经在我们的童年给我们带来了很多乐趣。推箱子这款游戏现在基本上没人玩了,甚至在新一代人的印象中都已毫无记忆了。。。但是,这款游戏可以在一定程度上锻炼自己的编程能力。

窗口画面设计:
import javax.swing.*;
import java.awt.event.*;

public class Pushbox extends JFrame implements ActionListener {
    private static final long serialVersionUID = -392323205976303799L;
    Game game;
    JLabel jl = new JLabel("推箱子游戏");
    JTextArea tips = new JTextArea("            游戏规则\n玩家通过上下左右按键\n移动小人推箱子到指定\n"
            + "的箱子归位处,当所有\n箱子都被推至归位处后\n则玩家成功通过该关卡");
    JButton jb1 = new JButton("选择关卡");
    JButton jb2 = new JButton("重新开始");
    JButton jb3 = new JButton("退出游戏");
    JLabel jlt = new JLabel("在下方输入关卡1-3");
    JTextField jt = new JTextField();

    Pushbox() {
        setTitle("推箱子");//设置窗口显示的名字
        setBounds(500, 200, 600, 470);//设置窗口相对于屏幕的显示位置(500200),窗口大小(600,470)
        setVisible(true);// 使窗口显示
        setLayout(null);// 清空布局管理器,之后的控件可自定义坐标位置
        jl.setBounds(265, 10, 100, 20);
        tips.setBounds(430, 40, 120, 120);
        jlt.setBounds(430, 180, 120, 20);
        jt.setBounds(430, 210, 120, 30);
        jb1.setBounds(430, 250, 120, 40);
        jb2.setBounds(430, 300, 120, 40);
        jb3.setBounds(430, 350, 120, 40);
        add(jl);
        add(tips);
        add(jlt);
        add(jt);
        add(jb1);
        add(jb2);
        add(jb3);
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
        game = new Game();
        add(game);
        game.Load(game.flag);
        game.requestFocus();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jb1) {
            String f = jt.getText();
            game.flag = Integer.parseInt(f) - 1;
            if (game.flag > 2 || game.flag < 0) {
                JOptionPane.showMessageDialog(this, "没有这一关!!!");//弹窗
                jt.setText("");
            } else {
                game.Load(game.flag);
                game.requestFocus();
                jt.setText("");
            }
        }
        if (e.getSource() == jb2) {
            game.Load(game.flag);
            game.requestFocus();
        }
        //如果点击了jb3按钮(退出游戏)
        if (e.getSource() == jb3) {
            System.exit(0);    //退出
        }
    }
}

地图读取及初始点设置:

public class Read {
    public int f, x, y;
    public int[][] map = new int[12][12];

    Read(int n) {
        f = n;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                map[i][j] = Pass.pass[f][i][j];
                if (map[i][j] == 5) {//找到人的位置
                    x = j;//把下标存起来
                    y = i;//把下标存起来
                }
            }
        }
    }

    int[][] getmap() {
        return map;
    }

    int getmx() {
        return x;
    }

    int getmy() {
        return y;
    }
}

游戏地图显示和控件操作:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends JPanel implements KeyListener {
    int len = 30;//地图格子的长度
    Image[] img;//定义图像数组
    public int flag = 0;//定义关数,因为数组下表第一个是0,所以0代表第一关
    int mx, my;//定义任务初始位置下标
    int[][] newmap;//定义目前的地图数组(这个就是实时显示的,会根据移动改变)
    int[][] oldmap;//定义加载的地图数组(这个就是一开始的地图数据,不会改变)
    Read fnewmap;
    Read foldmap;

    Game() {
        setBounds(40, 40, 360, 360);
        setBackground(Color.white);
        addKeyListener(this);
        img = new Image[7];
        for (int i = 0; i < 7; i++) {
            //图片资源存放的相对路径
            img[i] = Toolkit.getDefaultToolkit().getImage(".\\pushing_boxes\\src\\main\\java\\com\\txz\\img\\" + i + ".png");
        }
        setVisible(true);
        Load(flag);
    }

    void Load(int i)//加载地图函数
    {
        fnewmap = new Read(i);//调用Read
        foldmap = new Read(i);//调用Read
        newmap = fnewmap.getmap();//获取地图数据给newmap数组
        mx = fnewmap.getmx();//获取初始人物的X位置
        my = fnewmap.getmy();//获取初始人物的Y位置
        oldmap = foldmap.getmap();//获取地图数据给oldmap数组
        repaint();//刷新界面并绘制地图
    }

    @Override
    public void paint(Graphics g)//绘制地图函数,(自带的库函数),调用repaint()时就会调用这个函数,重写画界面
    {
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                g.drawImage(img[newmap[j][i]], i * len, j * len, this);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e)//键盘响应函数
    {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            up();
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            down();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left();
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right();
        }
        if (win()) {
            if (flag == 2) {
                JOptionPane.showMessageDialog(this, "恭喜您通过最后一关!!!");
            } else {
                String a = "恭喜您通过第" + (flag + 1) + "!!!\n是否要进入下一关?";
                String title = "恭喜过关";
                int text = JOptionPane.YES_NO_OPTION;
                int choice = 0;
                choice = JOptionPane.showConfirmDialog(null, a, title, text);
                if (choice == 0) {
                    flag++;
                    Load(flag);
                } else if (choice == 0) {
                    System.exit(0);
                }
            }
        }
    }

    void up()//上移函数
    {
        if (newmap[my - 1][mx] == 2 || newmap[my - 1][mx] == 4) {
            if (oldmap[my][mx] == 4) {
                newmap[my][mx] = 4;
            } else {
                newmap[my][mx] = 2;
            }
            newmap[my - 1][mx] = 5;
            repaint();
            my--;
        } else if (newmap[my - 1][mx] == 3) {
            if (newmap[my - 2][mx] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my - 1][mx] = 5;
                newmap[my - 2][mx] = 6;
                repaint();
                my--;
            } else if (newmap[my - 2][mx] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my - 1][mx] = 5;
                newmap[my - 2][mx] = 3;
                repaint();
                my--;
            }
        } else if (newmap[my - 1][mx] == 6) {
            if (newmap[my - 2][mx] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my - 1][mx] = 5;
                newmap[my - 2][mx] = 6;
                repaint();
                my--;
            } else if (newmap[my - 2][mx] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my - 1][mx] = 5;
                newmap[my - 2][mx] = 3;
                repaint();
                my--;
            }
        }
    }

    void down()//下移函数
    {
        if (newmap[my + 1][mx] == 2 || newmap[my + 1][mx] == 4) {
            if (oldmap[my][mx] == 4) {
                newmap[my][mx] = 4;
            } else {
                newmap[my][mx] = 2;
            }
            newmap[my + 1][mx] = 5;
            repaint();
            my++;
        } else if (newmap[my + 1][mx] == 3) {
            if (newmap[my + 2][mx] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my + 1][mx] = 5;
                newmap[my + 2][mx] = 6;
                repaint();
                my++;
            } else if (newmap[my + 2][mx] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my + 1][mx] = 5;
                newmap[my + 2][mx] = 3;
                repaint();
                my++;
            }
        } else if (newmap[my + 1][mx] == 6) {
            if (newmap[my + 2][mx] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my + 1][mx] = 5;
                newmap[my + 2][mx] = 6;
                repaint();
                my++;
            } else if (newmap[my + 2][mx] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my + 1][mx] = 5;
                newmap[my + 2][mx] = 3;
                repaint();
                my++;
            }
        }
    }

    void left()//左移函数
    {
        if (newmap[my][mx - 1] == 2 || newmap[my][mx - 1] == 4) {
            if (oldmap[my][mx] == 4) {
                newmap[my][mx] = 4;
            } else {
                newmap[my][mx] = 2;
            }
            newmap[my][mx - 1] = 5;
            repaint();
            mx--;
        } else if (newmap[my][mx - 1] == 3) {
            if (newmap[my][mx - 2] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx - 1] = 5;
                newmap[my][mx - 2] = 6;
                repaint();
                mx--;
            } else if (newmap[my][mx - 2] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx - 1] = 5;
                newmap[my][mx - 2] = 3;
                repaint();
                mx--;
            }
        } else if (newmap[my][mx - 1] == 6) {
            if (newmap[my][mx - 2] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx - 1] = 5;
                newmap[my][mx - 2] = 6;
                repaint();
                mx--;
            } else if (newmap[my][mx - 2] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx - 1] = 5;
                newmap[my][mx - 2] = 3;
                repaint();
                mx--;
            }
        }
    }

    void right()//右移函数
    {
        if (newmap[my][mx + 1] == 2 || newmap[my][mx + 1] == 4) {
            if (oldmap[my][mx] == 4) {
                newmap[my][mx] = 4;
            } else {
                newmap[my][mx] = 2;
            }
            newmap[my][mx + 1] = 5;
            repaint();
            mx++;
        } else if (newmap[my][mx + 1] == 3) {
            if (newmap[my][mx + 2] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx + 1] = 5;
                newmap[my][mx + 2] = 6;
                repaint();
                mx++;
            } else if (newmap[my][mx + 2] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx + 1] = 5;
                newmap[my][mx + 2] = 3;
                repaint();
                mx++;
            }
        } else if (newmap[my][mx + 1] == 6) {
            if (newmap[my][mx + 2] == 4) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx + 1] = 5;
                newmap[my][mx + 2] = 6;
                repaint();
                mx++;
            } else if (newmap[my][mx + 2] == 2) {
                if (oldmap[my][mx] == 4) {
                    newmap[my][mx] = 4;
                } else {
                    newmap[my][mx] = 2;
                }
                newmap[my][mx + 1] = 5;
                newmap[my][mx + 2] = 3;
                repaint();
                mx++;
            }
        }
    }

    boolean win()//判断输赢函数
    {
        boolean num = false;
        out:
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                if (oldmap[i][j] == 4) {
                    if (newmap[i][j] == 6) {
                        num = true;
                    } else {
                        num = false;
                        break out;
                    }
                }
            }
        }
        return num;
    }
}

运行效果如图:

完整素材及全部代码

代码已上传csdn,0积分下载,觉得这片博文有用请留下你的点赞

基于JAVA的推箱子小游戏,一起来实现自己的趣味小游戏

这篇关于基于JAVA实现的推箱子小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/938329

相关文章

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例:

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

NFS实现多服务器文件的共享的方法步骤

《NFS实现多服务器文件的共享的方法步骤》NFS允许网络中的计算机之间共享资源,客户端可以透明地读写远端NFS服务器上的文件,本文就来介绍一下NFS实现多服务器文件的共享的方法步骤,感兴趣的可以了解一... 目录一、简介二、部署1、准备1、服务端和客户端:安装nfs-utils2、服务端:创建共享目录3、服