首页 > 其他分享 >数据结构小学期第六天

数据结构小学期第六天

时间:2024-07-06 16:19:31浏览次数:8  
标签:界面 int 小学 ++ 第六天 new JLabel 数据结构 data

今天完全实现了九宫格拼图游戏,具备一键通关功能按下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 }

效果图

 

标签:界面,int,小学,++,第六天,new,JLabel,数据结构,data
From: https://www.cnblogs.com/Lyh3012648079/p/18287392

相关文章

  • 【数据结构】栈和队列
    文章目录1.栈1.1栈的概念及结构1.2栈的实现2.队列2.1队列的概念及结构2.2队列的实现3.栈和队列面试题4.概念选择题1.栈1.1栈的概念及结构栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈......
  • 一种尽可能减小内存占用的数据结构设计方法
         背景:以三维点为例,随着采集设备的日新月异,三维点的属性信息也越来越多(例如颜色、强度、回波信息、gps时间等);导致点云数据在处理时加载到计算机中所需要的内存空间也越来越大,但是有些数据往往只有x、y、z三个坐标值,则不需要为其开辟多余的内存空间,那一套统一的数据结......
  • pwn的linux基础(计算机内部数据结构存储形式)
    linux基础保护层级:分为四个ring0-ring3一般来说就两个,0和30为内核3为用户 权限:用户分为多个组文件和目录等等的权限一般都是三个,即可读可写可执行。读:R,写:W,执行:X赋予一个可执行文件执行权限就是chmod+xfilename虚拟内存和物理内存:物理内存很直白,就是内存......
  • python数据结构(树和二叉树)
    树非线性结构一对多根结点(无前驱)多个叶子结点(无后继)其他数据元素(一个前驱,多个后驱)树与二叉树转换树与二叉树均可用二叉链表作为存储结构,则以二叉链表为媒介可导出树之间的一个对应关系-----即给定一颗树,可以找到唯一一颗二叉树与之对应。把树转化为二叉树步骤一:加线......
  • Redis数据结构-字典的实现
    字典,又称符号表(symboltable)、关联数组(associativearray)或者映射(map),是一种用于保存键值对(key-valuepair)的抽象数据结构。在字典中,一个键(key)可以和一个值(value)进行关联(或者说将键映射为值),这些关联的键和值就被称为键值对。字典中的每个键都是独一无二的,程序可以在字典......
  • 从零开始学数据结构系列之第四章《 广度优先遍历BFS》
    文章目录广度优先遍历(BFS)概念广度优先遍历算法步骤总代码往期回顾广度优先遍历(BFS)概念​  广度优先遍历(BreadthFirstSearch),又称为广度优先搜索,简称BFS。​  如果说图的深度优先遍历类似树的前序遍历,那么图的广度优先遍历就类似于树的层序遍历了。​ ......
  • ElasticSearch的数据结构是什么
    Elasticsearch的数据结构是基于文档的存储和检索模型。它使用一种灵活的、面向文档的方式来存储和管理数据,每个文档都可以包含多种类型的数据。下面详细介绍Elasticsearch的数据结构及其核心概念:核心概念索引(Index):Elasticsearch中的索引相当于关系型数据库中的数据库。......
  • 数据结构实验报告:查找
     一、实验目的1.掌握查找表的结构。2.掌握顺序查找、折半查找、二叉排序树查找和哈希查找。二、实验环境Windows10、VisualC++6.0三、实验任务1.编写程序实现顺序查找和折半查找。(1)顺序查找#include<stdio.h>#include<stdlib.h>#defineLIST_SIZE20typ......
  • Java中的JSON神器,如何轻松玩转复杂数据结构
    哈喽,大家好,我是木头左!一、揭秘JSON世界的基石在Java的世界中,JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,它基于文本,易于阅读和编写,同时也易于机器解析和生成。JSON在日常开发中的应用非常广泛,无论是前后端的数据交互,还是配置文件的读取,都离不开JSON的身影。那......
  • 【数据结构】(C语言):二叉搜索树(不使用递归)
    二叉搜索树:非线性的,树是层级结构。基本单位是节点,每个节点最多2个子节点。有序。每个节点,其左子节点都比它小,其右子节点都比它大。每个子树都是一个二叉搜索树。每个节点及其所有子节点形成子树。可以是空树。C语言实现:(使用链表实现,不使用递归) 创建结构体数据类型(记录二叉......