【问题标题】:Error on assigning a value to a Rectangle in Pygame在 Pygame 中为 Rectangle 赋值时出错
【发布时间】:2013-12-21 08:15:40
【问题描述】:

我试图创建一个在屏幕上绘制矩形地图的函数。这个函数是:

def parseMap(mapIndex):
    tileRect = pygame.Rect(0, 0, 32, 32)
    for x in maps.mapData[mapIndex]:
    tileRect.x = x*16
    for y in x:
        tileRect.y = y*16
        c.blit(maps.grass, tileRect)
        if maps.mapData[mapIndex][x][y] == 1: c.blit(maps.tallGrass, tileRect)

虽然我遇到了错误,

TypeError: invalid rect assignment

上线:

tileRect.x = x*16

我看不出代码有什么问题。谢谢!

【问题讨论】:

  • x 是什么? (maps.mapData[...] 中有什么?)在下一行你遍历它(for y in x:),它可能是一个列表吗?
  • 你确定你有错误并且你的缩进是正确的吗?我运行了相同的代码,没有错误。
  • @Seth maps.mapData 是一个 3 维列表,其中 [mapIndex] 是地图,然后它有坐标 [x] 和 [y]。
  • @Seth 天哪,我不敢相信我没有意识到这一点!非常感谢!

标签: python pygame typeerror rect


【解决方案1】:

如果x 是一个列表,那么x * 16 会列出重复,而不是乘法。

也就是说,你会得到类似

的东西
tileRect.x = [1, 1, 1, 1, ...]

【讨论】:

  • 已将评论移至回答。
猜你喜欢
  • 2019-11-22
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多