pythonnumpypygametetris

Setting numpy zeros elements with RGB values


I'm currently trying to write tetris using pygame.

I have a numpy zeros array which acts as the tetris window and its contents. Since it starts out as zeros, there is nothing on the window. When a block is placed at the bottom the matrix is then updated with the RGB values in the place of those zeros. I would then loop through this matrix to draw the colors of each square onto the game window.

I have the code written, however get this error in my matrixUpdate method:

ValueError: setting an array element with a sequence.

Here is my code that pertains to the question:

class Tetrimino(pygame.sprite.Sprite):
    def __init__(self, mino, color):
        pygame.sprite.Sprite.__init__(self)
        self.setMino(mino)
        self.color = color
        self.x = 160
        self.y = 160
        self.r = 0
        self.changey = 0
        self.changex = 0

    # Update the matrix of RGB values's
    def matrixUpdate(self, matrix):
        for part in self.getMino():
            matrix[((part[1] + self.y) // 40), ((part[0] + self.x) // 40)] = [self.color]

        return matrix

matrix = np.zeros((24, 10))

Solution

  • Looks like Numpy doesn't like putting different types in an array, unless you create it like this:

    matrix = np.zeros((24, 10), dtype=object)