Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
544 lines (513 loc) · 19.8 KB
/
Copy pathmain.lua
File metadata and controls
544 lines (513 loc) · 19.8 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.util.DisplayMetrics"
import "androidx.appcompat.widget.LinearLayoutCompat"
import "com.google.android.material.snackbar.Snackbar"
import "android.content.DialogInterface"
import "android.graphics.*"
import "com.google.android.material.button.MaterialButton"
import "com.google.android.material.chip.Chip"
import "com.google.android.material.shape.ShapeAppearanceModel"
import "android.graphics.Bitmap"
import "android.graphics.BitmapFactory"
import "android.graphics.drawable.BitmapDrawable"
import "android.graphics.drawable.GradientDrawable"
import "android.content.Context"
import "android.content.res.ColorStateList"
activity.setRequestedOrientation(1);
--------------------------------------------------
-- 工具函数
--------------------------------------------------
local v = activity.getSystemService(Context.VIBRATOR_SERVICE)
local PREFS = "TetrisSave"
local sp = activity.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
local function vibrate(millis)
local enabled = (activity.getSharedData("shadowEnabled") or "true") == "true"
if not enabled then return end
if v and v.hasVibrator() then v.vibrate(millis) end
end
local function vibrateTwice()
vibrate(100)
Handler().postDelayed(Runnable{ run = function() vibrate(100) end }, 100)
end
local function dp(v)
return v * activity.getResources().getDisplayMetrics().density
end
local function isDarkMode()
return (activity.getResources().getConfiguration().uiMode & 0x20) == 0x20
end
--------------------------------------------------
-- 布局常量
--------------------------------------------------
local metrics = DisplayMetrics()
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics)
local screenW, screenH = metrics.widthPixels, metrics.heightPixels
local BOARD_W, BOARD_H = 10, 20
local BTN_AREA_H = 260
local CELL_W = math.floor(screenW / BOARD_W)
local CELL_H = math.floor((screenH * 0.8 - BTN_AREA_H) / BOARD_H)
local CELL = math.min(CELL_W, CELL_H)
--------------------------------------------------
-- 方块数据
--------------------------------------------------
-- 完整添加 S 形和 Z 形后的两个变量
local SHAPES = {
{ {0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0} }, -- I 形
{ {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0}, {0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0}, -- L 形
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0}, {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0} },
{ {0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}, -- T 形
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0} },
{ {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0} }, -- O 形
{ {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0}, {0,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0}, -- J 形
{0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0} },
-- ↓↓↓ 以下两行即为新增的 S 形和 Z 形 ↓↓↓
{ {0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0}, {0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0} }, -- S 形
{ {0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0}, {0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0} } -- Z 形
}
-- 颜色同步扩展,新增两种颜色供 S 形和 Z 形使用
local COLORS = {
0xFFFF0000, -- 红
0xFF00FF00, -- 绿
0xFF0000FF, -- 蓝
0xFFFFFF00, -- 黄
0xFF00FFFF, -- 青
0xFFFF00FF, -- 紫(新增)
0xFFFFA500 -- 橙(新增)
}
--------------------------------------------------
-- 游戏状态
--------------------------------------------------
local board = {}
for y = 1, BOARD_H do board[y] = {}; for x = 1, BOARD_W do board[y][x] = 0 end end
local cur = { x=0, y=0, rot=1, id=1 }
local next = { id=math.random(1,#SHAPES), rot=1 } -- 新增:下一个方块
local score, handler, runnable = 0, Handler(), nil
local gameActive, paused = true, false
local parentView = activity.findViewById(android.R.id.content)
local function getDelay() return tonumber(activity.getSharedData("fallDelay") or 1000) end
--------------------------------------------------
-- 逻辑函数
--------------------------------------------------
local function valid(x, y, rot, id)
local shape = SHAPES[id][rot]
for i = 1, 16 do
if shape[i] == 1 then
local px, py = x + (i-1)%4, y + math.floor((i-1)/4)
if px < 1 or px > BOARD_W or py > BOARD_H or (py >= 1 and board[py][px] ~= 0) then return false end
end
end
return true
end
local function place(x, y, rot, id, val)
local shape = SHAPES[id][rot]
for i = 1, 16 do
if shape[i] == 1 then
local px, py = x + (i-1)%4, y + math.floor((i-1)/4)
if py >= 1 and py <= BOARD_H and px >= 1 and px <= BOARD_W then board[py][px] = val end
end
end
end
--------------------------------------------------
-- 改进:一次性消除的行数越多分数越高
--------------------------------------------------
local function clearLines()
-- 统计本次一次性消除的总行数
local lines = 0
local y = BOARD_H
while y >= 1 do
local full = true
for x = 1, BOARD_W do
if board[y][x] == 0 then
full = false
break
end
end
if full then
table.remove(board, y)
table.insert(board, 1, {})
for x = 1, BOARD_W do board[1][x] = 0 end
lines = lines + 1
else
y = y - 1
end
end
-- 根据一次性消除的行数给予阶梯分数
if lines > 0 then
local lineScore = { 100, 300, 500, 800 } -- 1/2/3/4 行对应得分
score = score + (lineScore[lines] or 800) -- 超过 4 行也按 4 行算
scoreLabel.setText("Score: " .. score)
speedLabel.setText("Speed: "..getDelay().." ms/格")
vibrate(math.min(50 + lines * 60, 500))
end
end
local function randomPiece()
return math.random(1, #SHAPES)
end
local function draw()
local bgColor = isDarkMode() and 0xFF222222 or 0xFFEEEEEE
for y = 1, BOARD_H do for x = 1, BOARD_W do cells[y][x].setBackgroundColor(bgColor) end end
for y = 1, BOARD_H do for x = 1, BOARD_W do if board[y][x] ~= 0 then cells[y][x].setBackgroundColor(COLORS[board[y][x]]) end end end
local shape = SHAPES[cur.id][cur.rot]
for i = 1, 16 do
if shape[i] == 1 then
local px, py = cur.x + (i-1)%4, cur.y + math.floor((i-1)/4)
if py >= 1 and py <= BOARD_H and px >= 1 and px <= BOARD_W then cells[py][px].setBackgroundColor(COLORS[cur.id]) end
end
end
end
-- 新增:绘制下一个方块
local function drawNext()
local bg = isDarkMode() and 0xFF222222 or 0xFFEEEEEE
for y=1,4 do for x=1,4 do miniCells[y][x].setBackgroundColor(bg) end end
local shape = SHAPES[next.id][next.rot]
for i=1,16 do
if shape[i]==1 then
local px,py = (i-1)%4+1, math.floor((i-1)/4)+1
if px>=1 and px<=4 and py>=1 and py<=4 then
miniCells[py][px].setBackgroundColor(COLORS[next.id])
end
end
end
end
local function newPiece()
cur.id, cur.rot, cur.x, cur.y = next.id, 1, math.floor(BOARD_W/2) - 1, 0
next.id = randomPiece()
drawNext() -- 刷新预览
return valid(cur.x, cur.y, cur.rot, cur.id)
end
local function saveState()
local ed = sp.edit()
ed.putBoolean("hasSave", true)
ed.putInt("score", score)
ed.putInt("cur.x", cur.x); ed.putInt("cur.y", cur.y)
ed.putInt("cur.rot", cur.rot); ed.putInt("cur.id", cur.id)
ed.putInt("next.id", next.id) -- 保存下一个
ed.putBoolean("paused", paused)
ed.putLong("fallDelay", getDelay())
for y = 1, BOARD_H do for x = 1, BOARD_W do ed.putInt("b"..y..x, board[y][x]) end end
ed.apply()
end
local function loadState()
if not sp.getBoolean("hasSave", false) then return false end
score = sp.getInt("score", 0)
cur.x, cur.y = sp.getInt("cur.x", 0), sp.getInt("cur.y", 0)
cur.rot, cur.id = sp.getInt("cur.rot", 1), sp.getInt("cur.id", 1)
next.id = sp.getInt("next.id", randomPiece())
paused = sp.getBoolean("paused", false)
activity.setSharedData("fallDelay", tostring(sp.getLong("fallDelay", 1000)))
for y = 1, BOARD_H do for x = 1, BOARD_W do board[y][x] = sp.getInt("b"..y..x, 0) end end
return true
end
local function resetGame()
sp.edit().clear().apply()
for y = 1, BOARD_H do board[y] = {}; for x = 1, BOARD_W do board[y][x] = 0 end end
score = 0; scoreLabel.setText("Score: 0")
speedLabel.setText("Speed: "..getDelay().." ms/格")
gameActive, paused = true, false
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_pause.png")))
pauseOverlay.setAlpha(0)
next.id = randomPiece()
if newPiece() then draw(); drawNext(); handler.postDelayed(runnable, getDelay()) end
end
local function showRestartDialog()
local wasPaused = paused
paused = true
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_play.png")))
pauseOverlay.setAlpha(0.5)
handler.removeCallbacks(runnable)
import "com.google.android.material.dialog.MaterialAlertDialogBuilder"
MaterialAlertDialogBuilder(activity)
.setTitle("确认重新开始")
.setMessage("是否要重新开始游戏?")
.setCancelable(false)
.setPositiveButton("确定", {onClick = function() resetGame() end})
.setNegativeButton("取消", {onClick = function(d)
if not wasPaused then paused = false; pauseOverlay.setAlpha(0); handler.postDelayed(runnable, getDelay()) end
d.dismiss()
end})
.show()
end
runnable = Runnable{
run = function()
if paused then return end
if valid(cur.x, cur.y+1, cur.rot, cur.id) then cur.y = cur.y + 1
else
place(cur.x, cur.y, cur.rot, cur.id, cur.id)
vibrate(30)
clearLines()
if not newPiece() then
handler.removeCallbacks(runnable)
gameActive = false
vibrateTwice()
Snackbar.make(parentView, "Game Over! Score: "..score, Snackbar.LENGTH_LONG)
.setAction("重新开始", {onClick = function() resetGame() end})
.show()
return
end
end
draw()
handler.postDelayed(runnable, getDelay())
end
}
local function onKey(key)
if not gameActive or paused then return end
if key == "left" and valid(cur.x-1, cur.y, cur.rot, cur.id) then cur.x = cur.x - 1
elseif key == "right" and valid(cur.x+1, cur.y, cur.rot, cur.id) then cur.x = cur.x + 1
elseif key == "rotate" then
local newRot = cur.rot % (#SHAPES[cur.id]) + 1
if valid(cur.x, cur.y, newRot, cur.id) then cur.rot = newRot end
elseif key == "down" then
while valid(cur.x, cur.y+1, cur.rot, cur.id) do cur.y = cur.y + 1 end
place(cur.x, cur.y, cur.rot, cur.id, cur.id)
vibrate(30)
clearLines()
if not newPiece() then return end
end
draw()
end
local function makeRoundRect(color, radius)
local gd = GradientDrawable()
gd.setColor(color); gd.setCornerRadius(radius)
return gd
end
--------------------------------------------------
-- 主界面
--------------------------------------------------
local scroll = ScrollView(activity)
local main = LinearLayout(activity)
main.setOrientation(LinearLayout.VERTICAL)
main.setGravity(Gravity.CENTER_HORIZONTAL)
main.setPadding(0, 0, 0, 20)
-- 顶部卡片
local topCard = LinearLayout(activity)
topCard.setOrientation(LinearLayout.HORIZONTAL)
topCard.setGravity(Gravity.CENTER_VERTICAL)
topCard.setPadding(30, 20, 30, 20)
local topLp = LinearLayout.LayoutParams(-1, -2)
topLp.setMargins(30, 30, 30, 20)
topCard.setLayoutParams(topLp)
local scoreContainer = LinearLayout(activity)
scoreContainer.setOrientation(LinearLayout.VERTICAL)
scoreContainer.setGravity(Gravity.START)
scoreLabel = TextView(activity)
scoreLabel.setText("Score: 0")
scoreLabel.setTextSize(22)
scoreLabel.setTextColor(0xFFFFFFFF)
speedLabel = TextView(activity)
speedLabel.setText("Speed: "..getDelay().." ms/格")
speedLabel.setTextSize(12)
speedLabel.setTextColor(0xFFAAAAAA)
scoreContainer.addView(scoreLabel)
scoreContainer.addView(speedLabel)
topCard.addView(scoreContainer)
local space = Space(activity)
local spaceLp = LinearLayout.LayoutParams(0, 1, 1)
space.setLayoutParams(spaceLp)
topCard.addView(space)
pauseBtn = MaterialButton(activity)
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_pause.png")))
pauseBtn.setIconGravity(MaterialButton.ICON_GRAVITY_TEXT_START)
pauseBtn.setIconPadding(0)
pauseBtn.setPadding(16, 12, 16, 12)
pauseBtn.setOnClickListener(View.OnClickListener{
onClick = function()
paused = not paused
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir()..(paused and "/ic_play.png" or "/ic_pause.png"))))
if paused then pauseOverlay.setAlpha(0.5); handler.removeCallbacks(runnable)
else pauseOverlay.setAlpha(0); handler.postDelayed(runnable, getDelay()) end
end
})
topCard.addView(pauseBtn)
main.addView(topCard)
--------------------------------------------------
-- 游戏网格 + 预览区(顶部对齐)
--------------------------------------------------
local gridWrapper = LinearLayout(activity)
gridWrapper.setOrientation(LinearLayout.HORIZONTAL)
gridWrapper.setGravity(Gravity.TOP)
gridWrapper.setPadding(0, 0, 0, 0)
-- 主游戏网格
local grid = LinearLayout(activity)
grid.setOrientation(LinearLayout.VERTICAL)
cells = {}
for y = 1, BOARD_H do
local row = LinearLayout(activity)
row.setOrientation(LinearLayout.HORIZONTAL)
cells[y] = {}
for x = 1, BOARD_W do
local cell = TextView(activity)
local p = LinearLayout.LayoutParams(CELL, CELL)
p.setMargins(1, 1, 1, 1)
cell.setLayoutParams(p)
cell.setBackground(makeRoundRect(isDarkMode() and 0xFF222222 or 0xFFEEEEEE, 4))
row.addView(cell)
cells[y][x] = cell
end
grid.addView(row)
end
gridWrapper.addView(grid)
-- 预览容器(Next: + 4×4)
local previewContainer = LinearLayout(activity)
previewContainer.setOrientation(LinearLayout.VERTICAL)
previewContainer.setGravity(Gravity.CENTER_HORIZONTAL)
previewContainer.setPadding(20, 0, 20, 0)
local nextLabel = TextView(activity)
nextLabel.setText("Next:")
nextLabel.setTextSize(14)
nextLabel.setTextColor(isDarkMode() and 0xFFFFFFFF or 0xFF000000)
nextLabel.setPadding(0, 0, 0, dp(4))
previewContainer.addView(nextLabel)
local miniCell = math.floor((screenW - BOARD_W*CELL - 120) / 4)
if miniCell < 8 then miniCell = 8 end
local miniGrid = LinearLayout(activity)
miniGrid.setOrientation(LinearLayout.VERTICAL)
miniCells = {}
for y = 1, 4 do
local row = LinearLayout(activity)
row.setOrientation(LinearLayout.HORIZONTAL)
miniCells[y] = {}
for x = 1, 4 do
local cell = TextView(activity)
local p = LinearLayout.LayoutParams(miniCell, miniCell)
p.setMargins(1, 1, 1, 1)
cell.setLayoutParams(p)
cell.setBackground(makeRoundRect(isDarkMode() and 0xFF222222 or 0xFFEEEEEE, 2))
row.addView(cell)
miniCells[y][x] = cell
end
miniGrid.addView(row)
end
previewContainer.addView(miniGrid)
gridWrapper.addView(previewContainer)
-- 暂停遮罩
pauseOverlay = View(activity)
pauseOverlay.setBackgroundColor(0xAA000000)
pauseOverlay.setAlpha(0)
local frame = FrameLayout(activity)
frame.addView(gridWrapper)
frame.addView(pauseOverlay)
local gridLp = LinearLayout.LayoutParams(-2, -2)
gridLp.gravity = Gravity.CENTER
frame.setLayoutParams(gridLp)
main.addView(frame)
-- 方向按钮
local btnRow = LinearLayout(activity)
btnRow.setOrientation(LinearLayout.HORIZONTAL)
btnRow.setPadding(30, 20, 30, 0)
local function makeIconBtn(iconFile, key)
local b = MaterialButton(activity)
b.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/"..iconFile)))
b.setIconGravity(MaterialButton.ICON_GRAVITY_TEXT_START)
b.setIconPadding(0)
b.setPadding(16, 12, 16, 12)
local p = LinearLayout.LayoutParams(0, -2, 1)
p.setMargins(6, 0, 6, 0)
b.setLayoutParams(p)
b.setOnClickListener{ onClick = function() onKey(key) end }
btnRow.addView(b)
end
makeIconBtn("ic_left.png", "left")
makeIconBtn("ic_right.png", "right")
makeIconBtn("ic_down.png", "down")
makeIconBtn("ic_rotate.png", "rotate")
main.addView(btnRow)
-- 底部重启 / 设置按钮
local bottomRow = LinearLayout(activity)
bottomRow.setOrientation(LinearLayout.HORIZONTAL)
bottomRow.setGravity(Gravity.CENTER)
bottomRow.setPadding(30, 110, 30, 30)
restartBtn = Chip(activity)
restartBtn.setText("重新开始")
restartBtn.setChipIconVisible(true)
restartBtn.setChipIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_close.png")))
restartBtn.setChipIconSize(dp(24))
restartBtn.setIconStartPadding(dp(8))
local restartLp = LinearLayout.LayoutParams(0, dp(56), 1)
restartLp.setMargins(0, 0, dp(12), 0)
restartBtn.setLayoutParams(restartLp)
restartBtn.setOnClickListener{ onClick = function() showRestartDialog() end }
bottomRow.addView(restartBtn)
settingsBtn = Chip(activity)
settingsBtn.setText("")
settingsBtn.setChipIconVisible(true)
settingsBtn.setChipIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_settings.png")))
settingsBtn.setChipIconSize(dp(24))
settingsBtn.setIconStartPadding(0)
settingsBtn.setIconEndPadding(0)
settingsBtn.setLayoutParams(LinearLayout.LayoutParams(dp(40), dp(56)))
settingsBtn.setOnClickListener(View.OnClickListener{
onClick = function()
paused = true
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_play.png")))
pauseOverlay.setAlpha(0.5)
handler.removeCallbacks(runnable)
activity.newActivity("settings.lua")
end
})
bottomRow.addView(settingsBtn)
main.addView(bottomRow)
-- Chip 样式
local function stylizeChip(chip)
chip.post(function()
chip.setPadding(chip.getPaddingLeft(), 0, chip.getPaddingRight(), 0)
local model = ShapeAppearanceModel().toBuilder().setAllCornerSizes(dp(28)).build()
chip.setShapeAppearanceModel(model)
end)
end
stylizeChip(settingsBtn)
settingsBtn.post(function()
local drawable = settingsBtn.getChipDrawable()
drawable.setStrokeWidth(dp(2))
drawable.setStrokeColor(ColorStateList.valueOf(Color.parseColor("#B9ABABAB")))
end)
restartBtn.post(function()
restartBtn.setPadding(restartBtn.getPaddingLeft(), 0, restartBtn.getPaddingRight(), 0)
local model = ShapeAppearanceModel().toBuilder().setAllCornerSizes(dp(28)).build()
restartBtn.setShapeAppearanceModel(model)
local drawable = restartBtn.getChipDrawable()
drawable.setStrokeWidth(dp(2))
drawable.setStrokeColor(ColorStateList.valueOf(Color.parseColor("#84FF0000")))
end)
scroll.addView(main)
activity.setTitle("AndLua Tetris")
activity.setContentView(scroll)
--------------------------------------------------
-- 生命周期
--------------------------------------------------
local application = activity.getApplication()
application.registerActivityLifecycleCallbacks(luajava.createProxy(
"android.app.Application$ActivityLifecycleCallbacks", {
onActivityPaused = function(activity)
saveState()
if not paused then
paused = true
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir().."/ic_play.png")))
pauseOverlay.setAlpha(0.5)
handler.removeCallbacks(runnable)
end
end,
onActivityResumed = function() end,
onActivityCreated = function() end,
onActivityStarted = function() end,
onActivityStopped = function() end,
onActivitySaveInstanceState = function() end,
onActivityDestroyed = function() end,
}))
--------------------------------------------------
-- 启动
--------------------------------------------------
math.randomseed(os.time())
if loadState() then
scoreLabel.setText("Score: "..score)
speedLabel.setText("Speed: "..getDelay().." ms/格")
pauseBtn.setIcon(BitmapDrawable(activity.getResources(), BitmapFactory.decodeFile(activity.getLuaDir()..(paused and "/ic_play.png" or "/ic_pause.png"))))
pauseOverlay.setAlpha(paused and 0.5 or 0)
draw(); drawNext()
if gameActive and not paused then handler.postDelayed(runnable, getDelay()) end
else
next.id = randomPiece()
if newPiece() then draw(); drawNext(); handler.postDelayed(runnable, getDelay()) end
end