Free Friday 3/13/26

Today in class, I started making a tetris game.

This is the code for the display surface

screen = pygame.display.set_mode((300, 600)) 
pygame.display.set_caption("Python Tetris")

This is the code for the grid.

class Grid:
    def __init__(self):
        self.num_rows = 20
        self.num_cols = 10
        self.cell_size = 30
        self.grid = [[0 for j in range(self.num_cols)] for i in range(self.num_rows)]
        self.colors = Colors.get_cell_colors()

    def print_grid(self):
        for row in range(self.num_rows):
            for column in range(self.num_cols):
                print(self.grid[row][column], end = " ")
            print()        
    
   
    
    def draw(self, screen):
        for row in range(self.num_rows):
            for column in range(self.num_cols):
                cell_value = self.grid[row][column]
                cell_rect = pygame.Rect(column*self.cell_size +1, row*self.cell_size +1,
                self.cell_size -1, self.cell_size -1)
                pygame.draw.rect(screen, self.colors[cell_value], cell_rect)

I started making the blocks, but I had a error in the colors.

This is the code for the colors.

class Colors:
    dark_grey = (26, 31, 40)
    green = (47, 230, 23)
    red = (232, 18, 18)
    orange = (226, 116, 17)
    yellow = (237, 234, 4)
    purple = (166, 0, 247)
    cyan = (21, 204, 209)
    blue = (13, 64, 216)
@classmethod
def get_cell_colors(cls):
    return [cls.dark_grey, cls.green, cls.red, cls.orange, cls.yellow, cls.purple, cls.cyan, cls.blue]

This is the code for the blocks.
class LBlock (Block):
    def __init__(self):
        super().__init__(id = 1)
        self.cells = {
            0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2),],
            1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2),],
            2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0),],
            3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1),]
        }

Link:https://www.youtube.com/watch?v=nF_crEtmpBo

CATEGORIES:

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *