Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/python-igra-tetris/main_pygame.py
5925 views
1
import pygame
2
from copy import deepcopy
3
from random import choice, randrange
4
5
W, H = 10, 20
6
TILE = 45
7
GAME_RES = W * TILE, H * TILE
8
RES = 750, 940
9
FPS = 60
10
11
pygame.init()
12
sc = pygame.display.set_mode(RES)
13
game_sc = pygame.Surface(GAME_RES)
14
clock = pygame.time.Clock()
15
16
grid = [pygame.Rect(x * TILE, y * TILE, TILE, TILE) for x in range(W) for y in range(H)]
17
18
figures_pos = [[(-1, 0), (-2, 0), (0, 0), (1, 0)],
19
[(0, -1), (-1, -1), (-1, 0), (0, 0)],
20
[(-1, 0), (-1, 1), (0, 0), (0, -1)],
21
[(0, 0), (-1, 0), (0, 1), (-1, -1)],
22
[(0, 0), (0, -1), (0, 1), (-1, -1)],
23
[(0, 0), (0, -1), (0, 1), (1, -1)],
24
[(0, 0), (0, -1), (0, 1), (-1, 0)]]
25
26
figures = [[pygame.Rect(x + W // 2, y + 1, 1, 1) for x, y in fig_pos] for fig_pos in figures_pos]
27
figure_rect = pygame.Rect(0, 0, TILE - 2, TILE - 2)
28
field = [[0 for i in range(W)] for j in range(H)]
29
30
anim_count, anim_speed, anim_limit = 0, 60, 2000
31
32
bg = pygame.image.load('img/bg.jpg').convert()
33
game_bg = pygame.image.load('img/bg2.jpg').convert()
34
35
main_font = pygame.font.Font('font/font.ttf', 65)
36
font = pygame.font.Font('font/font.ttf', 45)
37
38
title_tetris = main_font.render('TETRIS', True, pygame.Color('red'))
39
title_score = font.render('score:', True, pygame.Color('white'))
40
title_record = font.render('record:', True, pygame.Color('white'))
41
42
get_color = lambda : (randrange(30, 256), randrange(30, 256), randrange(30, 256))
43
44
figure, next_figure = deepcopy(choice(figures)), deepcopy(choice(figures))
45
color, next_color = get_color(), get_color()
46
47
score, lines = 0, 0
48
scores = {0: 0, 1: 100, 2: 300, 3: 700, 4: 1500}
49
50
51
def check_borders():
52
if figure[i].x < 0 or figure[i].x > W - 1:
53
return False
54
elif figure[i].y > H - 1 or field[figure[i].y][figure[i].x]:
55
return False
56
return True
57
58
59
def get_record():
60
try:
61
with open('record') as f:
62
return f.readline()
63
except FileNotFoundError:
64
with open('record', 'w') as f:
65
f.write('0')
66
67
68
def set_record(record, score):
69
rec = max(int(record), score)
70
with open('record', 'w') as f:
71
f.write(str(rec))
72
73
74
while True:
75
record = get_record()
76
dx, rotate = 0, False
77
sc.blit(bg, (0, 0))
78
sc.blit(game_sc, (20, 20))
79
game_sc.blit(game_bg, (0, 0))
80
# delay for full lines
81
for i in range(lines):
82
pygame.time.wait(200)
83
# control
84
for event in pygame.event.get():
85
if event.type == pygame.QUIT:
86
exit()
87
if event.type == pygame.KEYDOWN:
88
if event.key == pygame.K_LEFT:
89
dx = -1
90
elif event.key == pygame.K_RIGHT:
91
dx = 1
92
elif event.key == pygame.K_DOWN:
93
anim_limit = 100
94
elif event.key == pygame.K_UP:
95
rotate = True
96
# move x
97
figure_old = deepcopy(figure)
98
for i in range(4):
99
figure[i].x += dx
100
if not check_borders():
101
figure = deepcopy(figure_old)
102
break
103
# move y
104
anim_count += anim_speed
105
if anim_count > anim_limit:
106
anim_count = 0
107
figure_old = deepcopy(figure)
108
for i in range(4):
109
figure[i].y += 1
110
if not check_borders():
111
for i in range(4):
112
field[figure_old[i].y][figure_old[i].x] = color
113
figure, color = next_figure, next_color
114
next_figure, next_color = deepcopy(choice(figures)), get_color()
115
anim_limit = 2000
116
break
117
# rotate
118
center = figure[0]
119
figure_old = deepcopy(figure)
120
if rotate:
121
for i in range(4):
122
x = figure[i].y - center.y
123
y = figure[i].x - center.x
124
figure[i].x = center.x - x
125
figure[i].y = center.y + y
126
if not check_borders():
127
figure = deepcopy(figure_old)
128
break
129
# check lines
130
line, lines = H - 1, 0
131
for row in range(H - 1, -1, -1):
132
count = 0
133
for i in range(W):
134
if field[row][i]:
135
count += 1
136
field[line][i] = field[row][i]
137
if count < W:
138
line -= 1
139
else:
140
anim_speed += 3
141
lines += 1
142
# compute score
143
score += scores[lines]
144
# draw grid
145
[pygame.draw.rect(game_sc, (40, 40, 40), i_rect, 1) for i_rect in grid]
146
# draw figure
147
for i in range(4):
148
figure_rect.x = figure[i].x * TILE
149
figure_rect.y = figure[i].y * TILE
150
pygame.draw.rect(game_sc, color, figure_rect)
151
# draw field
152
for y, raw in enumerate(field):
153
for x, col in enumerate(raw):
154
if col:
155
figure_rect.x, figure_rect.y = x * TILE, y * TILE
156
pygame.draw.rect(game_sc, col, figure_rect)
157
# draw next figure
158
for i in range(4):
159
figure_rect.x = next_figure[i].x * TILE + 380
160
figure_rect.y = next_figure[i].y * TILE + 185
161
pygame.draw.rect(sc, next_color, figure_rect)
162
# draw titles
163
sc.blit(title_tetris, (505, 30))
164
sc.blit(title_score, (535, 780))
165
sc.blit(font.render(str(score), True, pygame.Color('white')), (550, 840))
166
sc.blit(title_record, (525, 650))
167
sc.blit(font.render(record, True, pygame.Color('gold')), (550, 710))
168
# game over
169
for i in range(W):
170
if field[0][i]:
171
set_record(record, score)
172
field = [[0 for i in range(W)] for i in range(H)]
173
anim_count, anim_speed, anim_limit = 0, 60, 2000
174
score = 0
175
for i_rect in grid:
176
pygame.draw.rect(game_sc, get_color(), i_rect)
177
sc.blit(game_sc, (20, 20))
178
pygame.display.flip()
179
clock.tick(200)
180
181
pygame.display.flip()
182
clock.tick(FPS)
183
184