基于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

相关文章

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis