突然想要在android上写一个消消乐的代码,在此之前没有系统地学过java的面向对象,也没有任何android相关知识,不过还是会一点C++。8月初开始搭建环境,在这上面花了相当多的时间,然后看了一些视频和电子书,对android有了一个大概的了解,感觉差不多了的时候就开始写了。
疯狂地查阅各种资料,反反复复了好几天后,也算是写出了个成品。原计划有很多地方还是可以继续写下去的,比如UI设计,比如动画特效,时间设计,关卡设计,以及与数据库的连接,如果可以的话还能写个联网功能,当然因为写到后期内心感到格外的疲倦,所以以上内容全部被砍掉了。
所以最后的成果就只有这样了……因为完全没有设计过所以非常难看,功能也只有消方块而已,图片还是从之前写的连连看里搬过来的。反正就算一个小的demo好了。
布局
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
26private void initBtn(int i,int j) { btn[8*i+j].setBackgroundDrawable (this.getResources().getDrawable(getStyle())); point p = new point(i,j,btn[8*i+j],id); btn[8*i+j].setTag(p); map[i][j] = num; } //…… LinearLayout vlayout = (LinearLayout)findViewById(R.id.vlayout); TableLayout tlayout = new TableLayout(this); TableRow row[] = new TableRow[8]; for(int i=0;i<8;i++){ row[i] = new TableRow(this); row[i].setGravity(Gravity.CENTER); for(int j=0;j<8;j++){ btn[8*i+j] = new ImageButton(this); btn[8*i+j].setLayoutParams(new TableRow.LayoutParams(38,38)); initBtn(i,j); btn[8*i+j].setOnClickListener(listener); row[i].addView(btn[8*i+j]); } tlayout.addView(row[i]); } //……
其中getStyle()函数返回了按钮的样式id,该id是随机生成的。
point p存储了关于按钮的信息,它在按钮点击事件中会被使用。
android中的按钮有三种状态:点击态、普通态、焦点态。最后一个的意思是用户按方向键盘(或类似功能键)来控制哪个按钮处于焦点,这样就可以不通过鼠标对按钮进行操作。当然在这里并没有用到这个状态,只用到了前面两种,但是因为已经有现成图片了,就把三种状态都写入了btn?.xml(放在drawable文件夹下),使用的时候直接引用这个xml文件就可以了。
btn1.xml:
1
2
3
4
5
6
7
8
9<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/a1_2" android:state_pressed="true"/> <item android:drawable="@drawable/a1" android:state_focused="false" android:state_pressed="false"/> <item android:drawable="@drawable/a1_1" android:state_focused="true"/> <item android:drawable="@drawable/a1" android:state_focused="false"/> </selector>

判断是否可消去
判断地图是否还存在解

多线程
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@Override public void run() { if(find()){ flag = false; int n = 10; alpha = 255; while(n--!=0){ wait(30); mHandler.sendEmptyMessage(0); } wait(100); mHandler.sendEmptyMessage(1); } else if(flag==true){ swapMap(p1.x,p1.y,p2.x,p2.y); wait(300); mHandler.sendEmptyMessage(2); } else if(flag==false){ p1 = new point(-2,-2); p2 = new point(-2,-2); if(check()==false){ mHandler.sendEmptyMessage(3); } } }
代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/background" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/vlayout"> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="40dip" android:textSize="18sp" android:autoText="true" android:textColor="#000000" android:capitalize="sentences" android:text="开心消消乐" /> </LinearLayout>
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411package com.example.android.market.licensing; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; import android.view.Gravity; import android.view.View; @SuppressLint("NewApi") public class MainActivity extends Activity implements Runnable { private static final String TAG = "App"; private point p1 = new point(-2,-2); private point p2 = new point(-2,-2);; private boolean isPoint1 = true; private int map[][] = new int[8][8]; private int id; private int mark[][] = new int[8][8]; private Thread thread; private int num; private int alpha = 255; boolean flag = false; private int score = 0; private TextView text; ImageButton[] btn = new ImageButton[64]; private int getStyle(){ num = (int)(1+Math.random()*(7-1+1)); switch(num){ case 1:return id = R.drawable.btn1; case 2:return id = R.drawable.btn2; case 3:return id = R.drawable.btn3; case 4:return id = R.drawable.btn4; case 5:return id = R.drawable.btn5; case 6:return id = R.drawable.btn6; case 7:return id = R.drawable.btn7; default:return id = 0; } } public MainActivity() { } private void initBtn(int i,int j) { btn[8*i+j].setBackgroundDrawable (this.getResources().getDrawable(getStyle())); point p = new point(i,j,btn[8*i+j],id); btn[8*i+j].setTag(p); map[i][j] = num; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout vlayout = (LinearLayout)findViewById(R.id.vlayout); TableLayout tlayout = new TableLayout(this); TableRow row[] = new TableRow[8]; for(int i=0;i<8;i++){ row[i] = new TableRow(this); row[i].setGravity(Gravity.CENTER); for(int j=0;j<8;j++){ btn[8*i+j] = new ImageButton(this); btn[8*i+j].setLayoutParams(new TableRow.LayoutParams(38,38)); initBtn(i,j); btn[8*i+j].setOnClickListener(listener); row[i].addView(btn[8*i+j]); } tlayout.addView(row[i]); } text = new TextView(this); text.setText("分数 : 0"); vlayout.addView(tlayout); vlayout.addView(text); while(find()){ updateState(); } thread = new Thread(this); } private void wait(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { if(find()){ flag = false; int n = 10; alpha = 255; while(n--!=0){ wait(30); mHandler.sendEmptyMessage(0); } wait(100); mHandler.sendEmptyMessage(1); } else if(flag==true){ swapMap(p1.x,p1.y,p2.x,p2.y); wait(300); mHandler.sendEmptyMessage(2); } else if(flag==false){ p1 = new point(-2,-2); p2 = new point(-2,-2); if(check()==false){ mHandler.sendEmptyMessage(3); } } } void swapImage() { p1.v.setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p2.id)); p2.v.setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p1.id)); int tmp =p1.id; p1.id = p2.id; p2.id = tmp; p1.v.setTag(p1); p2.v.setTag(p2); } private void updateBtn(int x1,int y1,int x2,int y2) { map[x1][y1] = map[x2][y2]; point p = (point)(btn[8*x1+y1].getTag()); p.id = ((point)(btn[8*x2+y2].getTag())).id; btn[8*x1+y1].setTag(p); btn[8*x1+y1].setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p.id)); } private void updateBtn(int i,int j) { btn[8*i+j].setBackgroundDrawable (MainActivity.this.getResources().getDrawable(getStyle())); map[i][j] = num; point p = (point)(btn[8*i+j].getTag()); p.id = id; btn[8*i+j].setTag(p); } private void hideBtn(){ alpha -= 25; for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(mark[i][j]!=0&&mark[i][j]!=2){ btn[8*i+j].getBackground().setAlpha(alpha); } } } } private void updateState() { for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ btn[8*i+j].getBackground().setAlpha(255); } } for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(mark[i][j]==1){ for(int k=i;k>0;k--){ updateBtn(k,j,k-1,j); } updateBtn(0,j); } else if(mark[i][j]>=3){ if(i-mark[i][j]>=0) { updateBtn(i,j,i-mark[i][j],j); updateBtn(i-mark[i][j],j); } else{ updateBtn(i,j); } } else if(mark[i][j]==2){ updateBtn(i,j); } } } } public Handler mHandler=new Handler() { public void handleMessage(Message msg) { switch(msg.what){ case 0:{ hideBtn(); break; } case 1:{ updateState(); text.setText("分数 " + ((Integer)score).toString()); thread.start(); break; } case 2:{ swapImage(); p1 = new point(-2,-2); p2 = new point(-2,-2); break; } case 3:{ Toast.makeText(MainActivity.this, "已自动生成新地图", Toast.LENGTH_SHORT).show(); for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ initBtn(i,j); } } while(find()){ updateState(); } break; } } super.handleMessage(msg); } }; private boolean find() { for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ mark[i][j] = 0; } } boolean flag = false; // heng for(int i=0;i<8;i++){ int count = 1; for(int j=0;j<7;j++){ if(map[i][j]==map[i][j+1]){ count++; if(count==3){ flag = true; mark[i][j-1] = 1; mark[i][j] = 1; mark[i][j+1] = 1; score += 15; } else if(count>3){ mark[i][j+1] = 1; score += 5; } } else count = 1; } } //shu for(int j=0;j<8;j++){ int count = 1; for(int i=0;i<7;i++){ if(map[i][j]==map[i+1][j]){ count++; if(count==3){ flag = true; if(mark[i][j]==1)score+=10; else score +=15; mark[i-1][j] = 3; mark[i][j] = 3; mark[i+1][j] = 3; for(int k=i-5;k>=0;k--){ mark[k][j] = 2; } } else if(count>3){ mark[i+1][j] = count; for(int k=1;k<count;k++){ mark[i-k+1][j]++; } if(i-2*count+2>=0)mark[i-2*count+2][j] = 0; score += 5; } } else count = 1; } } return flag; } private boolean check(int i,int j) { //shu int count = 1; if(i>=1&&map[i-1][j]==map[i][j]){ count++; if(i>=2&&map[i-2][j]==map[i][j]){ count++; } } if(count>=3)return true; if(i+1<8&&map[i+1][j]==map[i][j]){ count++; if(i+2<8&&map[i+2][j]==map[i][j]){ count++; } } if(count>=3)return true; //heng count = 1; if(j>=1&&map[i][j-1]==map[i][j]){ count++; if(j>=2&&map[i][j-2]==map[i][j]){ count++; } } if(count>=3)return true; if(j+1<8&&map[i][j+1]==map[i][j]){ count++; if(j+2<8&&map[i][j+2]==map[i][j]){ count++; } } if(count>=3)return true; return false; } private void swapMap(int x1,int y1,int x2,int y2) { int tmp = map[x1][y1]; map[x1][y1] = map[x2][y2]; map[x2][y2] = tmp; } private boolean check() { //heng for(int i=0;i<8;i++){ for(int j=0;j<7;j++){ swapMap(i,j,i,j+1); if(check(i,j)){ swapMap(i,j,i,j+1); return true; } if(check(i,j+1)){ swapMap(i,j,i,j+1); return true; } swapMap(i,j,i,j+1); } } //shu for(int j=0;j<8;j++){ for(int i=0;i<7;i++){ swapMap(i,j,i+1,j); if(check(i,j)){ swapMap(i,j,i+1,j); return true; } if(check(i+1,j)){ swapMap(i,j,i+1,j); return true; } swapMap(i,j,i+1,j); } } return false; } ImageButton.OnClickListener listener = new ImageButton.OnClickListener(){//创建监听对象 @SuppressLint("NewApi") public void onClick(View v) { if(isPoint1){ p1 = (point)v.getTag(); isPoint1 = false; } else { p2 = (point)v.getTag(); isPoint1 = true; } if(((p1.x-p2.x==1||p1.x-p2.x==-1)&&p1.y==p2.y)|| (p1.y-p2.y==1||p1.y-p2.y==-1)&&p1.x==p2.x){ flag = true; swapMap(p1.x,p1.y,p2.x,p2.y); swapImage(); thread.start(); } } }; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package com.example.android.market.licensing; import android.view.View; public class point { public int x; public int y; View v; int id; point(int _x,int _y,View _v,int _id){ x = _x; y = _y; v = _v; id = _id; } point(int _x,int _y){ x = _x; y = _y; } }
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<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.market.licensing" android:versionCode="2" android:versionName="1.1"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- Devices >= 3 have version of Android Market that supports licensing. --> <uses-sdk android:minSdkVersion="3" /> <!-- Required permission to check licensing. --> <uses-permission android:name="com.android.vending.CHECK_LICENSE" /> </manifest>
最后
以上就是糟糕冰淇淋最近收集整理的关于[Android] 开心消消乐代码(写的比较简单)的全部内容,更多相关[Android]内容请搜索靠谱客的其他文章。
发表评论 取消回复