用python库pygame实现俄罗斯方块小游戏

以下是一个简单的 Python 代码实现俄罗斯方块游戏的示例:

import pygame
import random

# 初始化 Pygame
pygame.init()

# 游戏区域大小
screen_width = 640
screen_height = 480

# 方块大小和间隙
block_size = 20
block_gap = 2

# 颜色定义
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
purple = (128, 0, 128)
orange = (255, 165, 0)

# 方块形状定义
shapes = [
    [(0, 1, 0), (1, 1, 1)],
    [(2, 2), (2, 2)],
    [(0, 3, 3), (3, 3, 0)],
    [(4, 4, 0), (0, 4, 4)],
    [(5, 5, 5, 5)],
    [(6, 6, 6), (0, 0, 6)],
    [(7, 7, 7), (7, 0, 0)]
]

# 定义方块类
class Block:
    def __init__(self):
        self.shape = random.choice(shapes)
        self.color = random.choice([red, green, blue, yellow, purple, orange])
        self.x = int(screen_width / block_size / 2) - len(self.shape[0]) // 2
        self.y = 0

    # 绘制方块
    def draw(self, screen):
        for i in range(len(self.shape)):
            for j in range(len(self.shape[i])):
                if self.shape[i][j]:
                    pygame.draw.rect(screen, self.color, (self.x * block_size + j * (block_size + block_gap),
                                                          self.y * block_size + i * (block_size + block_gap),
                                                          block_size, block_size))

    # 检查方块是否可以移动到指定位置
    def can_move(self, x, y, board):
        for i in range(len(self.shape)):
            for j in range(len(self.shape[i])):
                if self.shape[i][j]:
                    if x + j < 0 or x + j >= len(board[0]) or y + i >= len(board) or board[y + i][x + j] != black:
                        return False
        return True

    # 移动方块
    def move(self, dx, dy, board):
        if self.can_move(self.x + dx, self.y + dy, board):
            self.x += dx
            self.y += dy
            return True
        return False

    # 旋转方块
    def rotate(self, board):
        old_shape = self.shape
        self.shape = list(zip(*self.shape[::-1]))
        if not self.can_move(self.x, self.y, board):
            self.shape = old_shape


# 初始化游戏区域和方块
board = [[black for _ in range(screen_width // block_size)]
         for _ in range(screen_height // block_size)]
active_block = Block()

# 设置 Pygame 窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Tetris")

# 游戏循环
while True:
    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                active_block.move(-1, 0, board)
            elif event.key == pygame.K_RIGHT:
                active_block.move(1, 0, board)
            elif event.key == pygame.K_DOWN:
                active_block.move(0, 1, board)
            elif event.key == pygame.K_UP:
                active_block.rotate(board)

    # 移动方块并在底部停止时生成新方块
    if not active_block.move(0, 1, board):
        for i in range(len(active_block.shape)):
            for j in range(len(active_block.shape[i])):
                if active_block.shape[i][j]:
                    board[active_block.y + i][active_block.x + j] = active_block.color
        active_block = Block()

    # 绘制游戏区域和方块
    screen.fill(black)
    for i in range(len(board)):
        for j in range(len(board[i])):
            pygame.draw.rect(screen, board[i][j], (j * (block_size + block_gap), i * (block_size + block_gap),
                                                   block_size, block_size))
    active_block.draw(screen)
    pygame.display.update()

此代码实现了一个简单的俄罗斯方块游戏,其中使用 Pygame 库来处理游戏图形、事件和界面显示。运行代码后,将会出现一个窗口,并在其中显示俄罗斯方块游戏,玩家可以通过键盘控制方块的移动和旋转,将方块拼接在一起并消除行。

你可能感兴趣的:(python项目,python,pygame,开发语言)