问题描述
我目前正在尝试使用 pygame 编写俄罗斯方块。
我有一个 numpy zeros 数组,它充当俄罗斯方块窗口及其内容。由于它从零开始,因此窗口上没有任何内容。当一个块被放置在底部时,矩阵会用 RGB 值代替那些零进行更新。然后我将遍历这个矩阵以将每个方块的颜色绘制到游戏窗口上。
我已经编写了代码,但是在我的 matrixUpdate
方法中出现此错误:
ValueError: setting an array element with a sequence.
这是我与问题有关的代码:
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))
解决方法
看起来 Numpy 不喜欢将不同的类型放在一个数组中,除非你像这样创建它:
matrix = np.zeros((24,10),dtype=object)