拼图小游戏-Java GUI

创建主界面

image-20231112105222250

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package Itluo;

import javax.swing.*;

public class MWin {
public static void main(String[] args) {
//创建游戏主界面
JFrame game0=new JFrame();
game0.setSize(603,680);
game0.setVisible(true);

//创建登录界面
JFrame game1=new JFrame();
game1.setSize(488,430);
game1.setVisible(true);

//创建注册界面
JFrame game2=new JFrame();
game2.setSize(488,500);
game2.setVisible(true);
}
}

小练习

屏幕截图 2023-11-13 002124

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
private void changearr() {
int []arr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//设置循环对数组进行打乱
for (int i = 0; i < arr.length; i++) {
//生成0-15的序列号
Random rd=new Random();
int x= rd.nextInt(16);
//将当前数和x对应的序列数进行交换,创建一个中间载体
int x1=arr[i];
arr[i]=arr[x];
arr[x]=x1;
}
//将所得一维数组改成二维数组

//进行循环录入数据
int y=0;
for (int i = 0; i < 4; i++) {
for (int i1 = 0; i1 < 4; i1++) {
arr1[i1][i]=arr[y];
//登记0图坐标
if(arr[y]==0){
x1=i1;
y1=i;
}
y++;
}
}
}

拼图游戏代码

1
2
3
4
5
6
7
8
9
10
11
import Itluo.Login;
import Itluo.game;
import Itluo.regist;

public class APP {
public static void main(String[] args) {

new game();
}
}

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package Itluo;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;

public class game extends JFrame implements ActionListener {
//定义一个二维数组容器
int [][]arr1=new int[4][4];
//定义0号拼图坐标
int x1;
int y1;
int[][]win1={
{1, 5, 9, 13},
{2, 6, 10, 14},
{2, 7, 11, 15},
{4, 8, 12, 0}
};
//定义步数计数器
int step=0;
JMenuItem replay=new JMenuItem("重新游戏");
JMenuItem reLogin=new JMenuItem("重新登录");
JMenuItem Exsist=new JMenuItem("关闭游戏");
JMenuItem abouT=new JMenuItem("公众号");
public game(){
//边框设置
Thelooking();
//设置菜单
Menue0();
//初始化数据
changearr();

//初始化图片
runimagin();

//将界面展示
this.setVisible(true);


}

private void changearr() {
int []arr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//设置循环对数组进行打乱
for (int i = 0; i < arr.length; i++) {
//生成0-15的序列号
Random rd=new Random();
int x= rd.nextInt(16);
//将当前数和x对应的序列数进行交换,创建一个中间载体
int x1=arr[i];
arr[i]=arr[x];
arr[x]=x1;
}
//将所得一维数组改成二维数组

//进行循环录入数据
int y=0;
for (int i = 0; i < 4; i++) {
for (int i1 = 0; i1 < 4; i1++) {
arr1[i1][i]=arr[y];
//登记0图坐标
if(arr[y]==0){
x1=i1;
y1=i;
}
y++;
}
}
}

private void runimagin() {
//清楚原本的图层,因为先加载的图层在上边
this.getContentPane().removeAll();
if(win()){
arr1[3][3]=16;
}
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
//导入一张图片
//将图片放进管理器
int num=arr1[i][j];
JLabel run1=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\"+num+".jpg"));
//指定图片位置
run1.setBounds(105*i+83,j*105+134,105,105);
run1.setBorder(new BevelBorder(0));
this.getContentPane().add(run1);
//刷新一下
this.getContentPane().repaint();
}
}
JLabel s=new JLabel("步数:"+step);
s.setBounds(10,0,100,50);
this.getContentPane().add(s);
}

//定义展示最终结果
private void printfall(){
//清楚原本的图层,因为先加载的图层在上边
this.getContentPane().removeAll();
//导入一张图片
//将图片放进管理器
JLabel run1=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\all.jpg"));
//指定图片位置
run1.setBounds(83,134,420,420);
this.getContentPane().add(run1);
//刷新一下
this.getContentPane().repaint();
}

