数据结构小学期第六天

news/2024/10/5 13:59:54/文章来源:https://www.cnblogs.com/Lyh3012648079/p/18287392

今天完全实现了九宫格拼图游戏,具备一键通关功能按下W键,查看原图功能按住A键不松,移动图片按上下左右键,如果你自己想要实现这个功能,需要自己的图片,图片格式要求。

每个小图片是105*105,完整图片是315*315.有人想要做一下,可以试一试。代码如下

启动类

 1 import com.itheima.ui.GameJFrame;
 2 
 3 
 4 
 5 public class App {
 6     public static void main(String[] args) {
 7         //表示程序的启动入口
 8         //new LoginJFrame();
 9         new GameJFrame();
10         //new RegisterJFrame();
11     }
12 }

实现游戏类

  1 package com.itheima.ui;
  2 
  3 import javax.swing.*;
  4 import javax.swing.border.BevelBorder;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.awt.event.KeyEvent;
  8 import java.awt.event.KeyListener;
  9 import java.util.Random;
 10 
 11 public class GameJFrame extends JFrame implements KeyListener, ActionListener {
 12 
 13     //创建一个二位数组
 14     //用来管理数据
 15     int[][] data = new int[3][3];
 16 
 17     //记录空白方块在二维数组中的位置
 18     int x=0;
 19     int y=0;
 20 
 21     //定义一个正确的数组,用来存放正确的数据
 22     int[][] win = {
 23             {1,2,3},
 24             {4,5,6},
 25             {7,8,0},
 26     };
 27 
 28     //定义一个变量来统计步数
 29     int step = 0;
 30 
 31     //记录图片路径
 32     String path ="D:\\ideaworkplace\\puzzlegame2\\image\\";
 33 
 34     //创建选项中的条目
 35     JMenuItem replayItem = new JMenuItem("重新游戏");
 36     JMenuItem closeItem = new JMenuItem("关闭游戏");
 37 
 38     public GameJFrame(){
 39         //初始化界面
 40         initJFrame();
 41 
 42         //初始化菜单
 43         initJMenuBar();
 44 
 45         //初始化数据
 46         initData();
 47 
 48         //初始化图片
 49         initImage();
 50 
 51         //使界面显示出来
 52         this.setVisible(true);
 53     }
 54 
 55     //初始化数据
 56     private void initData() {
 57         int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8};
 58 
 59         Random r = new Random();
 60         for (int i = 0; i < tempArr.length; i++) {
 61             int index = r.nextInt(tempArr.length);
 62             int temp = tempArr[i];
 63             tempArr[i] = tempArr[index];
 64             tempArr[index] = temp;
 65         }
 66 
 67 
 68         for (int i = 0; i < tempArr.length; i++) {
 69 
 70             if(tempArr[i] ==0 ){
 71                 x=i/3;
 72                 y=i%3;
 73             }
 74             data[i / 3][i % 3] = tempArr[i];
 75 
 76         }
 77 
 78     }
 79 
 80     private void initImage() {
 81 
 82         //清空原先的缓存
 83         this.getContentPane().removeAll();
 84 
 85         //加载图片是判断是否胜利
 86         if (victory()) {
 87             JLabel winJLabel = new JLabel(new ImageIcon("D:\\ideaworkplace\\puzzlegame2\\image\\win.png"));
 88             winJLabel.setBounds(203,283,197,73);
 89             this.getContentPane().add(winJLabel);
 90         }
 91 
 92         //把步数添加到界面当中
 93         JLabel stepJLabel = new JLabel("步数:" + step);
 94         stepJLabel.setBounds(50,30,100,20);
 95         this.getContentPane().add(stepJLabel);
 96 
 97 
 98         for (int i = 0; i < 3; i++) {
 99             for (int j = 0; j < 3; j++) {
100                 int num = data[i][j];
101                 //创建一个ImageIcon对象
102                 //创建一个JLabel对象(管理容器)
103                 JLabel jLabel = new JLabel(new ImageIcon(path+num+".jpg"));
104                 //移动图片的坐标
105                 jLabel.setBounds(105*j+83,105*i+134,105,105);
106                 //给图片添加边框
107                 jLabel.setBorder(new BevelBorder(1));
108                 //把管理容器添加到界面当中
109                 //this.add(jLabel);
110                 this.getContentPane().add(jLabel);
111             }
112         }
113 
114 ////        //添加背景图片
115 //        JLabel background = new JLabel(new ImageIcon("D:\\ideaworkplace\\puzzlegame2\\image\\background.jpg"));
116 //        background.setBounds(40,40,508,560);
117 //        //把背景图片加到界面当中
118 //        this.getContentPane().add(background);
119 
120 
121         //刷新一下界面
122         this.getContentPane().repaint();
123     }
124 
125     private void initJMenuBar() {
126         //创建整个菜单对象
127         JMenuBar jMenuBar = new JMenuBar();
128 
129         //创建菜单上两个选项的对象
130         JMenu functionMenu = new JMenu("功能");
131 
132 
133         //把条目放到选项当中
134         functionMenu.add(replayItem);
135 
136         functionMenu.add(closeItem);
137 
138 
139         //把选项放到菜单中
140         jMenuBar.add(functionMenu);
141 
142         //给条目绑定监听事件
143         replayItem.addActionListener(this);
144 
145         closeItem.addActionListener(this);
146 
147 
148         //把菜单放到界面中
149         this.setJMenuBar(jMenuBar);
150     }
151 
152     private void initJFrame() {
153         //设置界面长宽
154         this.setSize(498,575);
155         //设置界面的标题
156         this.setTitle("拼图游戏 v-1.0");
157         //设置游戏总是置顶
158         this.setAlwaysOnTop(true);
159         //设置游戏界面居中
160         this.setLocationRelativeTo(null);
161         //设置关闭模式
162         this.setDefaultCloseOperation(3);
163         //取消默认放置
164         this.setLayout(null);
165         //添加键盘监听
166         this.addKeyListener(this);
167     }
168 
169     @Override
170     public void keyTyped(KeyEvent e) {
171 
172     }
173 
174     @Override
175     public void keyPressed(KeyEvent e) {
176         //按下A键显示完整的图片
177         int code = e.getKeyCode();
178         if(code == 65){
179             //先删除整个界面的图片
180             this.getContentPane().removeAll();
181             //加载完整的图片
182             JLabel all = new JLabel(new ImageIcon(path+"all.jpg"));
183             all.setBounds(83,134,315,315);
184             this.getContentPane().add(all);
185 
186 //            //加载背景图片
187 //            JLabel background = new JLabel(new ImageIcon("D:\\ideaworkplace\\puzzlegame2\\image\\background.jpg"));
188 //            background.setBounds(40,40,508,560);
189 //            //把背景图片加到界面当中
190 //            this.getContentPane().add(background);
191 
192             //刷新一下界面
193             this.getContentPane().repaint();
194         }
195 
196     }
197 
198     @Override
199     public void keyReleased(KeyEvent e) {
200         //如果胜利就不能再进行游戏
201         if(victory()){
202             return;
203         }
204 
205         //对上下左右进行判断
206         //左上右下:分别为37,38,39,40
207         int code = e.getKeyCode();
208         if(code == 37){
209             System.out.println("向左移动");
210             if(y==2){
211                 return;
212             }
213             //空白方块:x,y
214             //右边方块:x,y+1
215             data[x][y] = data[x][y+1];
216             data[x][y+1] = 0;
217             y++;
218             //步数加1
219             step++;
220             //更新界面
221             initImage();
222         }
223         if(code == 38){
224 
225             //逻辑:把空白方块和下面的方块交换位置
226             //空白方块:x,y
227             //下面方块:x+1,y
228             System.out.println("向上移动");
229             if(x==2){
230                 return;
231             }
232             data[x][y] = data[x+1][y];
233             data[x+1][y] = 0;
234             x++;
235             //步数加1
236             step++;
237             //更新界面
238             initImage();
239         }
240         if(code == 39){
241 
242             System.out.println("向右移动");
243             if(y==0){
244                 return;
245             }
246             //空白方块:x,y
247             //下面方块:x,y-1
248             data[x][y] = data[x][y-1];
249             data[x][y-1] = 0;
250             y--;
251             //步数加1
252             step++;
253             //更新界面
254             initImage();
255         }
256         if(code == 40){
257             System.out.println("向下移动");
258             if(x==0){
259                 return;
260             }
261             //空白方块:x,y
262             //下面方块:x-1,y
263             data[x][y] = data[x-1][y];
264             data[x-1][y] = 0;
265             x--;
266             //步数加1
267             step++;
268             //更新界面
269             initImage();
270         }
271         //显示正确的图片
272         if(code == 65){
273             initImage();
274         }
275         //一键通关
276         if(code == 87){
277             data = new int[][]{
278                     {1,2,3},
279                     {4,5,6},
280                     {7,8,0},
281             };
282             initImage();
283         }
284     }
285 
286     //判断游戏是否胜利
287     public boolean victory(){
288         for (int i = 0; i < data.length; i++) {
289             for (int j = 0; j < data[i].length; j++) {
290                 //如果有一个不等于正确的,就退出
291                 if(data[i][j] != win[i][j]){
292                     return false;
293                 }
294             }
295         }
296         return true;
297     }
298 
299     @Override
300     public void actionPerformed(ActionEvent e) {
301         Object obj = e.getSource();
302         if(obj == replayItem){
303             System.out.println("1");
304             //重新加载步数
305             step = 0;
306             //重新打乱数组
307             initData();
308             //重新加载打乱的页面
309             initImage();
310 
311         }  else if (obj == closeItem) {
312             System.out.println("3");
313             //关闭游戏
314             System.exit(0);
315         }
316     }
317 }

