forked from Gerold55/laptop_apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris_app.lua
285 lines (246 loc) · 7.18 KB
/
tetris_app.lua
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
-- Based on https://github.com/minetest-mods/homedecor_modpack/blob/master/computer/tetris.lua
local shapes = {
{ { x = {0, 1, 0, 1}, y = {0, 0, 1, 1} } },
{ { x = {1, 1, 1, 1}, y = {0, 1, 2, 3} },
{ x = {0, 1, 2, 3}, y = {1, 1, 1, 1} } },
{ { x = {0, 0, 1, 1}, y = {0, 1, 1, 2} },
{ x = {1, 2, 0, 1}, y = {0, 0, 1, 1} } },
{ { x = {1, 0, 1, 0}, y = {0, 1, 1, 2} },
{ x = {0, 1, 1, 2}, y = {0, 0, 1, 1} } },
{ { x = {1, 2, 1, 1}, y = {0, 0, 1, 2} },
{ x = {0, 1, 2, 2}, y = {1, 1, 1, 2} },
{ x = {1, 1, 0, 1}, y = {0, 1, 2, 2} },
{ x = {0, 0, 1, 2}, y = {0, 1, 1, 1} } },
{ { x = {1, 1, 1, 2}, y = {0, 1, 2, 2} },
{ x = {0, 1, 2, 0}, y = {1, 1, 1, 2} },
{ x = {0, 1, 1, 1}, y = {0, 0, 1, 2} },
{ x = {0, 1, 2, 2}, y = {1, 1, 1, 0} } },
{ { x = {1, 0, 1, 2}, y = {0, 1, 1, 1} },
{ x = {1, 1, 1, 2}, y = {0, 1, 2, 1} },
{ x = {0, 1, 2, 1}, y = {1, 1, 1, 2} },
{ x = {0, 1, 1, 1}, y = {1, 0, 1, 2} } } }
local base_color_texture = "laptop_tetris_block.png"
local base_color_alpha = '128'
local colors = { base_color_texture.."^[colorize:#00FFFF:"..base_color_alpha, base_color_texture.."^[colorize:#FF00FF:"..base_color_alpha, base_color_texture.."^[colorize:#FF0000:"..base_color_alpha,
base_color_texture.."^[colorize:#0000FF:"..base_color_alpha, base_color_texture.."^[colorize:#00FF00:"..base_color_alpha, base_color_texture.."^[colorize:#FF4500:"..base_color_alpha,
base_color_texture.."^[colorize:#FFFF00:"..base_color_alpha }
local boardx, boardy = 0, 0
local sizex, sizey, size = 0.42, 0.44, 0.47
local comma = ","
local semi = ";"
local close = "]"
local concat = table.concat
local insert = table.insert
local tetris_class = {}
tetris_class.__index = tetris_class
local function get_tetris(app, data)
local self = setmetatable({}, tetris_class)
self.data = data
self.app = app
return self
end
function tetris_class:new_game()
local nex = math.random(7)
self.data.t = {
board = {},
boardstring = "",
previewstring = self:draw_shape(nex, 0, 0, 1, 6.5, 0.8),
score = 0,
cur = math.random(7),
nex = nex,
x=4, y=0, rot=1
}
self.app:get_timer():start(0.3)
end
function tetris_class:update_boardstring()
local scr = {}
local ins = #scr
local fixed_color
if self.app.os.theme.monochrome_textcolor then
fixed_color = base_color_texture.."^[colorize:"..
self.app.os.theme.monochrome_textcolor..
":"..base_color_alpha
end
for i, line in pairs(self.data.t.board) do
for _, tile in pairs(line) do
local tmp = { "image[",
tile[1]*sizex+boardx, comma,
i*sizey+boardy, semi,
size, comma, size, semi,
fixed_color or colors[tile[2]], close }
ins = ins + 1
scr[ins] = concat(tmp)
end
end
self.data.t.boardstring = concat(scr)
end
function tetris_class:add()
local t = self.data.t
local d = shapes[t.cur][t.rot]
for i=1,4 do
local l = d.y[i] + t.y
if not t.board[l] then t.board[l] = {} end
insert(t.board[l], {d.x[i] + t.x, t.cur})
end
end
function tetris_class:scroll(l)
for i=l, 1, -1 do
self.data.t.board[i] = self.data.t.board[i-1] or {}
end
end
function tetris_class:check_lines()
for i, line in pairs(self.data.t.board) do
if #line >= 10 then
self:scroll(i)
self.data.t.score = self.data.t.score + 20
end
end
end
function tetris_class:check_position(x, y, rot)
local d = shapes[self.data.t.cur][rot]
for i=1,4 do
local cx, cy = d.x[i]+x, d.y[i]+y
if cx < 0 or cx > 9 or cy < 0 or cy > 19 then
return false
end
for _, tile in pairs(self.data.t.board[ cy ] or {}) do
if tile[1] == cx then return false end
end
end
return true
end
function tetris_class:stuck()
local t = self.data.t
if self:check_position(t.x, t.y+1, t.rot) then
return false
else
return true
end
end
function tetris_class:tick()
local t = self.data.t
if self:stuck() then
if t.y <= 0 then
return false
end
self:add()
self:check_lines()
self:update_boardstring()
t.cur, t.nex = t.nex, math.random(7)
t.x, t.y, t.rot = 4, 0, 1
t.previewstring = self:draw_shape(t.nex, 0, 0, 1, 6.5, 0.8)
else
t.y = t.y + 1
end
return true
end
function tetris_class:move(dx, dy)
local t = self.data.t
local newx, newy = t.x+dx, t.y+dy
if not self:check_position(newx, newy, t.rot) then
return
end
t.x, t.y = newx, newy
end
function tetris_class:rotate(dr)
local t = self.data.t
local no = #(shapes[t.cur])
local newrot = (t.rot+dr) % no
if newrot<1 then newrot = newrot+no end
if not self:check_position(t.x, t.y, newrot) then
return
end
t.rot = newrot
end
function tetris_class:key(fields)
local t = self.data.t
if fields.left then
self:move(-1, 0)
end
if fields.rotateleft then
self:rotate(-1)
end
if fields.down then
t.score = t.score + 1
self:move(0, 1)
end
if fields.drop then
while not self:stuck() do
t.score = t.score + 2
self:move(0, 1)
end
end
if fields.rotateright then
self:rotate(1)
end
if fields.right then
self:move(1, 0)
end
end
function tetris_class:draw_shape(id, x, y, rot, posx, posy)
local d = shapes[id][rot]
local scr = {}
local ins = #scr
local color = colors[id]
if self.app.os.theme.monochrome_textcolor then
color = base_color_texture.."^[colorize:"..
self.app.os.theme.monochrome_textcolor..
":"..base_color_alpha
end
for i=1,4 do
local tmp = { "image[",
(d.x[i]+x)*sizex+posx, comma,
(d.y[i]+y)*sizey+posy, semi,
size, comma, size, semi,
color, close }
ins = ins + 1
scr[ins] = concat(tmp)
end
return concat(scr)
end
laptop.register_app("tetris", {
app_name = "Tetris",
app_icon = "laptop_tetris_icon.png",
app_info = "Falling Tile Game",
formspec_func = function(app, mtos)
local data = mtos.bdev:get_app_storage('ram', 'tetris')
local tetris = get_tetris(app, data)
local timer = minetest.get_node_timer(mtos.pos)
if not data.t then
return mtos.theme:get_button('2,4;2,2', 'major', 'new', 'New Game', 'Start a new game')
end
local buttons = mtos.theme:get_button('6,6;1,1', 'minor', 'left', '<')..
mtos.theme:get_button('6,5;1,1', 'minor', 'rotateleft', 'L')..
mtos.theme:get_button('7,5;1,1', 'minor', 'down', 'v')..
mtos.theme:get_button('7,6;1,1', 'minor', 'drop', 'V')..
mtos.theme:get_button('8,5;1,1', 'minor', 'rotateright', 'R')..
mtos.theme:get_button('8,6;1,1', 'minor', 'right', '>')..
mtos.theme:get_button('6,3.5;3,1', 'major', 'new', 'New Game', 'Start a new game')
local t = tetris.data.t
return 'container[3,1]background[0,-0.05;4.35,9;'.. mtos.theme.contrast_background .. ']' ..
t.boardstring .. t.previewstring ..
tetris:draw_shape(t.cur, t.x, t.y, t.rot, boardx, boardy) ..
mtos.theme:get_label('6.5,0.1', 'Next...') ..
mtos.theme:get_label('6.5,2.7', 'Score:...'..t.score) ..
buttons .. 'container_end[]'
end,
receive_fields_func = function(app, mtos, sender, fields)
local data = mtos.bdev:get_app_storage('ram', 'tetris')
local tetris = get_tetris(app, data)
if fields.new then
tetris:new_game()
elseif fields.continue then
app:get_timer():start(0.3)
else
tetris:key(fields)
end
end,
on_timer = function(app, mtos)
local data = mtos.bdev:get_app_storage('ram', 'tetris')
if not data.t then
return false
else
return get_tetris(app, data):tick()
end
end,
})