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

问题描述

我正在用 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 或任何外部软件的情况下直接在游戏中使用它们

blue block

green blocks

purple block

red block

yellow block

简历:

enter image description here

--->>>

enter image description here

(这只是一种可能的情况)

解决方法

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

shapes = [
    [(0,0),(1,(2,(3,0)],[(0,(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,# [...] add more
]

def draw_shape(x,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,red_tile)
    draw_shape(170,1,blue_tile)
    draw_shape(70,170,2,green_tile)
    draw_shape(170,3,yellow_tile)
    pygame.display.flip()

pygame.quit()
exit()