pythonpygametetris

Tetris generating coloured shapes from blocks


I'm making Tetris with Pygame. I have designed the blocks separately (a block in every color) so I need to let the game generate all of the blocks' shapes in all colors. I mean I want it to generate for example a shape composed of 4 red blocks in a row or a blue-coloured shape that looks like the letter L from those independent blocks... Then I would create a list in which I would store all these shapes to randomly generate them after.

And these are ones of those independent blocks:

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')

So from these, i want to make tetris shapes in pygame to use them directly ingame without photoshop or any external software blue block green blocks purple block red block yellow block

Resume : enter image description here --->>> enter image description here (This is only a possible case)


Solution

  • Tetris tiles are arranged in a grid. Define a list of shapes. Each shape is a list. The list contains tuples specifying the column and row of each tile that makes up the shape:

    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
    ]
    

    Create a function that can be used to draw a single shape of the list:

    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))
    

    Complete example:

    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()