当然可以,下面是一个简单的俄罗斯方块游戏的代码示例:

import pygame
import random

# 游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600

# 方块大小和颜色
BLOCK_SIZE = 30
BLACK = (0, 0, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
PURPLE = (128, 0, 128)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (255, 165, 0)

# 方块类型和对应的颜色
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, 0], [0, 1, 1]],
    [[0, 1, 1], [1, 1, 0]]
]
COLORS = [CYAN, YELLOW, PURPLE, GREEN, RED, BLUE, ORANGE]

def draw_block(screen, x, y, color):
    pygame.draw.rect(screen, color, (x, y, BLOCK_SIZE, BLOCK_SIZE))

def draw_board(screen, board):
    for i in range(len(board)):
        for j in range(len(board[0])):
            if board[i][j] != 0:
                draw_block(screen, j * BLOCK_SIZE, i * BLOCK_SIZE, COLORS[board[i][j] - 1])

def check_collision(board, shape, x, y):
    for i in range(len(shape)):
        for j in range(len(shape[0])):
            if shape[i][j] != 0:
                if x + j < 0 or x + j >= len(board[0]) or y + i >= len(board) or board[y + i][x + j] != 0:
                    return True
    return False

def merge_shape(board, shape, x, y):
    for i in range(len(shape)):
        for j in range(len(shape[0])):
            if shape[i][j] != 0:
                board[y + i][x + j] = shape[i][j]

def remove_completed_rows(board):
    completed_rows = []
    for i in range(len(board)):
        if all(board[i]):
            completed_rows.append(i)
    for row in completed_rows:
        del board[row]
        board.insert(0, [0] * len(board[0]))

def rotate_shape(shape):
    return list(zip(*reversed(shape)))

def generate_new_shape():
    shape = random.choice(SHAPES)
    return shape, WINDOW_WIDTH // 2 - len(shape[0]) // 2, 0

def main():
    pygame.init()
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption("俄罗斯方块游戏")
    clock = pygame.time.Clock()

    board = [[0] * (WINDOW_WIDTH // BLOCK_SIZE) for _ in range(WINDOW_HEIGHT // BLOCK_SIZE)]
    shape, x, y = generate_new_shape()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if not check_collision(board, shape, x - 1, y):
                        x -= 1
                elif event.key == pygame.K_RIGHT:
                    if not check_collision(board, shape, x + 1, y):
                        x += 1
                elif event.key == pygame.K_DOWN:
                    if not check_collision(board, shape, x, y + 1):
                        y += 1
                elif event.key == pygame.K_UP:
                    rotated_shape = rotate_shape(shape)
                    if not check_collision(board, rotated_shape, x, y):
                        shape = rotated_shape

        if not check_collision(board, shape, x, y + 1):
            y += 1
        else:
            merge_shape(board, shape, x, y)
            remove_completed_rows(board)
            shape, x, y = generate_new_shape()
            if check_collision(board, shape, x, y):
                pygame.quit()
                return

        screen.fill(BLACK)
        draw_board(screen, board)
        for i in range(len(shape)):
            for j in range(len(shape[0])):
                if shape[i][j] != 0:
                    draw_block(screen, (x + j) * BLOCK_SIZE, (y + i) * BLOCK_SIZE, COLORS[shape[i][j] - 1])
        pygame.display.flip()
        clock.tick(5)

if __name__ == '__main__':
    main()

这是一个基于pygame库的简单实现,可以在Python环境下运行

帮我生成一个俄罗斯方块游戏的代码

原文地址: https://www.cveoy.top/t/topic/h6jF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录