#需要使用python3版本
######################################################
安装python3
安装 EPEL 存储库,它包含了 Python 3 的软件包。
yum install epel-release
安装 Python 3 和 pip(Python 包管理工具):
yum install python3
yum install python3-pip
验证 Python 3 安装是否成功:
python3 --version
升级 pip 到最新版本:
pip3 install --upgrade pip
#######################################################
vim tetris.py
加入:
import pygame
import random
# Initialize Pygame
pygame.init()
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
GREEN = (0, 128, 0)
RED = (255, 0, 0)
MAGENTA = (255, 0, 255)
ORANGE = (255, 165, 0)
# Tetris grid size
GRID_SIZE = 30
GRID_WIDTH = 10
GRID_HEIGHT = 20
# Initialize the screen
screen_width = GRID_SIZE * GRID_WIDTH
screen_height = GRID_SIZE * GRID_HEIGHT
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Tetris")
# Tetris shapes
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [0, 0, 1]]
]
SHAPE_COLORS = [CYAN, YELLOW, BLUE, GREEN, RED, MAGENTA, ORANGE]
def draw_grid(surface, grid):
for y, row in enumerate(grid):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(surface, cell, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
pygame.draw.rect(surface, BLACK, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 3)
def new_piece():
shape = random.choice(SHAPES)
color = random.choice(SHAPE_COLORS)
piece = {
'shape': shape,
'color': color,
'x': GRID_WIDTH // 2 - len(shape[0]) // 2,
'y': 0
}
return piece
def check_collision(piece, grid):
for y, row in enumerate(piece['shape']):
for x, cell in enumerate(row):
if cell:
if (
piece['x'] + x < 0 or
piece['x'] + x >= GRID_WIDTH or
piece['y'] + y >= GRID_HEIGHT or
grid[piece['y'] + y][piece['x'] + x] != 0
):
return True
return False
def merge_piece(piece, grid):
for y, row in enumerate(piece['shape']):
for x, cell in enumerate(row):
if cell:
grid[piece['y'] + y][piece['x'] + x] = piece['color']
def clear_rows(grid):
full_rows = [i for i, row in enumerate(grid) if all(row)]
for row in full_rows:
del grid[row]
grid.insert(0, [0] * GRID_WIDTH)
def main():
clock = pygame.time.Clock()
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
piece = new_piece()
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
piece['x'] -= 1
if check_collision(piece, grid):
piece['x'] += 1
elif event.key == pygame.K_RIGHT:
piece['x'] += 1
if check_collision(piece, grid):
piece['x'] -= 1
elif event.key == pygame.K_DOWN:
piece['y'] += 1
if check_collision(piece, grid):
piece['y'] -= 1
elif event.key == pygame.K_UP:
rotated_piece = {
'shape': list(zip(*reversed(piece['shape']))),
'color': piece['color'],
'x': piece['x'],
'y': piece['y']
}
if not check_collision(rotated_piece, grid):
piece = rotated_piece
piece['y'] += 1
if check_collision(piece, grid):
piece['y'] -= 1
merge_piece(piece, grid)
clear_rows(grid)
piece = new_piece()
if check_collision(piece, grid):
game_over = True
screen.fill(BLACK)
draw_grid(screen, grid)
for y, row in enumerate(piece['shape']):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, piece['color'], (piece['x'] * GRID_SIZE + x * GRID_SIZE, piece['y'] * GRID_SIZE + y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
pygame.draw.rect(screen, BLACK, (piece['x'] * GRID_SIZE + x * GRID_SIZE, piece['y'] * GRID_SIZE + y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 3)
pygame.display.update()
clock.tick(5)
pygame.quit()
if __name__ == "__main__":
main()
#执行脚本
python3 tetris.py