Загрузка данных
import os
import time
import random
import sys
# === Настройки ===
WIDTH = 10
HEIGHT = 20
EMPTY = '.'
BLOCK = '█'
# Фигурки
SHAPES = {
'I': [['....',
'████',
'....',
'....']],
'J': [['█..',
'███',
'...']],
'L': [['..█',
'███',
'...']],
'O': [['██',
'██']],
'S': [['.██',
'██.',
'...']],
'T': [['.█.',
'███',
'...']],
'Z': [['██.',
'.██',
'...']],
}
COLORS = list(SHAPES.keys())
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def create_board():
return [[EMPTY for _ in range(WIDTH)] for _ in range(HEIGHT)]
def rotate(shape):
# Поворот по часовой
return [''.join(row) for row in zip(*shape[::-1])]
def valid(board, shape, x, y):
for i, row in enumerate(shape):
for j, cell in enumerate(row):
if cell == '█':
nx, ny = x + j, y + i
if nx < 0 or nx >= WIDTH or ny >= HEIGHT:
return False
if ny >= 0 and board[ny][nx] == BLOCK:
return False
return True
def place(board, shape, x, y):
for i, row in enumerate(shape):
for j, cell in enumerate(row):
if cell == '█' and y + i >= 0:
board[y + i][x + j] = BLOCK
def clear_lines(board):
new_board = [row for row in board if EMPTY in row]
lines_cleared = HEIGHT - len(new_board)
for _ in range(lines_cleared):
new_board.insert(0, [EMPTY] * WIDTH)
return new_board, lines_cleared
def print_board(board, score, current_shape=None, cx=0, cy=0):
clear_screen()
temp = [row[:] for row in board]
if current_shape:
for i, row in enumerate(current_shape):
for j, cell in enumerate(row):
if cell == '█' and cy + i >= 0:
temp[cy + i][cx + j] = BLOCK
print("╔" + "══" * WIDTH + "╗")
for row in temp:
print("║" + "".join(row) + "║")
print("╚" + "══" * WIDTH + "╝")
print(f"Очки: {score}")
print("Управление: a/d - влево/вправо | s - вниз | w - поворот | q - выход")
def main():
board = create_board()
score = 0
fall_speed = 0.5
last_fall = time.time()
current = random.choice(COLORS)
shape = [list(row) for row in SHAPES[current]]
x, y = WIDTH // 2 - len(shape[0]) // 2, 0
while True:
print_board(board, score, shape, x, y)
# Управление (неблокирующее через таймер)
if sys.platform != 'win32':
import select
if select.select([sys.stdin], [], [], 0.05)[0]:
key = sys.stdin.read(1).lower()
else:
key = ''
else:
# На Windows онлайн-интерпретаторы часто не умеют нормальный ввод
# Поэтому делаем простое управление по Enter
key = input("Ход (a/d/s/w/q): ").lower().strip()
if key == 'q':
break
elif key == 'a':
if valid(board, shape, x - 1, y):
x -= 1
elif key == 'd':
if valid(board, shape, x + 1, y):
x += 1
elif key == 's':
if valid(board, shape, x, y + 1):
y += 1
elif key == 'w':
new_shape = rotate(shape)
if valid(board, new_shape, x, y):
shape = new_shape
# Автопадение
if time.time() - last_fall > fall_speed:
if valid(board, shape, x, y + 1):
y += 1
else:
place(board, shape, x, y)
board, cleared = clear_lines(board)
score += cleared * 100
# Новая фигура
current = random.choice(COLORS)
shape = [list(row) for row in SHAPES[current]]
x = WIDTH // 2 - len(shape[0]) // 2
y = 0
if not valid(board, shape, x, y):
print_board(board, score)
print("\nИГРА ОКОНЧЕНА")
break
last_fall = time.time()
time.sleep(0.05)
if __name__ == "__main__":
main()