private void Menue0() {
//初始化菜单
JMenuBar jb=new JMenuBar();

JMenu funcation=new JMenu("功能");
JMenu about=new JMenu("关于");
//事件绑定
replay.addActionListener(this);
reLogin.addActionListener(this);
Exsist.addActionListener(this);
abouT.addActionListener(this);
funcation.add(replay);
funcation.add(reLogin);
funcation.add(Exsist);

about.add(abouT);

jb.add(funcation);
jb.add(about);
//将对象传回
this.setJMenuBar(jb);
}

private void Thelooking() {
//设置宽高
this.setSize(603,680);
//设置界面标题
this.setTitle("丰哥拼图单机模式 v-1.0");
//设置置顶显示
this.setAlwaysOnTop(true);
//设置界面居中展示
this.setLocationRelativeTo(null);
//设置界面关闭模式
this.setDefaultCloseOperation(3);
//取消图片居中
this.setLayout(null);
//添加监听组件
KeyListener a=new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
//定义一个变量接受A的值,A=65
int p=e.getKeyCode();
if(p==65){
printfall();

}
}

@Override
public void keyReleased(KeyEvent e) {
//解决顺利后多一个空白的bug
if(win()){
System.out.println("胜利啦");
return;
}
//定义一个变量接收按键信息
int w=e.getKeyCode();
System.out.println(w);
if (w==37){
//进行拼图位置变换
if(x1==3)return;
arr1[x1][y1]=arr1[x1+1][y1];
arr1[x1+1][y1]=0;
x1++;step++;
runimagin();
}else if(w==38){
if(y1==3)return;
//进行拼图位置变换
arr1[x1][y1]=arr1[x1][y1+1];
arr1[x1][y1+1]=0;
y1++;step++;
runimagin();
}else if(w==39){
if(x1==0)return;
//进行拼图位置变换
arr1[x1][y1]=arr1[x1-1][y1];
arr1[x1-1][y1]=0;
x1--;step++;
runimagin();
}else if(w==40){
if(y1==0)return;
//进行拼图位置变换
arr1[x1][y1]=arr1[x1][y1-1];
arr1[x1][y1-1]=0;
y1--;step++;
runimagin();
}else if(w==65){
//不能删,否则无法复原
runimagin();
}else if(w==81){
//一键成功
arr1= new int[][]{
{1, 5, 9, 13},
{2, 6, 10, 14},
{2, 7, 11, 15},
{4, 8, 12, 0}
};
runimagin();
}
}
};
this.addKeyListener(a);
}
//定义一个win方法判断是否成功
private boolean win(){
for (int i = 0; i < 4; i++) {
for (int i1 = 0; i1 < 4; i1++) {
if(i1==3&&i==3)continue;
if(arr1[i1][i]!=win1[i1][i])return false;
}
}
return true;
}

@Override
public void actionPerformed(ActionEvent e) {
Object luo=e.getSource();
if(luo==replay){
System.out.println("重新游戏");
step=0;
changearr();
runimagin();
}else if(luo==reLogin){
System.out.println("重新登录");
//关闭当前窗口
this.setVisible(false);
//打开登录界面
JFrame Login1=new JFrame();
Login1.setTitle("登录");
Login1.setVisible(true);
} else if (luo==Exsist) {
System.exit(0);
} else if (luo==abouT) {
System.out.println("公众号");
JDialog t=new JDialog();
JLabel s=new JLabel(new ImageIcon("C:\\Users\\luo66\\Desktop\\作业\\blue cup_java\\untitled11\\image\\pay.jpg"));
//设置管理器宽高
s.setBounds(0,0,451,484);
t.getContentPane().add(s);
//设置宽高
t.setSize(500,500);
//让弹窗置顶
t.setAlwaysOnTop(true);
//关闭默认的左上角展示
t.setLocationRelativeTo(null);
//不关闭无法操作
t.setModal(true);
//将弹窗显示出来
t.setVisible(true);

}
}
}

会用到的图片资源

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

all

pay

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