Загрузка данных
import pygame
import random
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 670
BLOCK_SIZE = 30
GRID_WIDTH = SCREEN_WIDTH // BLOCK_SIZE
GRID_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORS = [
(0, 255, 255),
(255, 255, 0),
(128, 0, 128),
(0, 255, 0),
(255, 0, 0),
(0, 0, 255),
(255, 165, 0)
]
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[0, 1, 0], [1, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 0], [0, 1, 1]],
[[1, 0, 0], [1, 1, 1]],
[[0, 0, 1], [1, 1, 1]]
]
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Тетрис")
class Figure:
def __init__(self):
self.shape = random.choice(SHAPES)
self.color = random.choice(COLORS)
self.x = GRID_WIDTH // 2 - len(self.shape[0]) // 2
self.y = 0
def rotate(self):
self.shape = [list(row) for row in zip(*self.shape[::-1])]
def check_collision(grid, figure, dx=0, dy=0):
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
new_x = figure.x + x + dx
new_y = figure.y + y + dy
if new_x < 0 or new_x >= GRID_WIDTH or \
new_y >= GRID_HEIGHT or \
(new_y >= 0 and grid[new_y][new_x]):
return True
return False
def draw_grid(grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x]:
pygame.draw.rect(screen, grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))
def main():
grid = [[None for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
figure = Figure()
clock = pygame.time.Clock()
fall_time = 0
score = 0
font = pygame.font.Font(None, 36)
running = True
while running:
screen.fill(BLACK)
fall_time += clock.get_rawtime()
clock.tick(10)
if fall_time > 5:
if not check_collision(grid, figure, dy=1):
figure.y += 1
else:
# Фиксация
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
grid[figure.y + y][figure.x + x] = figure.color
lines_cleared = 0
for y in range(GRID_HEIGHT - 1, -1, -1):
if all(grid[y]):
del grid[y]
grid.insert(0, [None for _ in range(GRID_WIDTH)])
lines_cleared += 1
score += lines_cleared ** 2
figure = Figure()
if check_collision(grid, figure):
running = False
fall_time = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not check_collision(grid, figure, dx=-1):
figure.x -= 1
if event.key == pygame.K_RIGHT and not check_collision(grid, figure, dx=1):
figure.x += 1
if event.key == pygame.K_DOWN and not check_collision(grid, figure, dy=1):
figure.y += 1
if event.key == pygame.K_UP:
old_shape = figure.shape
figure.rotate()
if check_collision(grid, figure):
figure.shape = old_shape
draw_grid(grid)
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, figure.color, ((figure.x + x) * BLOCK_SIZE, (figure.y + y) * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))
score_text = font.render(f"Score: {score}", True,
import pygame
import random
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 670
BLOCK_SIZE = 30
GRID_WIDTH = SCREEN_WIDTH // BLOCK_SIZE
GRID_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORS = [
(0, 255, 255),
(255, 255, 0),
(128, 0, 128),
(0, 255, 0),
(255, 0, 0),
(0, 0, 255),
(255, 165, 0)
]
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[0, 1, 0], [1, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 0], [0, 1, 1]],
[[1, 0, 0], [1, 1, 1]],
[[0, 0, 1], [1, 1, 1]]
]
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Тетрис")
class Figure:
def __init__(self):
self.shape = random.choice(SHAPES)
self.color = random.choice(COLORS)
self.x = GRID_WIDTH // 2 - len(self.shape[0]) // 2
self.y = 0
def rotate(self):
self.shape = [list(row) for row in zip(*self.shape[::-1])]
def check_collision(grid, figure, dx=0, dy=0):
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
new_x = figure.x + x + dx
new_y = figure.y + y + dy
if new_x < 0 or new_x >= GRID_WIDTH or \
new_y >= GRID_HEIGHT or \
(new_y >= 0 and grid[new_y][new_x]):
return True
return False
def draw_grid(grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x]:
pygame.draw.rect(screen, grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))
def main():
grid = [[None for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
figure = Figure()
clock = pygame.time.Clock()
fall_time = 0
score = 0
font = pygame.font.Font(None, 36)
running = True
while running:
screen.fill(BLACK)
fall_time += clock.get_rawtime()
clock.tick(10)
if fall_time > 5:
if not check_collision(grid, figure, dy=1):
figure.y += 1
else:
# Фиксация
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
grid[figure.y + y][figure.x + x] = figure.color
lines_cleared = 0
for y in range(GRID_HEIGHT - 1, -1, -1):
if all(grid[y]):
del grid[y]
grid.insert(0, [None for _ in range(GRID_WIDTH)])
lines_cleared += 1
score += lines_cleared ** 2
figure = Figure()
if check_collision(grid, figure):
running = False
fall_time = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not check_collision(grid, figure, dx=-1):
figure.x -= 1
if event.key == pygame.K_RIGHT and not check_collision(grid, figure, dx=1):
figure.x += 1
if event.key == pygame.K_DOWN and not check_collision(grid, figure, dy=1):
figure.y += 1
if event.key == pygame.K_UP:
old_shape = figure.shape
figure.rotate()
if check_collision(grid, figure):
figure.shape = old_shape
draw_grid(grid)
for y, row in enumerate(figure.shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, figure.color, ((figure.x + x) * BLOCK_SIZE, (figure.y + y) * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))
score_text = font.render(f"Score: {score}", True,
WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()