涉及 lambda 在<locals><lambda> 0x00000234D43C68B8> 处提供代码<函数 main.. 的错误



我正在用python(使用pygame(制作一个贪吃蛇游戏,但是在绘制尾巴时,出现了错误。地图比屏幕大,所以我创建了一个坐标转换功能(输入游戏坐标并获取屏幕坐标(,但一旦我移动蛇/更新尾巴位置列表,该功能就会中断。

我完全不知道发生了什么,所以我不知道该尝试什么。


# Define convCoords. Converts game coordinates to screen coordinates for 20x20px squares.
def convCoords(mapX, mapY, mode):
screenX = ((camX - 15) - mapX) * -20
screenY = ((camY - 11) - mapY) * -20
if mode == 1:  # If mode = 1, return both X and Y
return screenX, screenY
elif mode == 2:  # If mode = 2, return only X
return screenX
elif mode == 3:  # If mode = 3, return only Y
return screenY
else:
raise IndexError("Use numbers 1-3 for mode")  # Raise an error if a number other than 1-3 is given as mode
def main():
stuff()
if snakeMoveTimer >= speed:
# Move the tail forward
convCoordsInserterX = lambda: convCoords(camX, 0, 2)
convCoordsInserterY = lambda: convCoords(0, camY, 3)
list.append(tailLocations, (convCoordsInserterX, 
convCoordsInserterY, 20, 20))
if not appleEaten:
list.pop(tailLocations, 0)
else:
appleEaten = False
furtherStuff()
# Draw the tail
for object in tailLocations:
print(object)
pygame.draw.rect(gameDisplay, GREEN, object)
# Increase the timer
snakeMoveTimer += 1

打印输出 (240, 220, 20, 20( (260, 220, 20, 20((280, 220, 20, 20( 直到输出时更新:

(<function main.<locals>.<lambda> at 0x00000234D43C68B8>, <function main.<locals>.<lambda> at 0x00000234D43CB048>, 20, 20)
Traceback (most recent call last):
File "C:/Users/Me/Python/Snake/snake.py", line 285, in <module>
main()
File "C:/Users/Me/Python/Snake/snake.py", line 255, in main
pygame.draw.rect(gameDisplay, GREEN, object)
TypeError: Rect argument is invalid

你在

(convCoordsInserterX, convCoordsInserterY, 20, 20)

但是您应该运行 lambda - 使用()- 并附加结果

(convCoordsInserterX(), convCoordsInserterY(), 20, 20)

或者你应该简单地跳过 lambda 以使其更短

(convCoords(camX, 0, 2), convCoords(0, camY, 3), 20, 20)

最新更新