效果图

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/739387.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1、flask-基本架构-MVT - 虚拟环境的安装 - 创建flask应用

flask基本架构图 创建虚拟环境 #1. 打开cmd或pycharm都可以(确保安装python环境) #2. 安装虚拟环境模块-windows - pip install virtualenv virtualenvwrapper-win#3. 查看虚拟环境 - workon#4. 创建虚拟环境 - mkvirtualenv flask2env - 默认创建在:C:\Users\Administrato…

基于Qwen2/Lllama3等大模型,部署团队私有化RAG知识库系统的详细教程(Docker+AnythingLLM)

大语言模型在垂直细分领域存在知识局限、幻觉、数据安全等一些问题,可通过RAG(检索增强生成)方案来解决。本文基于AnythingLLM框架,搭建团队私有知识库系统,并进行使用和验证,RAG系统在保留输出的有效性同时,还保留了创造性……自 ChatGPT 发布以来,大型语言模型(Larg…

基于STM32F1系列,驱动L298N电机驱动板实现直流电机的启动、停止、调速功能

一. L298N电机驱动板电源引脚 VCC 外接直流电源引脚,电压范围在5~35V之间 GND GND是接地引脚,连接到电源负极 5V 驱动芯片内部逻辑供电引脚,如果安装了5V跳帽,则此引脚可输出5V电压,为微控板或其他电路提供电力供给,如果拔掉5V跳帽,则需要独立外接5V电源 控制引脚 IN1 &…

SpringBoot引入WebSocket

WebSocket 是一种在客户端和服务器之间提供低延迟、全双工通信的网络协议。它允许双方在建立一次连接后,进行实时、持续的数据交换,无需像HTTP那样为每一个请求和响应建立新的连接。WebSocket的设计初衷是解决传统HTTP协议在实时通信方面的不足,比如实现实时聊天、游戏、股票…

博客园商业之路:全园求偶遇,懂园子懂商业的创业合伙人

各方面的因素将园子的商业化强推到一个关口,2024年7月-9月是决定园子命运的一个季度,我们将拼尽所有力气找各种可能的突破口,不会有任何保留。 这个关口是最后关头,也是三年多来最好的时间窗口,天时地利最需要人和,找到对的人,最有可能在这个时间窗口,一将解园子二十年…

KBPC2504-ASEMI无人机专用整流桥KBPC2504

KBPC2504-ASEMI无人机专用整流桥KBPC2504编辑:ll KBPC2504-ASEMI无人机专用整流桥KBPC2504 型号:KBPC2504 品牌:ASEMI 封装:KBPC-4 最大重复峰值反向电压:400V 最大正向平均整流电流(Vdss):25A 功率(Pd):中小功率 芯片个数:4 引脚数量:4 类型:整流方桥、整流桥 正向浪…

3款C#开源且实用的工具类库,工作效率提升利器!

前言 在日常工作开发中工具类库是软件开发中不可或缺的一部分,它们通过提供代码重用、通用功能、隐藏复杂性、提高代码质量、扩展性等方面的优势,帮助开发者更高效、更稳定地构建软件应用程序。今天大姚给大家分享3款C#开源且实用的工具类库,希望能帮助到有需要的小伙伴。 M…

关于 Puerts 的性能问题

Puerts 在 UE 开发中提供了一定的便利性,可以用代码的方式写蓝图,但是官方是不推荐这么做的 原话如下那么这个性能问题究竟有多大呢 这里先用 C++ 写一个测试代码#include "TestWidget.h"#include "Blueprint/WidgetBlueprintLibrary.h" #include "…

均方误差、二元交叉熵推导

详情可以看:https://zhuanlan.zhihu.com/p/626054495

MinGW GCC Windows下编译libmodbus

最近项目从MSVC切换到GCC,libmodbus官网没提供MinGW下GCC 如何编译,官网推荐在类UNIX环境下MSYS下编译,个人更偏向直接拿源文件编译。 编译libmodbus版本: libmodbus-3.1.10 GCC编译器版本: 5.3.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project) CMake版本: 3.29.0 1…

提取时序数据的趋势、季节性以及残差

一天的光滑数据sub = [199.68, 187.16, 173.97, 159.85, 146.92, 135.29, 125.04, 114.86, 105.85, 97.93, 90.6, 84.19, 78.37, 72.85, 68.93, 66.59, 62.19, 58.59, 54.15, 50.26, 47.16, 44.14, 41.62, 38.99, 36.84, 34.9, 33.32, 32.75, 33.1, 32.49, 31.49, 30.13, 28.96…

SpringBoot如何集成和使用开源工作流引擎camunda

使用camunda开源工作流引擎有:通过docker运行、使用springboot集成、部署camunda发行包、基于源代码编译运行等多种方式。其中,通过源代码编译运行的方式最为复杂,文本重点介绍如何在Spring Boot应用程序中如何集成Camunda Platform开源流程平台,这也是项目中最为常见的一种…