俄罗斯方块从方块生成彩色形状

Tetris generating coloured shapes from blocks

我正在用 Pygame 制作俄罗斯方块。我单独设计了方块(每种颜色的方块),所以我需要让游戏生成所有颜色的所有方块形状。我的意思是我希望它生成一个由连续 4 个红色块组成的形状或一个看起来像来自这些独立块的字母 L 的蓝色形状......然后我将创建一个列表,我将在其中存储所有这些形状之后随机生成它们。

这些是独立块中的一个:

red_block = pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\red.png')
blue_block = pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\blue.png')

所以根据这些,我想在 pygame 中制作俄罗斯方块形状,以便在没有 photoshop 或任何外部软件的情况下直接在游戏中使用它们

简历: --->>> (这只是一种可能的情况)

俄罗斯方块的方块排列成网格状。定义形状列表。每个形状都是一个列表。该列表包含指定构成形状的每个图块的列和行的元组:

shapes = [
    [(0, 0), (1, 0), (2, 0), (3, 0)],
    [(0, 0), (1, 0), (0, 1), (1, 1)],
    [(0, 0), (1, 0), (2, 0), (2, 1)],
    [(0, 0), (1, 0), (2, 0), (1, 1)],
    # [...] add more
]

创建一个可用于绘制列表的单个形状的函数:

def draw_shape(x, y, tile_index, surf):
    w, h = surf.get_size()
    for pos in shapes[tile_index]:
        screen.blit(surf, (x + pos[0]*w, y + pos[1]*h))

完整示例:

import pygame

pygame.init()
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

tile_size = 20
red_tile = pygame.Surface((tile_size, tile_size))
red_tile.fill("red")
blue_tile = pygame.Surface((tile_size, tile_size))
blue_tile.fill("blue")
green_tile = pygame.Surface((tile_size, tile_size))
green_tile.fill("green")
yellow_tile = pygame.Surface((tile_size, tile_size))
yellow_tile.fill("yellow")

shapes = [
    [(0, 0), (1, 0), (2, 0), (3, 0)],
    [(0, 0), (1, 0), (0, 1), (1, 1)],
    [(0, 0), (1, 0), (2, 0), (2, 1)],
    [(0, 0), (1, 0), (2, 0), (1, 1)],
    # [...] add more
]

def draw_shape(x, y, tile_index, surf):
    w, h = surf.get_size()
    for pos in shapes[tile_index]:
        screen.blit(surf, (x + pos[0]*w, y + pos[1]*h))

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    screen.fill(0)
    draw_shape(70, 70, 0, red_tile)
    draw_shape(170, 70, 1, blue_tile)
    draw_shape(70, 170, 2, green_tile)
    draw_shape(170, 170, 3, yellow_tile)
    pygame.display.flip()

pygame.quit()
exit()