forked from YouMakeTech/Pico-GB-MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.py
358 lines (296 loc) · 16.1 KB
/
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
# tetris.py by Vincent Mistler (YouMakeTech)
# Tetris game for the Raspberry Pi Pico-GB
from micropython import const
from PicoGB import PicoGB
import time
from random import randint
BLOCK_SIZE = const(12) # Size of a single tetromino block in pixels
GRID_OFFSET = const(2)
GRID_ROWS = const(15)
GRID_COLS = const(8)
pgb = PicoGB()
# image definitions 12x12 pixels
tetris_wall=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00mXmXmX3\x91\x00\x00\x00\x00mXmXmX3\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00mX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9emX\x00\x00\x00\x00\xf7\x9e\xf7\x9e\xf7\x9e3\x91\x00\x00\x00\x00mXmXmX3\x91\x00\x00\x00\x00mXmXmX3\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x913\x91\x00\x00\x00\x00mX3\x913\x91')
bottom_border=bytearray(b'\xeeP\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xf6\x90\xeeP\xf6p\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xf6p\xdd\x0f\xe5P\xe5P\xe5P\xe5P\xe5P\xe5P\xe5P\xe5P\xe5P\xe5P\xdd\x0fI\x84I\x84I\x84I\x84I\x84I\x84I\x84I\x84I\x84I\x84I\x84I\x84@\xc3H\xc4H\xc4H\xc4H\xc4H\xc4H\xc4H\xc4H\xc4H\xc4H\xc4@\xc3\xd3\r\xdb.\xdb.\xdb.\xdb.\xdb.\xdb.\xdb.\xdb.\xdb.\xdb.\xd3\r\xe4\xef\xed0\xed0\xed0\xed0\xed0\xed0\xed0\xed0\xed0\xed0\xe4\xef\xe5P\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xe5P\xf6p\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xfe\xb1\xf6p\xe5P\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xf5p\xe5P\xdc\xef\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xe5\x0f\xdc\xefI\x84I\xa4I\xa4I\xa4I\xa4I\xa4I\xa4I\xa4I\xa4I\xa4I\xa4I\x84')
corner=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00mX\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffmX\x00\x00\x00\x00mXmX\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffmXmX\x00\x00\x00\x00mXmXmX\xff\xff\xff\xff\xff\xff\xff\xffmXmXmX\x00\x00\x00\x00mXmXmXmX\xff\xff\xff\xffmXmXmXmX\x00\x00\x00\x00mXmXmXmX3\x913\x91mXmXmXmX\x00\x00\x00\x00mXmXmX3\x913\x913\x913\x91mXmXmX\x00\x00\x00\x00mXmX3\x913\x913\x913\x913\x913\x91mXmX\x00\x00\x00\x00mX3\x913\x913\x913\x913\x913\x913\x913\x91mX\x00\x00\x00\x003\x913\x913\x913\x913\x913\x913\x913\x913\x913\x91\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
left_border=bytearray(b'\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\xe3\x0e\x00\x00\x00\x00\xedP\xedP\xedP\x00\x00\xedP\xedP\xfe\xd1\xedP\xedP\xe3\x0e\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xedP\xedP\xfe\xd1\xedP\xedP\xe3\x0e\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xedP\xedP\xfe\xd1\xedP\xedP\xe3\x0e\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xedP\xedP\xfe\xd1\xedP\xedP\xe3\x0e\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xedP\xedP\xfe\xd1\xedP\xedP\xe3\x0e\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\x00\x00\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1')
right_border=bytearray(b'\xedP\xedP\xedP\x00\x00\x00\x00\xe3\x0e\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xe3\x0e\xedP\xedP\xfe\xd1\xedP\xedP\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xe3\x0e\xedP\xedP\xfe\xd1\xedP\xedP\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xe3\x0e\xedP\xedP\xfe\xd1\xedP\xedP\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xe3\x0e\xedP\xedP\xfe\xd1\xedP\xedP\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xe3\x0e\xedP\xedP\xfe\xd1\xedP\xedP\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\x00\x00\xe3\x0e\xe3\x0e\xedP\xe3\x0e\xe3\x0e\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\x00\x00\xfe\xd1\xfe\xd1\xedP\x00\x00\x00\x00\xedP\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\x00\x00')
top_border=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\xe3\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xedP\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1\xfe\xd1')
pgb.add_sprite(tetris_wall,12,12) #0
pgb.add_sprite(bottom_border,12,12) #1
pgb.add_sprite(corner,12,12) #2
pgb.add_sprite(left_border,12,12) #3
pgb.add_sprite(right_border,12,12) #4
pgb.add_sprite(top_border,12,12) #5
field = [[-1 for col in range(GRID_COLS)] for row in range(GRID_ROWS)]
# shape of the 7 tetrominos
# [0][1]
# [2][3]
# [4][5]
# [6][7]
# e.g. [3,4,5,7] is:
# [ ]
# [ ][ ]
# [ ]
tetrominos = [[1,3,5,7],
[2,4,5,7],
[3,5,4,6],
[3,5,4,7],
[2,3,5,7],
[3,5,7,6],
[2,3,4,5]]
# Game Boy Color Tetrominos colors
tetrominos_colors =[PicoGB.color(239,146,132),
PicoGB.color(222,146,239),
PicoGB.color(239,170,132),
PicoGB.color(165,211,132),
PicoGB.color(99,219,222),
PicoGB.color(231,97,115),
PicoGB.color(0,0,0)]
# Color scheme
BLACK = PicoGB.color(0,0,0)
WHITE = PicoGB.color(255,255,255)
GRID_BACKGROUND_COLOR = PicoGB.color(255,211,132)
BACKGROUND_COLOR = PicoGB.color(99,154,132)
BACKGROUND_COLOR2 = PicoGB.color(57,89,41)
TEXT_COLOR = BLACK
TEXT_BACKGROUND_COLOR = WHITE
lines = 0
level = 0
score = 0
last_button="NONE"
has_rotated=False
now = time.ticks_ms()
n = randint(0, 6)
next_n = randint(0, 6)
x=[0,0,0,0]
y=[0,0,0,0]
prev_x=[0,0,0,0]
prev_y=[0,0,0,0]
for i in range(0,4):
x[i]=(tetrominos[n][i]) % 2;
y[i]=int(tetrominos[n][i] / 2);
x[i]+=int(GRID_COLS/2)
def collision(x,y):
for i in range(4):
# check collision against the border
if x[i]<0 or x[i]>=GRID_COLS or y[i]>=GRID_ROWS:
return True
# check collision against another triomino
if field[y[i]][x[i]]>=0:
return True
return False
def title_screen():
# title screen
now = time.ticks_ms()
while pgb.any_button()==False:
pgb.load_image("tetris_title.bin")
pgb.show()
if time.ticks_diff(time.ticks_ms(), now) > 200:
now = time.ticks_ms()
pgb.center_text("PRESS ANY BUTTON",WHITE)
pgb.show()
while time.ticks_diff(time.ticks_ms(), now) < 200:
time.sleep(0.020)
now = time.ticks_ms()
def game_over_screen():
pgb.fill(BLACK)
pgb.center_text("GAME OVER",WHITE)
pgb.show()
while True:
time.sleep(0.500)
def draw_background():
pgb.fill(BACKGROUND_COLOR)
for i in range(0,int(240/BLOCK_SIZE),2):
for j in range(0,int(240/BLOCK_SIZE),2):
pgb.fill_rect(j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE,BACKGROUND_COLOR2)
pgb.fill_rect((j+1)*BLOCK_SIZE,(i+1)*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE,BACKGROUND_COLOR2)
pgb.fill_rect(GRID_OFFSET*BLOCK_SIZE,0,
GRID_COLS*BLOCK_SIZE,GRID_ROWS*BLOCK_SIZE,
GRID_BACKGROUND_COLOR)
# add walls
for i in range(GRID_ROWS):
pgb.sprite(0,(GRID_OFFSET-1)*BLOCK_SIZE,i*BLOCK_SIZE)
pgb.sprite(0,(GRID_OFFSET+GRID_COLS)*BLOCK_SIZE,i*BLOCK_SIZE)
# draw text (LINES)
pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,16*BLOCK_SIZE,
BLOCK_SIZE*7,BLOCK_SIZE*2,
TEXT_BACKGROUND_COLOR)
pgb.text("LINES",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,16*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % lines,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,17*BLOCK_SIZE+1,TEXT_COLOR)
# draw text (LEVEL)
pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,13*BLOCK_SIZE,
BLOCK_SIZE*7,BLOCK_SIZE*2,
TEXT_BACKGROUND_COLOR)
pgb.text("LEVEL",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,13*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % level,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,14*BLOCK_SIZE+1,TEXT_COLOR)
# draw text (SCORE)
pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,10*BLOCK_SIZE,
BLOCK_SIZE*7,BLOCK_SIZE*2,
TEXT_BACKGROUND_COLOR)
pgb.text("SCORE",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,10*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % score,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,11*BLOCK_SIZE+1,TEXT_COLOR)
# next tetromino box
pgb.fill_rect((GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE,2*BLOCK_SIZE,
BLOCK_SIZE*6 ,BLOCK_SIZE*7,TEXT_BACKGROUND_COLOR)
pgb.sprite(2,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE,2*BLOCK_SIZE) #upper left corner
pgb.sprite(5,(GRID_OFFSET+GRID_COLS+3)*BLOCK_SIZE,2*BLOCK_SIZE) #top border
pgb.sprite(5,(GRID_OFFSET+GRID_COLS+4)*BLOCK_SIZE,2*BLOCK_SIZE) #
pgb.sprite(5,(GRID_OFFSET+GRID_COLS+5)*BLOCK_SIZE,2*BLOCK_SIZE) #
pgb.sprite(5,(GRID_OFFSET+GRID_COLS+6)*BLOCK_SIZE,2*BLOCK_SIZE) #
pgb.sprite(2,(GRID_OFFSET+GRID_COLS+7)*BLOCK_SIZE,2*BLOCK_SIZE) #upper right corner
pgb.sprite(2,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE,8*BLOCK_SIZE) #lower left corner
pgb.sprite(1,(GRID_OFFSET+GRID_COLS+3)*BLOCK_SIZE,8*BLOCK_SIZE) #lower border
pgb.sprite(1,(GRID_OFFSET+GRID_COLS+4)*BLOCK_SIZE,8*BLOCK_SIZE) #
pgb.sprite(1,(GRID_OFFSET+GRID_COLS+5)*BLOCK_SIZE,8*BLOCK_SIZE) #
pgb.sprite(1,(GRID_OFFSET+GRID_COLS+6)*BLOCK_SIZE,8*BLOCK_SIZE) #
pgb.sprite(2,(GRID_OFFSET+GRID_COLS+7)*BLOCK_SIZE,8*BLOCK_SIZE) #lower right corner
for k in range(3,8):
pgb.sprite(3,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE,k*BLOCK_SIZE) #left border
pgb.sprite(4,(GRID_OFFSET+GRID_COLS+7)*BLOCK_SIZE,k*BLOCK_SIZE) #right border
for i in range(4):
draw_block((GRID_OFFSET+GRID_COLS+2)+tetrominos[next_n][i] % 2,
3+int(tetrominos[next_n][i] / 2), next_n)
def draw_block(j,i,n):
# draw a tetris block of type n at the ith row and jth column
# of the grid
x = (GRID_OFFSET+j)*BLOCK_SIZE
y = i*BLOCK_SIZE
pgb.fill_rect(x,y,BLOCK_SIZE,BLOCK_SIZE,tetrominos_colors[n]) # main color
pgb.rect(x,y,BLOCK_SIZE,BLOCK_SIZE,BLACK) # black border
pgb.line(x+3,y+3,x+5,y+3,WHITE)
pgb.line(x+3,y+3,x+3,y+5,WHITE)
#####################################################################
# show title screen and wait for a button
title_screen()
# game loop
while True:
dx=0
dy=1
rotate=False
delay=500
if pgb.button_A() or pgb.button_B():
if last_button!="UP":
rotate=True
last_button="UP"
elif pgb.button_left():
last_button="RIGHT"
dx=-1
elif pgb.button_right():
last_button="RIGHT"
dx=1
elif pgb.button_down():
last_button="DOWN"
delay=0
else:
last_button="NONE"
# save current position to restore it
# in case the requested move generates a collision
for i in range(4):
prev_x[i] = x[i]
prev_y[i] = y[i]
# move left & right
for i in range(4):
x[i]+=dx
if collision(x, y):
# collision detected => impossible move
# => restore previous position
for i in range(4):
x[i] = prev_x[i]
y[i] = prev_y[i]
# rotate
if rotate:
# center of rotation
x0 = x[1]
y0 = y[1]
for i in range(4):
x_=y[i]-y0
y_=x[i]-x0
x[i]=x0-x_
y[i]=y0+y_
if collision(x, y):
# collision detected => impossible move
# => restore previous position
for i in range(4):
x[i] = prev_x[i]
y[i] = prev_y[i]
else:
has_rotated=True
# move down
ticks_ms = time.ticks_ms()
if time.ticks_diff(ticks_ms, now) > delay:
print(str(time.ticks_diff(ticks_ms, now)))
now = ticks_ms
if has_rotated:
freq=180
elif delay>0:
freq=140
else:
freq=0
pgb.sound(freq)
has_rotated = False
for i in range(4):
prev_x[i]=x[i]
prev_y[i]=y[i]
y[i]+=dy
if collision(x,y):
# collision detected
# collision at the top of the screen?
# => game over
for i in range(4):
if prev_y[i]<=1:
pgb.sound(0)
game_over_screen()
# => Store the last good position in the field
for i in range(4):
field[prev_y[i]][prev_x[i]]=n
# => choose randomly the next trinomino
n = next_n
next_n = randint(0, 6)
for i in range(4):
x[i]=(tetrominos[n][i]) % 2;
y[i]=int(tetrominos[n][i] / 2);
x[i]+=int(GRID_COLS/2)
# check lines
k=GRID_ROWS-1
for i in range(GRID_ROWS-1,0,-1):
count=0
for j in range(GRID_COLS):
if field[i][j]>=0:
count+=1
field[k][j]=field[i][j]
if count<GRID_COLS:
k-=1
else:
# ith line complete
lines+=1
score+=40
# make the line blink white <-> black
for l in range(3):
pgb.sound(1100)
pgb.fill_rect(GRID_OFFSET*BLOCK_SIZE,i*BLOCK_SIZE,
GRID_COLS*BLOCK_SIZE,BLOCK_SIZE,WHITE)
pgb.show()
time.sleep(0.050)
pgb.sound(2000)
pgb.fill_rect(GRID_OFFSET*BLOCK_SIZE,i*BLOCK_SIZE,
GRID_COLS*BLOCK_SIZE,BLOCK_SIZE,BLACK)
pgb.show()
time.sleep(0.050)
pgb.sound(0)
#####################################################################
# update screen
# background
draw_background()
# draw all the previous blocks
for i in range(GRID_ROWS):
for j in range(GRID_COLS):
if field[i][j]>=0:
# non empty
draw_block(j,i,field[i][j])
# draw the current block
for i in range(4):
draw_block(x[i],y[i],n)
# transfer the frame buffer to the actual screen over the SPI bus
pgb.show()
pgb.sound(0)