-
Notifications
You must be signed in to change notification settings - Fork 21
/
pytris.old.py
451 lines (382 loc) · 14.2 KB
/
pytris.old.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# PYTRIS™ Copyright (c) 2017 Jason Kim All Rights Reserved.
import pygame
from mino import *
from random import *
from pygame.locals import *
# Define
block_size = 17 # Height, width of single block
width = 10 # Board width
height = 20 # Board height
framerate = 30 # Bigger -> Slower
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((300, 374))
pygame.time.set_timer(pygame.USEREVENT, framerate * 10)
pygame.display.set_caption("PYTRIS™")
class ui_variables:
# Fonts
font_path = "./assets/fonts/OpenSans-Light.ttf"
h1 = pygame.font.Font(font_path, 50)
h2 = pygame.font.Font(font_path, 30)
h4 = pygame.font.Font(font_path, 20)
h5 = pygame.font.Font(font_path, 13)
h6 = pygame.font.Font(font_path, 10)
# Sounds
click_sound = pygame.mixer.Sound("assets/sounds/SFX_ButtonUp.wav")
move_sound = pygame.mixer.Sound("assets/sounds/SFX_PieceMoveLR.wav")
drop_sound = pygame.mixer.Sound("assets/sounds/SFX_PieceHardDrop.wav")
single_sound = pygame.mixer.Sound("assets/sounds/SFX_SpecialLineClearSingle.wav")
double_sound = pygame.mixer.Sound("assets/sounds/SFX_SpecialLineClearDouble.wav")
triple_sound = pygame.mixer.Sound("assets/sounds/SFX_SpecialLineClearTriple.wav")
tetris_sound = pygame.mixer.Sound("assets/sounds/SFX_SpecialTetris.wav")
# Background colors
black = (10, 10, 10) #rgb(10, 10, 10)
white = (255, 255, 255) #rgb(255, 255, 255)
grey_1 = (26, 26, 26) #rgb(26, 26, 26)
grey_2 = (35, 35, 35) #rgb(35, 35, 35)
grey_3 = (55, 55, 55) #rgb(55, 55, 55)
# Tetrimino colors
cyan = (69, 206, 204) #rgb(69, 206, 204) # I
blue = (64, 111, 249) #rgb(64, 111, 249) # J
orange = (253, 189, 53) #rgb(253, 189, 53) # L
yellow = (246, 227, 90) #rgb(246, 227, 90) # O
green = (98, 190, 68) #rgb(98, 190, 68) # S
pink = (242, 64, 235) #rgb(242, 64, 235) # T
red = (225, 13, 27) #rgb(225, 13, 27) # Z
t_color = [grey_2, cyan, blue, orange, yellow, green, pink, red, grey_3]
# Draw single block
def draw_block(x, y, color):
pygame.draw.rect(
screen,
color,
Rect(x, y, block_size, block_size)
)
pygame.draw.rect(
screen,
ui_variables.grey_1,
Rect(x, y, block_size, block_size),
1
)
# Draw game screen
def draw_board(next, hold, score, level, goal):
screen.fill(ui_variables.grey_1)
pygame.draw.rect(
screen,
ui_variables.white,
Rect(204, 0, 96, 374)
)
# Draw next mino
grid_n = tetrimino.mino_map[next - 1][0]
for i in range(4):
for j in range(4):
dx = 220 + block_size * j
dy = 150 + block_size * i
if grid_n[i][j] != 0:
pygame.draw.rect(
screen,
ui_variables.t_color[grid_n[i][j]],
Rect(dx, dy, block_size, block_size)
)
# Draw hold mino
grid_h = tetrimino.mino_map[hold - 1][0]
if hold_mino != -1:
for i in range(4):
for j in range(4):
dx = 220 + block_size * j
dy = 50 + block_size * i
if grid_h[i][j] != 0:
pygame.draw.rect(
screen,
ui_variables.t_color[grid_h[i][j]],
Rect(dx, dy, block_size, block_size)
)
# Set max score
if score > 999999:
score = 999999
# Draw texts
text_hold = ui_variables.h5.render("HOLD", 1, ui_variables.black)
text_next = ui_variables.h5.render("NEXT", 1, ui_variables.black)
text_score = ui_variables.h5.render("SCORE", 1, ui_variables.black)
score_value = ui_variables.h4.render(str(score), 1, ui_variables.black)
text_level = ui_variables.h5.render("LEVEL", 1, ui_variables.black)
level_value = ui_variables.h4.render(str(level), 1, ui_variables.black)
text_goal = ui_variables.h5.render("GOAL", 1, ui_variables.black)
goal_value = ui_variables.h4.render(str(goal), 1, ui_variables.black)
# Place texts
screen.blit(text_hold, (215, 14))
screen.blit(text_next, (215, 114))
screen.blit(text_score, (215, 214))
screen.blit(score_value, (220, 230))
screen.blit(text_level, (215, 264))
screen.blit(level_value, (220, 280))
screen.blit(text_goal, (215, 314))
screen.blit(goal_value, (220, 330))
# Draw board
for x in range(width):
for y in range(height):
dx = 17 + block_size * x
dy = 17 + block_size * y
draw_block(dx, dy, ui_variables.t_color[matrix[x][y]])
# Draw a tetrimino
def draw_mino(x, y, mino, r):
grid = tetrimino.mino_map[mino - 1][r]
tx, ty = x, y
while not is_bottom(tx, ty, mino, r):
ty += 1
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
matrix[tx + j][ty + i] = 8
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
matrix[x + j][y + i] = grid[i][j]
# Erase a tetrimino
def erase_mino(x, y, mino, r):
grid = tetrimino.mino_map[mino - 1][r]
for j in range(20):
for i in range(10):
if matrix[i][j] == 8:
matrix[i][j] = 0
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
matrix[x + j][y + i] = 0
def is_bottom(x, y, mino, r):
grid = tetrimino.mino_map[mino - 1][r]
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
if (y + i + 1) > 19:
return True
elif matrix[x + j][y + i + 1] != 0 and matrix[x + j][y + i + 1] != 8:
return True
return False
def is_leftedge(x, y, mino, r):
grid = tetrimino.mino_map[mino - 1][r]
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
if (x + j - 1) < 0:
return True
elif matrix[x + j - 1][y + i] != 0:
return True
return False
def is_rightedge(x, y, mino, r):
grid = tetrimino.mino_map[mino - 1][r]
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
if (x + j + 1) > 9:
return True
elif matrix[x + j + 1][y + i] != 0:
return True
return False
def is_turnable(x, y, mino, r):
if r != 3:
grid = tetrimino.mino_map[mino - 1][r + 1]
else:
grid = tetrimino.mino_map[mino - 1][0]
for i in range(4):
for j in range(4):
if grid[i][j] != 0:
if (x + j) < 0 or (x + j) > 9 or (y + i) < 0 or (y + i) > 19:
return False
elif matrix[x + j][y + i] != 0:
return False
return True
def is_stackable(mino):
grid = tetrimino.mino_map[mino - 1][0]
for i in range(4):
for j in range(4):
#print(grid[i][j], matrix[3 + j][i])
if grid[i][j] != 0 and matrix[3 + j][i] != 0:
return False
return True
# Initial values
blink = True
start = False
done = False
game_over = False
score = 0
level = 1
goal = level * 5
bottom_count = 0
hard_drop = False
dx, dy = 3, 0
rotation = 0
mino = randint(1, 7)
next_mino = randint(1, 7)
hold = False
hold_mino = -1
matrix = [[0 for y in range(height)] for x in range(width)]
###########################################################
# Loop Start
###########################################################
while not done:
# Game screen
if start:
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == USEREVENT:
# Set speed
pygame.time.set_timer(pygame.USEREVENT, framerate * 10)
# Draw a mino
draw_mino(dx, dy, mino, rotation)
draw_board(next_mino, hold_mino, score, level, goal)
# Erase a mino
erase_mino(dx, dy, mino, rotation)
# Move mino down
if not is_bottom(dx, dy, mino, rotation):
dy += 1
# Create new mino
else:
if hard_drop or bottom_count == 4:
hard_drop = False
bottom_count = 0
score += 10 * level
draw_mino(dx, dy, mino, rotation)
draw_board(next_mino, hold_mino, score, level, goal)
if is_stackable(next_mino):
mino = next_mino
next_mino = randint(1, 7)
dx, dy = 3, 0
rotation = 0
hold = False
else:
start = False
game_over = True
else:
bottom_count += 1
# Erase line
erase_count = 0
for j in range(20):
is_full = True
for i in range(10):
if matrix[i][j] == 0:
is_full = False
if is_full:
erase_count += 1
k = j
while k > 0:
for i in range(10):
matrix[i][k] = matrix[i][k - 1]
k -= 1
if erase_count == 1:
ui_variables.single_sound.play()
score += 50 * level
elif erase_count == 2:
ui_variables.double_sound.play()
score += 150 * level
elif erase_count == 3:
ui_variables.triple_sound.play()
score += 350 * level
elif erase_count == 4:
ui_variables.tetris_sound.play()
score += 1000 * level
# Increase level
goal -= erase_count
if goal < 1 and level < 15:
level += 1
goal += level * 5
framerate = int(framerate * 0.8)
elif event.type == KEYDOWN:
erase_mino(dx, dy, mino, rotation)
if event.key == K_SPACE:
ui_variables.drop_sound.play()
while not is_bottom(dx, dy, mino, rotation):
dy += 1
hard_drop = True
pygame.time.set_timer(pygame.USEREVENT, 5)
elif event.key == K_LSHIFT:
if hold == False:
ui_variables.move_sound.play()
if hold_mino == -1:
hold_mino = mino
mino = next_mino
next_mino = randint(1, 7)
else:
hold_mino, mino = mino, hold_mino
dx, dy = 3, 0
rotation = 0
hold = True
elif event.key == K_UP:
if is_turnable(dx, dy, mino, rotation):
ui_variables.move_sound.play()
rotation += 1
if rotation == 4:
rotation = 0
elif event.key == K_DOWN:
if not is_bottom(dx, dy, mino, rotation):
ui_variables.move_sound.play()
dy += 1
elif event.key == K_LEFT:
if not is_leftedge(dx, dy, mino, rotation):
ui_variables.move_sound.play()
dx -= 1
elif event.key == K_RIGHT:
if not is_rightedge(dx, dy, mino, rotation):
ui_variables.move_sound.play()
dx += 1
draw_mino(dx, dy, mino, rotation)
draw_board(next_mino, hold_mino, score, level, goal)
pygame.display.update()
# Game over screen
elif game_over:
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_SPACE:
ui_variables.click_sound.play()
game_over = False
hold = False
dx, dy = 3, 0
rotation = 0
mino = randint(1, 7)
next_mino = randint(1, 7)
hold_mino = -1
score = 0
matrix = [[0 for y in range(height)] for x in range(width)]
over_text = ui_variables.h2.render("GAME OVER", 1, ui_variables.white)
over_start = ui_variables.h5.render("Press space to continue", 1, ui_variables.white)
if game_over == True:
draw_board(next_mino, hold_mino, score, level, goal)
screen.blit(over_text, (20, 100))
if blink:
screen.blit(over_start, (32, 160))
blink = False
else:
blink = True
pygame.display.update()
clock.tick(3)
# Start screen
else:
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_SPACE:
ui_variables.click_sound.play()
start = True
screen.fill(ui_variables.white)
pygame.draw.rect(
screen,
ui_variables.grey_1,
Rect(0, 187, 300, 187)
)
title = ui_variables.h1.render("PYTRIS™", 1, ui_variables.grey_1)
title_start = ui_variables.h5.render("Press space to start", 1, ui_variables.white)
title_info = ui_variables.h6.render("Copyright (c) 2017 Jason Kim All Rights Reserved.", 1, ui_variables.white)
if blink:
screen.blit(title_start, (92, 195))
blink = False
else:
blink = True
screen.blit(title, (65, 120))
screen.blit(title_info, (40, 335))
if not start:
pygame.display.update()
clock.tick(3)
pygame.quit()