本文实例为大家分享了java实现2048小游戏的具体代码,供大家参考,具体内容如下
一、实现效果
二、实现代码
Check表示格子,GameView实现游戏视图界面及功能,是核心。
Check.java
?
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
|
import java.awt.Color;
import java.awt.Font;
// 方格类
public class Check {
public int value;
Font font1 = new Font( "宋体" , Font.BOLD, 46 );
Font font2 = new Font( "宋体" , Font.BOLD, 40 );
Font font3 = new Font( "宋体" , Font.BOLD, 34 );
Font font4 = new Font( "宋体" , Font.BOLD, 28 );
Font font5 = new Font( "宋体" , Font.BOLD, 22 );
public Check() {
value = 0 ; //value为方格中数字
}
//字体颜色
public Color getForeground() {
switch (value) {
case 0 :
return new Color( 0xcdc1b4 ); //0的颜色与背景色一致,相当于没有数字
case 2 :
case 4 :
return Color.BLACK;
default :
return Color.WHITE;
}
}
//字体背景颜色,即方格颜色
public Color getBackground() {
switch (value) {
case 0 :
return new Color( 0xcdc1b4 );
case 2 :
return new Color( 0xeee4da );
case 4 :
return new Color( 0xede0c8 );
case 8 :
return new Color( 0xf2b179 );
case 16 :
return new Color( 0xf59563 );
case 32 :
return new Color( 0xf67c5f );
case 64 :
return new Color( 0xf65e3b );
case 128 :
return new Color( 0xedcf72 );
case 256 :
return new Color( 0xedcc61 );
case 512 :
return new Color( 0xedc850 );
case 1024 :
return new Color( 0xedc53f );
case 2048 :
return new Color( 0xedc22e );
case 4096 :
return new Color( 0x65da92 );
case 8192 :
return new Color( 0x5abc65 );
case 16384 :
return new Color( 0x248c51 );
default :
return new Color( 0x248c51 );
}
}
public Font getCheckFont() {
if (value < 10 ) {
return font1;
}
if (value < 100 ) {
return font2;
}
if (value < 1000 ) {
return font3;
}
if (value < 10000 ) {
return font4;
}
return font5;
}
}
|
GameView.java
?
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
|