-
Notifications
You must be signed in to change notification settings - Fork 1
/
_g_tetris.py
423 lines (379 loc) · 15.4 KB
/
_g_tetris.py
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
# tetris.py
#
# ESP8266 (node MCU D1 mini) micropython game for Tetris
# by Billy Cheung 2019 08 31
#
# SPI OLED
# GND
# VCC
# D0/Sck - D5 (=GPIO14=HSCLK)
# D1/MOSI- D7 (=GPIO13=HMOSI)
# RES - D0 (=GPIO16)
# DC - D4 (=GPIO2)
# CS - D3 (=GPIO0)
# Speaker
# GPIO15 D8 Speaker
# n.c. - D6 (=GPIO13=HMOSI)
#
# GPIO5 D1—— On to read ADC for Btn
# GPIO4 D2—— On to read ADC for Paddle
#
# buttons A0
# A0 VCC-9K-U-9K-L-12K-R-9K-D-9K-A-12K-B-9K-GND
#
import gc
import sys
gc.collect()
print ("heap: %dkB" % gc.mem_free())
import utime
from utime import sleep_ms
# all dislplay, buttons, paddle, sound logics are in gameESP.mpy module
from gameESP import *
g=gameESP(rotation=0)
g.frameRate = 30
g.bgm = 3
g.maxBgm = 3
bgmBuf= [
[g.songStart, False, 1, g.songEnd],
# Empire Strikes Back
[ g.songStart,True, 100, 0, 4,
'g3',1,0,1,'g3',1,0,1,'g3',1,0,1,'c4',8,'g4',8,0,4,'f4',2,'e4',2,'d4',2,'c5',8,'g4',8, 0,4, 'f4',2,'e4',2,'d4',2,'c5',8,'g4',8,0,4,'f4',2,'e4',2,'f4',2,'d4',8,0,8,
'g3',1,0,1,'g3',1,0,1,'g3',1,0,1,'c4',8,'g4',8,0,4,'f4',2,'e4',2,'d4',2,'c5',8,'g4',8, 0,4, 'f4',2,'e4',2,'d4',2,'c5',8,'g4',8,0,4,'f4',2,'e4',2,'f4',2,'d4',8,0,8,
'g3',1,0,1,'g3',1,0,1,'a3',4,0,4,'f4',2,'e4',2,'d4',2,'c4',1,0,1,'c4',2,'d4',1,'e4',1,'d4',2,'a3',2,'b3',4,
'g3',1,0,1,'g3',1,0,1,'a3',4,0,4,'f4',2,'e4',2,'d4',2,'c4',1,0,1,'g4',2,0,1,'d4',1,'d4',4,0,4,
'g3',1,0,1,'g3',1,0,1,'a3',4,0,4,'f4',2,'e4',2,'d4',2,'c4',1,0,1,'c4',2,'d4',1,'e4',1,'d4',2,'a3',2,'b3',4,
'e4',1,0,1,'e4',2,'a4',2,'g4',2,'f4',2,'e4',2,'d4',2,'c4',2,'b3',2,'a3',2,'e4',8, 0, 8,
g.songLoop],
# The Imperial March
[ g.songStart,False, 1, 0, 400,
440, 400, 0, 100, 440, 400, 0, 100, 440, 400, 0,100, 349, 350, 523, 150, 440, 500, 349, 350, 523, 150, 440, 650, 0,500, 659, 500, 659, 500, 659, 500, 698, 350, 523, 150, 415, 500, 349, 350, 523, 150, 440, 650, 0, 500,
880, 500, 440, 300, 440, 150, 880, 500, 830, 325, 784, 175, 740, 125, 698, 125, 740, 250, 0, 325, 445, 250, 622, 500, 587, 325, 554, 175, 523, 125, 466, 125, 523, 250, 0, 350,
349, 250, 415, 500, 349, 350, 440, 125, 523, 500, 440, 375, 523, 125, 659, 650, 0, 500,349, 250, 415, 500, 349, 375, 523, 125, 440, 500, 349, 375, 523, 125, 440, 650,0, 650,
880, 500, 440, 300, 440, 150, 880, 500, 830, 325, 784, 175, 740, 125, 698, 125, 740, 250, 0, 325, 445, 250, 622, 500, 587, 325, 554, 175, 523, 125, 466, 125, 523, 250, 0, 350,
349, 250, 415, 500, 349, 350, 440, 125, 523, 500, 440, 375, 523, 125, 659, 650, 0, 500,349, 250, 415, 500, 349, 375, 523, 125, 440, 500, 349, 375, 523, 125, 440, 650,0, 650,
g.songLoop],
# Tetris
[ g.songStart,False,200, 0, 4,
659,2, 494, 1, 523,1, 587,2, 523, 1, 493, 1, 440, 2, 440, 1, 523,1, 659,2,587,1,523,1,493,2, 493,1,523,1,587,2,659,2,523,2,440,2,440,2,0,2,587, 1,698,1,880,2,783,1,698,1,659,2,523,1,659,2,587,1,523,1,493,2,493,1,523,1,587,2,659,2,523,2,440,2,440,2,0,2,
329,4,261,4,293,4,246,4,261,4,220,4,207,4,246,4,329,4,261,4,293,4,246,4,261,2,329,2,440,4,415,6,0,2,
g.songLoop]
]
size = width, height = 80, 160
color ={'black': st7789.BLACK, 'white': st7789.WHITE}
sqrsize = 8
occupied_squares = []
top_of_screen = (2, 24)
top_x, top_y = top_of_screen[0], top_of_screen[1]
num_block = 4
pen_size = 1
mov_delay, r_delay = 200, 50
board_centre = int(width/2)+2
no_move = 0
score = 0
life = 0
shape_blcks = []
shape_name = ""
new_shape_blcks = []
new_shape_name = ""
occupied_squares = []
def reset_board():
global shape_blcks, shape_name, occupied_squares
shape_blcks = []
shape_name = ""
occupied_squares = []
g.display.fill(0)
g.display.rect(top_x-1, top_y-1, width+2, height+2,st7789.WHITE)
def drawScore () :
global score, life
g.display.text(font, 'S:{}'.format (score), 8, 0,st7789.YELLOW)
g.display.text(font, 'L:{}'.format (life), 48, 0,st7789.YELLOW)
def draw_shape():
'''this draws list of blocks or a block to the background and blits
background to screen'''
if isinstance(shape_blcks,list):
for blck in shape_blcks:
g.display.rect(blck[0], blck[1], sqrsize, sqrsize, st7789.WHITE)
else:
g.display.rect(shape_blcks[0], shape_blcks[1], sqrsize, sqrsize, st7789.WHITE)
def row_filled(row_no):
global occupied_squares
'''check if a row is fully occupied by a shape block'''
for x_coord in range(top_x, width+top_x, sqrsize):
if (x_coord, row_no) in occupied_squares:
continue
else:
return False
return True
def delete_row(row_no):
'''removes all squares on a row from the occupied_squares list and then
moves all square positions which have y-axis coord less than row_no down
board'''
global occupied_squares
g.display.fill(0)
g.display.rect(top_x-1, top_y-1, width+2, height+2,st7789.WHITE)
new_buffer = []
x_coord, y_coord = 0, 1
for sqr in occupied_squares:
if sqr[y_coord] != row_no:
new_buffer.append(sqr)
occupied_squares = new_buffer
for index in range(len(occupied_squares)):
if occupied_squares[index][y_coord] < row_no:
occupied_squares[index] = (occupied_squares[index][x_coord],
occupied_squares[index][y_coord] + sqrsize)
for sqr in occupied_squares:
g.display.rect(sqr[x_coord], sqr[y_coord], sqrsize, sqrsize, st7789.WHITE)
def move(direction):
global shape_blcks
'''input:- list of blocks making up a tetris shape
output:- list of blocks making up a tetris shape
function moves the input list of blocks that make up shape and then checks
that the list of blocks are all in positions that are valide. position is
valid if it has not been occupied previously and is within the tetris board.
If move is successful, function returns the moved shape and if move is not
possible, function returns a false'''
directs = {'down':(no_move, sqrsize), 'left':(-sqrsize, no_move),
'right':(sqrsize, no_move), 'pause': (no_move, no_move)}
delta_x, delta_y = directs[direction]
for index in range(num_block):
shape_blcks[index] = [shape_blcks[index][0] + delta_x, shape_blcks[index][1]+ delta_y]
if legal(shape_blcks):
for index in range(num_block):
# erase previous positions of block
g.display.fill_rect(shape_blcks[index][0]-delta_x, shape_blcks[index][1]-delta_y, sqrsize, sqrsize, st7789.BLACK)
return True
else:
# undo the move, as it's not legal (being blocked by existing blocks)
for index in range(num_block):
shape_blcks[index] = [shape_blcks[index][0]-delta_x, shape_blcks[index][1]- delta_y]
return False
def legal(blcks):
'''input: list of shape blocks
checks whether a shape is in a legal portion of the board as defined in the
doc of 'move' function'''
for index in range(num_block):
new_x = blcks[index][0]
new_y = blcks[index][1]
if (((new_x, new_y) in occupied_squares or new_y >= height) or
(new_x >= width or new_x < top_x)):
return False
return True
def create_newshape(start_x=board_centre, start_y=32):
'''A shape is a list of four rectangular blocks.
Input:- coordinates of board at which shape is to be created
Output:- a list of the list of the coordinates of constituent blocks of each
shape relative to a reference block and shape name. Reference block has
starting coordinates of start_x and start_y. '''
global shape_blcks, shape_name, new_shape_blcks, new_shape_name
shape_blcks = new_shape_blcks
shape_name = new_shape_name
shape_names = ['S', 'O', 'I', 'L', 'T']
shapes = {'S':[(start_x + 1*sqrsize, start_y + 2*sqrsize),
(start_x, start_y), (start_x, start_y + 1*sqrsize),(start_x + 1*sqrsize,
start_y + 1*sqrsize)],
'O':[(start_x + 1*sqrsize, start_y + 1*sqrsize), (start_x, start_y),
(start_x, start_y + 1*sqrsize), (start_x + 1*sqrsize, start_y)],
'I':[(start_x, start_y + 3*sqrsize), (start_x, start_y),
(start_x, start_y + 2*sqrsize), (start_x, start_y + 1*sqrsize)],
'L':[(start_x + 1*sqrsize, start_y + 2*sqrsize), (start_x, start_y),
(start_x, start_y + 2*sqrsize), (start_x, start_y + 1*sqrsize)],
'T':[(start_x + 1*sqrsize, start_y + 1*sqrsize),(start_x, start_y),
(start_x - 1*sqrsize, start_y + 1*sqrsize),(start_x,
start_y + 1*sqrsize)]
}
a_shape = g.random(0, 4)
new_shape_blcks = shapes[shape_names[a_shape]]
new_shape_name = shape_names[a_shape]
g.display.fill_rect(82, top_y+32, 60, 40, st7789.BLACK)
if isinstance(new_shape_blcks, list):
for blck in new_shape_blcks:
g.display.rect(blck[0]+60, blck[1]+32, sqrsize, sqrsize, st7789.YELLOW)
else:
g.display.rect(new_shape_blcks[0+40], new_shape_blcks[1]+15, sqrsize, sqrsize, st7789.WHITE)
def rotate():
'''input:- list of shape blocks
ouput:- list of shape blocks
function tries to rotate ie change orientation of blocks in the shape
and this applied depending on the shape for example if a 'O' shape is passed
to this function, the same shape is returned because the orientation of such
shape cannot be changed according to tetris rules'''
if shape_name == 'O':
return shape_blcks
else:
#global no_move, occupied_squares, background
ref_shape_ind = 3 # index of block along which shape is rotated
start_x, start_y = (shape_blcks[ref_shape_ind][0],
shape_blcks[ref_shape_ind][1])
save_blcks = shape_blcks
Rshape_blcks = [(start_x + start_y-shape_blcks[0][1],
start_y - (start_x - shape_blcks[0][0])),
(start_x + start_y-shape_blcks[1][1],
start_y - (start_x - shape_blcks[1][0])),
(start_x + start_y-shape_blcks[2][1],
start_y - (start_x - shape_blcks[2][0])),
(shape_blcks[3][0], shape_blcks[3][1])]
if legal(Rshape_blcks):
for index in range(num_block): # erase the previous shape
g.display.fill_rect(shape_blcks[index][0], shape_blcks[index][1],sqrsize, sqrsize, st7789.BLACK)
return Rshape_blcks
else:
return shape_blcks
exitGame = False
demo = False
while not exitGame:
g.startSong(bgmBuf[g.bgm])
#menu screen
while True:
# g.display.fill(0)
g.display.text(font, 'TETRIS', 0, 0, st7789.WHITE)
g.display.rect(100,80, g.max_vol*4+2,6,st7789.WHITE)
g.display.fill_rect(101,81, g.vol * 4,4,st7789.WHITE)
g.display.text(font, 'A Start', 10, 16, st7789.WHITE)
if demo :
g.display.text(font, ' AI-Player', 10,32, st7789.WHITE)
else :
g.display.text(font, ' 1-Player', 10,32, st7789.WHITE)
g.display.text(font, ' Frame/s {}'.format(g.frameRate), 10,48, st7789.WHITE)
if g.bgm :
g.display.text(font, 'B Music {}'.format(g.bgm), 10, 64, st7789.WHITE)
else :
g.display.text(font, 'B Music Off', 10, 64, st7789.WHITE)
g.display.text(font, 'Sound FX', 10, 80, st7789.WHITE)
# g.display.show()
g.getBtn()
# if g.setVol() :
# pass
# elif g.setFrameRate() :
# pass
# elif g.pressed(g.btnB) and g.justPressed(g.btnL) :
# exitGame = True
# gameOver= True
# break
if g.justPressed(g.btnA) :
if demo :
g.display.fill(0)
g.display.text(font, 'DEMO', 5, 0, st7789.WHITE)
g.display.text(font, 'B to Stop', 5, 30, st7789.WHITE)
# g.display.show()
sleep_ms(1000)
break
# elif g.justPressed(g.btnD) :
# demo = not demo
elif g.justPressed(g.btnB) :
g.bgm = 0 if g.bgm >= g.maxBgm else g.bgm + 1
if g.bgm :
g.startSong(bgmBuf[g.bgm])
else :
g.stopSong()
sleep_ms(10)
life = 3
Score = 0
reset_board()
create_newshape()
gameOver = False
# game loop
while not gameOver:
drawScore()
create_newshape()
extramoves = 3
l_of_blcks_ind = blck_x_axis = 0
shape_name_ind = blck_y_axis = 1
move_dir = 'down' #default move direction
game = 'playing' #default game state play:- is game paused or playing
if legal(shape_blcks):
draw_shape()
else:
life -= 1
if life <= 0 :
gameOver = True
break
else :
g.playTone('g4', 100)
g.playTone('e4', 100)
g.playTone('c4', 100)
# g.display.show()
sleep_ms(2000)
reset_board()
g.bgm = 0 if g.bgm >= g.maxBgm else g.bgm + 1
if g.bgm :
g.startSong(bgmBuf[g.bgm])
continue
while True:
mov_delay = 150
move_dir = 'down'
g.getBtn()
# if game == 'paused':
# if g.justPressed(g.btnB) :
# g.playTone('c4', 100)
# g.playTone('e4', 100)
# game = 'playing'
# else:
# if g.justPressed(g.btnB) :
# g.playTone('e4', 100)
# g.playTone('f4', 100)
# game = 'paused'
# move_dir = 'pause'
# elif g.pressed(g.btnU) and g.pressed(g.btnD) :
# gameOver = True
# break
# if g.pressed(g.btnD) :
# mov_delay = 10
# move_dir = 'down'
# if g.justPressed(g.btnA | g.btnB) :
# shape_blcks = rotate()
# draw_shape()
# # g.display.show()
# sleep_ms(r_delay)
# continue
if g.pressed(g.btnA) and g.pressed(g.btnB):
shape_blcks = rotate()
draw_shape()
sleep_ms(r_delay)
continue
elif g.justPressed(g.btnA | g.btnB):
move_dir = 'left' if g.pressed(g.btnA) else 'right'
mov_delay = 50
move (move_dir)
draw_shape()
# g.display.show()
sleep_ms(mov_delay)
continue
moved = move( move_dir)
draw_shape()
sleep_ms(mov_delay)
'''if block did not move and the direction for movement is down
then shape has come to rest so we can exit loop and then a new
shape is generated. if direction for movement is sideways and
block did not move it should be moved down rather'''
if not moved and move_dir == 'down':
extramoves = extramoves - 1
if extramoves <= 0 :
for block in shape_blcks:
occupied_squares.append((block[0],block[1]))
break
draw_shape()
# g.display.show()
for row_no in range (height - sqrsize + top_y, 0, -sqrsize):
if row_filled(row_no):
delete_row(row_no)
score+=10
drawScore()
# g.display.show()
g.playTone('c4', 100)
g.playTone('e4', 100)
g.playTone('g4', 100)
g.playTone('e4', 100)
g.playTone('c4', 100)
# g.display.show()
if gameOver :
g.display.fill_rect(20, 120, 90, 35, st7789.RED)
g.display.text(font, "Game Over", 30, 130, st7789.BLUE, st7789.RED)
# g.display.show()
g.playTone('c4', 100)
g.playTone('e4', 100)
g.playTone('g4', 100)
sleep_ms(2000)
if g.ESP32 :
g.deinit()
del sys.modules["gameESP"]
gc.collect()