-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathДЗ 30 Pygame Tetris.txt
More file actions
131 lines (111 loc) · 4.19 KB
/
ДЗ 30 Pygame Tetris.txt
File metadata and controls
131 lines (111 loc) · 4.19 KB
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
import pygame
import random
pygame.init()
# Налаштування екрану
WIDTH = 300
HEIGHT = 600
GRID_WIDTH = 10
GRID_HEIGHT = 20
# Встановлення вікна
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
# Кольори
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)
colors = [BLACK, RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN]
# Фігури
shapes = [
[[1, 1, 1, 1]], # Лінія
[[1, 1], [1, 1]], # Квадрат
[[0, 1, 0], [1, 1, 1]], # Т
[[1, 1, 0], [0, 1, 1]], # Z
[[0, 1, 1], [1, 1, 0]], # S
[[1, 0, 0], [1, 1, 1]], # L
[[0, 0, 1], [1, 1, 1]], # J
]
# Сітка
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
def draw_grid():
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x] != 0:
pygame.draw.rect(screen, colors[grid[y][x]], (x * (WIDTH // GRID_WIDTH), y * (HEIGHT // GRID_HEIGHT), WIDTH // GRID_WIDTH, HEIGHT // GRID_HEIGHT))
def draw_shape(shape, offset):
for y in range(len(shape)):
for x in range(len(shape[y])):
if shape[y][x]:
pygame.draw.rect(screen, colors[shape[y][x]], ((x + offset[0]) * (WIDTH // GRID_WIDTH), (y + offset[1]) * (HEIGHT // GRID_HEIGHT), WIDTH // GRID_WIDTH, HEIGHT // GRID_HEIGHT))
def check_collision(shape, offset):
for y in range(len(shape)):
for x in range(len(shape[y])):
if shape[y][x]:
if (x + offset[0] < 0 or
x + offset[0] >= GRID_WIDTH or
y + offset[1] >= GRID_HEIGHT or
(y + offset[1] >= 0 and grid[y + offset[1]][x + offset[0]] != 0)):
return True
return False
def merge_shape(shape, offset):
for y in range(len(shape)):
for x in range(len(shape[y])):
if shape[y][x]:
grid[y + offset[1]][x + offset[0]] = shape[y][x]
def clear_lines():
global grid
new_grid = [row for row in grid if any(value == 0 for value in row)]
lines_cleared = GRID_HEIGHT - len(new_grid)
new_grid = [[0] * GRID_WIDTH for _ in range(lines_cleared)] + new_grid
grid = new_grid
def get_new_shape():
return random.choice(shapes)
# Основний цикл гри
while True:
screen.fill(BLACK)
current_shape = get_new_shape()
current_pos = [GRID_WIDTH // 2 - len(current_shape[0]) // 2, 0]
game_over = False
while not game_over:
screen.fill(BLACK)
draw_grid()
draw_shape(current_shape, current_pos)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_pos[0] -= 1
if check_collision(current_shape, current_pos):
current_pos[0] += 1
if event.key == pygame.K_RIGHT:
current_pos[0] += 1
if check_collision(current_shape, current_pos):
current_pos[0] -= 1
if event.key == pygame.K_DOWN:
current_pos[1] += 1
if check_collision(current_shape, current_pos):
current_pos[1] -= 1
if event.key == pygame.K_UP:
current_shape = [list(row) for row in zip(*current_shape[::-1])]
if check_collision(current_shape, current_pos):
current_shape = [list(row) for row in zip(*current_shape)][::-1]
current_pos[1] += 1
if check_collision(current_shape, current_pos):
current_pos[1] -= 1
merge_shape(current_shape, current_pos)
clear_lines()
current_shape = get_new_shape()
current_pos = [GRID_WIDTH // 2 - len(current_shape[0]) // 2, 0]
if check_collision(current_shape, current_pos):
game_over = True
pygame.display.flip()
clock.tick(10)
pygame.quit()