-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.moon
370 lines (292 loc) · 6.82 KB
/
main.moon
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
export aroma = love if love
import rectangle, setColor, getColor from aroma.graphics
import insert from table
_print = aroma.graphics.print
class Piece
rot: {2, 2} -- rotation origin
new: (@board, @ox=0, @oy=0, @pts=nil) =>
error "need shape" if not @shape
if @pts == nil
@pts = {}
for y=1,#@shape
for x=1,#@shape[y]
if @shape[y][x] > 0
insert @pts, {x, y}
@calc_shadow!
calc_shadow: =>
return if @is_shadow
@shadow = with self.__class @board, @ox, @oy, [pt for pt in *@pts]
.is_shadow = true
while @shadow\try_move 0, 1 do nil
nil
-- clockwise unless flipped
rotate: (flip=false)=>
rot_x, rot_y = unpack @rot
@pts = for pt in *@pts
x = pt[1] - rot_x
y = pt[2] - rot_y
x,y = if flip
y, -x
else
-y, x
{x + rot_x, y + rot_y}
each_pt: =>
coroutine.wrap ->
for pt in *@pts
x, y = pt[1] + @ox, pt[2] + @oy
coroutine.yield x,y
try_rotate: =>
@rotate!
if @collides!
@rotate true
else
@calc_shadow!
try_move: (dx, dy) =>
@ox += dx
@oy += dy
if @collides!
@ox -= dx
@oy -= dy
false
else
@calc_shadow!
true
collides: =>
for x, y in @each_pt!
return true if x < 1 or x > @board.width or y < 1 or y > @board.height
return true if @board.grid[x][y]
false
draw: =>
if @shadow
setColor 64,64,64
for x,y in @shadow\each_pt!
@board\draw_cell x, y
setColor @color
for x, y in @each_pt!
@board\draw_cell x, y
class Ell extends Piece
rot: {1, 2}
color: {250, 94, 20}
shape: {
{1,1}
{1,0}
{1,0}
}
class Seven extends Piece
color: {229, 59, 116}
shape: {
{1,1}
{0,1}
{0,1}
}
class Tee extends Piece
color: {196, 229, 59}
shape: {
{0,1,0}
{1,1,1}
}
class Ess extends Piece
color: {59, 229, 122}
shape: {
{0,1,1}
{1,1,0}
}
class Zee extends Piece
color: {229, 83, 59}
shape: {
{1,1,0}
{0,1,1}
}
class Oo extends Piece
color: {229, 219, 59}
shape: {
{1,1}
{1,1}
}
class Ii extends Piece
rot: {1, 2}
color: {59, 107, 229}
shape: {
{1}
{1}
{1}
{1}
}
class Board
cell_size: 14
padding: 1
ox: 40
oy: 40
color: {200, 200, 200}
new: (@width, @height) =>
@grid = {}
for x=1,@width
col = {}
for y=1,@height
col[y] = false
@grid[x] = col
@center!
s = (@cell_size + @padding)
@real_width = @width * s
@real_height = @height * s
center: =>
-- center in window
size = @cell_size + @padding
mx, my = @width * size + 2, @height * size + 2
@ox = (aroma.graphics.getWidth! - mx) / 2
@oy = (aroma.graphics.getHeight! - my) / 2
-- returns the number of rows cleared
set_piece: (piece) =>
to_check = {}
for x,y in piece\each_pt!
to_check[y] = true
@set x,y
print "** setting piece"
to_check = [y for y in pairs to_check]
table.sort to_check
count = 0
for y in *to_check
count += 1 if @check_row y
count
-- check if a row is completed
check_row: (y) =>
print "checking", y
for i=1,@width
return false if not @grid[i][y]
print "got row"
for yy=y,1,-1
for xx=1,@width
if yy < 1
@grid[xx][yy] = false
else
@grid[xx][yy] = @grid[xx][yy - 1]
true
set: (x,y) =>
@grid[x][y] = true
draw_cell: (x, y, color) =>
setColor color if color
size = @cell_size + @padding
rectangle "fill",
@ox + (x - 1)*size, @oy + (y - 1)*size,
@cell_size, @cell_size
draw: =>
size = @cell_size + @padding
setColor 255,255,255
rectangle "line",
@ox - 1, @oy - 1,
@width * size + 2, @height * size + 2
setColor @color
for y=1,@height
for x=1,@width
val = @grid[x][y]
if val then @draw_cell x, y
bind_state = (state) ->
for ev in *{"update", "draw", "keypressed", "focus"}
aroma[ev] = state[ev] and (...) -> state[ev] state, ...
key_repeater = (key, initial=0.2, sustain=0.05) ->
time = 0
sustaining = false
(dt) ->
if aroma.keyboard.isDown key
if time == 0
with true
time += dt
else
time += dt
dt = if sustaining then sustain else initial
if time >= dt
time -= dt
sustaining = true
true
else
false
else
time = 0
sustaining = false
false
Game = nil
class GameOver
new: (@game) =>
draw: =>
@game\draw!
setColor 0,0,0, 128
rectangle "fill", 0, 0, aroma.graphics.getWidth!, aroma.graphics.getHeight!
setColor 255,255,255
_print "Game over! - Enter to play again", 10, 10
keypressed: (key) =>
os.exit! if key == "escape" and love
if key == "return"
bind_state Game!
class Paused
new: (@game) =>
focus: (focus) =>
bind_state @game if focus
keypressed: (key) =>
@focus true if key == "return"
draw: =>
setColor 255,255,255
_print "Click to play", 10, 10
@game\draw!
class Game
pieces: {
Ell, Seven, Tee, Ess, Zee, Oo, Ii
}
new: =>
@speed = 0.4
@board = Board 10, 22
@score = 0
@keys = {name, key_repeater(name) for name in *{
"left", "right", "up", "down", " "
}}
-- called from key repeaters
handle_key: (name) =>
if @current_piece
switch name
when "left"
@current_piece\try_move -1, 0
when "right"
@current_piece\try_move 1, 0
when "down"
@push_down!
when "up"
@current_piece\try_rotate!
when " "
while @push_down! do nil
nil
keypressed: (key, code) =>
bind_state GameOver self if key == "q"
os.exit! if key == "escape"
push_down: =>
return false if not @current_piece
if not @current_piece\try_move 0, 1
count = @board\set_piece @current_piece
@speed -= 0.01 if count > 0
@speed = math.max @speed, 0.05
@score += count*count*50
@current_piece = nil
false
else true
update: (dt) =>
if not @current_piece
cls = @pieces[math.random 1, #@pieces]
@current_piece = cls @board,
cls.rot[1] + math.floor(@board.width / 3), 0
-- game over if it immediately collides
if @current_piece\collides!
return bind_state GameOver self
@last_tick = 0
-- check for input
for key, fn in pairs @keys
@handle_key key if fn dt
if @last_tick > @speed
@last_tick -= @speed
@push_down!
@last_tick += dt
draw: =>
@board\draw!
@current_piece\draw! if @current_piece
setColor 200, 200, 200
-- _print "rate: " .. @speed, @board.ox, @board.oy - 18
_print "score: " .. @score, @board.ox, @board.oy + @board.real_height + 2
aroma.load = ->
bind_state Paused Game!