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
|
-- mod-version:3
local core = require "core"
local config = require "core.config"
local style = require "core.style"
local common = require "core.common"
local View = require "core.view"
local command = require "core.command"
local keymap = require "core.keymap"
local TetrisView = View:extend()
config.plugins.tetris = common.merge({
tick = 0.5, -- The amount of time in seconds it takes for a piece to fall one line at 0 score.
height = 30, -- The amount of cells of height.
width = 10, -- The amount of cells of width.
cell_size = 18, -- The size in pixels of each cell.
cell_padding = 2, -- pixels between each cell
drop_shadow = true, -- Should cast a drop shadow.
lock_delay = 3, -- the multiplier for lock delay over a normal tick. set to 0 to disable
down_amount = 1 -- the amount we move a tetronimo down when you hit the down key; change to math.huge for instant.
}, config.plugins.tetris)
function TetrisView:new(options)
TetrisView.super.new(self)
self.cell_size = options.cell_size
self.cell_padding = options.cell_padding
self.grid = { x = options.width, y = options.height }
self.size.x = self.grid.x * (self.cell_size + self.cell_padding) + style.padding.x * 2
self.cells = { }
self.score = 0
self.paused = false
self.initial_tick = options.tick
self.lock_delay = options.lock_delay
self.drop_shadow = options.drop_shadow
self.tick = self:calculate_tick(self.score)
self.finished = false
self.thread = core.add_thread(function()
while not self.finished do
self:step()
core.redraw = true
coroutine.yield(self.tick)
end
end)
-- easier to specify rotations than to start doing matrix multiplication
self.tetronimos = {
{
color = { common.color "#ff0000" },
shape = { {
0,1,1,0,
0,1,1,0,
0,0,0,0,
0,0,0,0
} }
},
{
color = { common.color "#00ff00" },
shape = { {
1,1,0,0,
1,0,0,0,
1,0,0,0,
0,0,0,0
}, {
1,1,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0
}, {
0,0,1,0,
0,0,1,0,
0,1,1,0,
0,0,0,0
}, {
0,0,0,0,
1,0,0,0,
1,1,1,0,
0,0,0,0
} }
},
{
color = { common.color "#0000ff" },
shape = { {
1,0,0,0,
1,0,0,0,
1,1,0,0,
0,0,0,0
}, {
1,1,1,0,
1,0,0,0,
0,0,0,0,
0,0,0,0
}, {
0,1,1,0,
0,0,1,0,
0,0,1,0,
0,0,0,0
}, {
0,0,0,0,
0,0,1,0,
1,1,1,0,
0,0,0,0
} }
},
{
color = { common.color "#00ffff" },
shape = { {
1,1,0,0,
0,1,1,0,
0,0,0,0,
0,0,0,0
}, {
0,0,1,0,
0,1,1,0,
0,1,0,0,
0,0,0,0
}, {
0, 0,0,0,
1,1,0,0,
0,1,1,0,
0,0,0,0
}, {
0,1,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0
} }
},
{
color = { common.color "#ffff00" },
shape = { {
0,1,1,0,
1,1,0,0,
0,0,0,0,
0,0,0,0
}, {
0,1,0,0,
0,1,1,0,
0,0,1,0,
0,0,0,0
}, {
0,0,0,0,
0,1,1,0,
1,1,0,0,
0,0,0,0
}, {
1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0
} }
},
{
color = { common.color "#ff00ff" },
shape = { {
0,1,0,0,
0,1,0,0,
0,1,0,0,
0,1,0,0
}, {
0,0,0,0,
1,1,1,1,
0,0,0,0,
0,0,0,0
}, {
0,0,1,0,
0,0,1,0,
0,0,1,0,
0,0,1,0
}, {
0,0,0,0,
0,0,0,0,
1,1,1,1,
0,0,0,0
} }
},
{
color = { common.color "#ffffff" },
shape = { {
1,1,1,0,
0,1,0,0,
0,0,0,0,
0,0,0,0
}, {
0,0,1,0,
0,1,1,0,
0,0,1,0,
0,0,0,0
}, {
0,0,0,0,
0,1,0,0,
1,1,1,0,
0,0,0,0
}, {
1,0,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0
} }
}
}
self.live_piece = nil
self.hold_piece = nil
end
function TetrisView:calculate_tick(score)
return self.initial_tick / (math.floor(score / 10) + 1)
end
function TetrisView:does_collide(x, y, tetronimo, rot)
local shape = tetronimo.shape[rot]
for i = 0, 3 do
for j = 0, 3 do
local ny = y + i
local nx = x + j
if (nx >= self.grid.x or ny >= self.grid.y or nx < 0 or ny < 0 or self.cells[self.grid.x * ny + nx + 1]) and shape[i * 4 + j + 1] == 1 then
return true
end
end
end
return false
end
function TetrisView:finalize_live_piece()
assert(self.live_piece)
local shape = self.live_piece.tetronimo.shape[self.live_piece.rot]
for i = 0, 3 do
for j = 0, 3 do
local ny = self.live_piece.y + i
local nx = self.live_piece.x + j
if shape[(i * 4 + j) + 1] == 1 then
self.cells[ny * self.grid.x + nx + 1] = self.live_piece.idx
end
end
end
for y = self.live_piece.y, math.min(self.live_piece.y + 4, self.grid.y - 1) do
local all_present = true
for x = 0, self.grid.x - 1 do
if not self.cells[y * self.grid.x + x + 1] then all_present = false end
end
if all_present then
self.score = self.score + 1
self.tick = self:calculate_tick(self.score)
for ny = y, 2, -1 do
for nx = 0, self.grid.x - 1 do
self.cells[ny * self.grid.x + nx + 1] = self.cells[(ny - 1) * self.grid.x + nx + 1]
end
end
end
end
self.live_piece = nil
end
function TetrisView:step()
if not self.finished and not self.paused then
if not self.live_piece then
local idx = self.next_piece or math.floor(math.random() * #self.tetronimos) + 1
self.live_piece = { tetronimo = self.tetronimos[idx], idx = idx, x = math.floor(self.grid.x / 2), y = 0, rot = 1 }
self.next_piece = math.floor(math.random() * #self.tetronimos) + 1
if (self:does_collide(self.live_piece.x, self.live_piece.y + 1, self.live_piece.tetronimo, self.live_piece.rot)) then
self:finalize_live_piece()
self.finished = true
end
else
if (self:does_collide(self.live_piece.x, self.live_piece.y + 1, self.live_piece.tetronimo, self.live_piece.rot)) then
self.live_piece.countup = (self.live_piece.countup or 0) + 1
if self.live_piece.countup > self.lock_delay then
self:finalize_live_piece()
end
else
self.live_piece.y = self.live_piece.y + 1
self.live_piece.countup = 0
end
end
end
end
function TetrisView:draw_tetronimo(posx, posy, tetronimo, rot, color)
local shape = tetronimo.shape[rot]
for y = 0, 3 do
for x = 0, 3 do
if shape[y * 4 + x + 1] == 1 then
renderer.draw_rect(posx + x * (self.cell_size + self.cell_padding), posy + y * (self.cell_size + self.cell_padding), self.cell_size, self.cell_size, color or tetronimo.color)
end
end
end
end
function TetrisView:draw()
self:draw_background(style.background3)
local lh = style.font:get_height()
local tx = self.position.x + style.padding.x
local ty = self.position.y + style.padding.y
renderer.draw_text(style.font, "Score: " .. self.score, tx, self.position.y + style.padding.y, style.normal)
local w = renderer.draw_text(style.font, "Next Piece", tx, self.position.y + style.padding.y + lh, style.normal)
if self.next_piece then
self:draw_tetronimo(tx, self.position.y + style.padding.y + lh * 2, self.tetronimos[self.next_piece], 1)
end
if self.held_piece then
self:draw_tetronimo(w + style.padding.x, self.position.y + style.padding.y + lh * 2, self.tetronimos[self.held_piece], 1)
end
renderer.draw_text(style.font, "Held Piece", w + style.padding.x, self.position.y + style.padding.y + lh, style.normal)
ty = ty + lh * 2 + (self.cell_size + self.cell_padding) * 4 + style.padding.y
renderer.draw_rect(tx, ty, (self.cell_size + self.cell_padding) * self.grid.x, (self.cell_size + self.cell_padding) * self.grid.y, style.background)
for y = 0, self.grid.y - 1 do
for x = 0, self.grid.x - 1 do
if self.cells[y * self.grid.x + x + 1] then
local color = self.tetronimos[self.cells[y * self.grid.x + x + 1]].color
renderer.draw_rect(tx + x * (self.cell_size + self.cell_padding), ty + y * (self.cell_size + self.cell_padding), self.cell_size, self.cell_size, color)
end
end
end
if self.live_piece then
self:draw_tetronimo(tx + self.live_piece.x * (self.cell_size + self.cell_padding), ty + self.live_piece.y * (self.cell_size + self.cell_padding), self.live_piece.tetronimo, self.live_piece.rot)
if self.drop_shadow then
local y = self:get_max_drop(math.huge)
if y ~= self.live_piece.y then
self:draw_tetronimo(tx + self.live_piece.x * (self.cell_size + self.cell_padding), ty + y * (self.cell_size + self.cell_padding), self.live_piece.tetronimo, self.live_piece.rot, { self.live_piece.tetronimo.color[1], self.live_piece.tetronimo.color[2], self.live_piece.tetronimo.color[3], 50 })
end
end
end
if self.finished or self.paused then renderer.draw_rect(tx, ty, self.grid.x * (self.cell_size + self.cell_padding), self.grid.y * (self.cell_size + self.cell_padding), { common.color "rgba(255, 255, 255, 0.5)" }) end
if self.finished then common.draw_text(style.font, style.error, "GAME OVER", "center", tx, ty, self.grid.x * (self.cell_size + self.cell_padding), self.grid.y * (self.cell_size + self.cell_padding)) end
if self.paused then common.draw_text(style.font, style.warn, "PAUSED", "center", tx, ty, self.grid.x * (self.cell_size + self.cell_padding), self.grid.y * (self.cell_size + self.cell_padding)) end
end
function TetrisView:rotate()
if self.live_piece and not self.paused then
local new_rot = (self.live_piece.rot % #self.live_piece.tetronimo.shape) + 1
if not self:does_collide(self.live_piece.x, self.live_piece.y, self.live_piece.tetronimo, new_rot) then
self.live_piece.rot = new_rot
end
end
end
function TetrisView:hold()
if self.live_piece and not self.paused then
if self.held_piece then
if not self:does_collide(self.live_piece.x, self.live_piece.y, self.tetronimos[self.held_piece], 1) then
local live_piece = self.live_piece.idx
self.live_piece = { x = self.live_piece.x, y = self.live_piece.y, rot = 1, idx = self.held_piece, tetronimo = self.tetronimos[self.held_piece] }
self.held_piece = live_piece
end
else
self.held_piece = self.live_piece.idx
self.live_piece = nil
end
end
end
function TetrisView:get_max_drop(amount)
if self.live_piece then
for y = self.live_piece.y, math.min(self.grid.y, self.live_piece.y + amount) do
if self:does_collide(self.live_piece.x, y + 1, self.live_piece.tetronimo, self.live_piece.rot) then
return y, true
end
end
end
return self.live_piece.y + amount, false
end
function TetrisView:drop(amount)
if self.live_piece and not self.paused then
local y, collides = self:get_max_drop(amount)
self.live_piece.y = y
if collides then
self:finalize_live_piece()
end
end
end
function TetrisView:shift(delta)
if self.live_piece and not self.paused and not self:does_collide(self.live_piece.x + delta, self.live_piece.y, self.live_piece.tetronimo, self.live_piece.rot) then
self.live_piece.x = self.live_piece.x + delta
end
end
command.add(TetrisView, {
["tetris:rotate"] = function() core.active_view:rotate() end,
["tetris:shift-left"] = function() core.active_view:shift(-1) end,
["tetris:shift-right"] = function() core.active_view:shift(1) end,
["tetris:drop"] = function() core.active_view:drop(config.plugins.tetris.down_amount) end,
["tetris:hard-drop"] = function() core.active_view:drop(math.huge) end,
["tetris:hold"] = function() core.active_view:hold() end,
["tetris:toggle-pause"] = function() core.active_view.paused = not core.active_view.paused end,
["tetris:quit"] = function()
core.active_view.finished = true
core.active_view.node:close_view(core.root_view.root_node, core.active_view)
end
})
command.add(nil, {
["tetris:start"] = function()
local view = TetrisView(config.plugins.tetris)
local node = core.root_view:get_active_node()
view.node = node:split("right", view, { x = true }, false)
core.set_active_view(view)
end
})
keymap.add {
["up"] = "tetris:rotate",
["left"] = "tetris:shift-left",
["right"] = "tetris:shift-right",
["down"] = "tetris:drop",
["space"] = "tetris:hard-drop",
["tab"] = "tetris:hold",
["escape"] = "tetris:quit",
["ctrl+e"] = { "tetris:quit", "tetris:start" },
["p"] = "tetris:toggle-pause"
}
return { view = TetrisView }
|