-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris_pygame.py
190 lines (168 loc) · 6.13 KB
/
tetris_pygame.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
"""Tetris pygame"""
import pygame
from copy import deepcopy
from random import choice, randrange
from const import *
def check_borders():
"""Функція, яка відстежує вихід за межі поля"""
if not 0 <= figure[i].x < WIDTH:
return False
elif figure[i].y > HEIGHT - 1 or field[figure[i].y][figure[i].x]:
return False
return True
def get_record():
"""Функція, яка відкриває файл для читання найкращого результату"""
try:
with open('record.txt') as file:
return file.readline()
except FileNotFoundError:
with open('record.txt', 'w', encoding='UTF-8') as file:
file.write('0')
def set_record(record, score):
"""Функція, яка записує новий рекорд до файлу"""
rec = max(int(record), score)
with open('record.txt', 'w', encoding='UTF-8') as file:
file.write(str(rec))
# ініціалізація бібліотеки
pygame.init()
sc = pygame.display.set_mode(RES)
game_sc = pygame.Surface(GAME_RES)
clock = pygame.time.Clock()
# фонова музика
pygame.mixer.init()
pygame.mixer.music.load(MAIN_MUSIC_PATH)
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(-1)
grid = [pygame.Rect(x * TILE, y * TILE, TILE, TILE) for x in range(WIDTH) for y in range(HEIGHT)]
figures_pos = [
[(-2, -1), (-1, -1), (0, -1), (1, -1)],
[(0, -1), (-1, -1), (-1, 0), (0, 0)],
[(-1, -1), (0, 0), (-1, 0), (0, 1)],
[(0, -1), (0, 0), (-1, 0), (-1, 1)],
[(0, 0), (0, -1), (0, 1), (-1, -1)],
[(0, -1), (-1, -1), (-1, 0), (-1, 1)],
[(0, 0), (0, -1), (0, 1), (-1, 0)],
]
figures = [[pygame.Rect(x + WIDTH // 2, y + 1, 1, 1) for x, y in fig_pos] for fig_pos in figures_pos]
figure_rect = pygame.Rect(0, 0, TILE - 2, TILE - 2)
field = [[0 for i in range(WIDTH)] for j in range(HEIGHT)]
ANIM_COUNT, ANIM_SPEED, ANIM_LIMIT = 0, 60, 2000
bg = pygame.image.load('img/bg1.jpg').convert()
game_bg = pygame.image.load('img/bg2.jpg').convert()
main_font = pygame.font.Font('font/font.ttf', 65)
font = pygame.font.Font('font/font.ttf', 45)
title_tetris = main_font.render('TETRIS', True, pygame.Color('darkorange'))
title_score = font.render('score:', True, pygame.Color('green'))
title_record = font.render('record:', True, pygame.Color('purple'))
get_color = lambda: (randrange(50, 256), randrange(50, 256), randrange(50, 256))
figure, next_figure = deepcopy(choice(figures)), deepcopy(choice(figures))
color, next_color = get_color(), get_color()
SCORE, lines = 0, 0
while True:
record = get_record()
dx, rotate = 0, False
sc.blit(bg, (0, 0))
sc.blit(game_sc, (20, 20))
game_sc.blit(game_bg, (0, 0))
# delay for full lines
for i in range(lines):
pygame.time.wait(200)
# control
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx = -1
elif event.key == pygame.K_RIGHT:
dx = 1
elif event.key == pygame.K_DOWN:
ANIM_LIMIT = 100
elif event.key == pygame.K_UP:
rotate = True
# move x
figure_old = deepcopy(figure)
for i in range(4):
figure[i].x += dx
if not check_borders():
figure = deepcopy(figure_old)
break
# move y
ANIM_COUNT += ANIM_SPEED
if ANIM_COUNT > ANIM_LIMIT:
ANIM_COUNT = 0
figure_old = deepcopy(figure)
for i in range(4):
figure[i].y += 1
if not check_borders():
for i in range(4):
field[figure_old[i].y][figure_old[i].x] = color
figure, color = next_figure, next_color
next_figure, next_color = deepcopy(choice(figures)), get_color()
ANIM_LIMIT = 2000
break
# rotate
center = figure[0]
figure_old = deepcopy(figure)
if rotate:
for i in range(4):
x = figure[i].y - center.y
y = figure[i].x - center.x
figure[i].x = center.x - x
figure[i].y = center.y + y
if not check_borders():
figure = deepcopy(figure_old)
break
# check lines
line, lines = HEIGHT - 1, 0
for row in range(HEIGHT - 1, -1, -1):
count = 0
for i in range(WIDTH):
if field[row][i]:
count += 1
field[line][i] = field[row][i]
if count < WIDTH:
line -= 1
else:
ANIM_SPEED += 3
lines += 1
# compute score
SCORE += SCORES[lines]
# draw grid
[pygame.draw.rect(game_sc, (40, 40, 40), i_rect, 1) for i_rect in grid]
# draw figure
for i in range(4):
figure_rect.x = figure[i].x * TILE
figure_rect.y = figure[i].y * TILE
pygame.draw.rect(game_sc, color, figure_rect)
# draw field
for y, raw in enumerate(field):
for x, col in enumerate(raw):
if col:
figure_rect.x, figure_rect.y = x * TILE, y * TILE
pygame.draw.rect(game_sc, col, figure_rect)
# draw next figure
for i in range(4):
figure_rect.x = next_figure[i].x * TILE + 380
figure_rect.y = next_figure[i].y * TILE + 185
pygame.draw.rect(sc, next_color, figure_rect)
# draw titles
sc.blit(title_tetris, (450, 10))
sc.blit(title_score, (450, 580))
sc.blit(font.render(str(SCORE), True, pygame.Color('white')), (450, 640))
sc.blit(title_record, (450, 450))
sc.blit(font.render(record, True, pygame.Color('gold')), (450, 510))
# game over
for i in range(WIDTH):
if field[0][i]:
set_record(record, SCORE)
field = [[0 for i in range(WIDTH)] for i in range(HEIGHT)]
ANIM_COUNT, ANIM_SPEED, ANIM_LIMIT = 0, 60, 2000
SCORE = 0
for i_rect in grid:
pygame.draw.rect(game_sc, get_color(), i_rect)
sc.blit(game_sc, (20, 20))
pygame.display.flip()
clock.tick(200)
pygame.display.flip()
clock.tick(FPS)