我正在用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)(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
答案 0 :(得分:0)
您在
中附加了lambda。(convCoordsInserterX, convCoordsInserterY, 20, 20)
但是您应该运行lambdas-使用()
-并附加结果
(convCoordsInserterX(), convCoordsInserterY(), 20, 20)
或者您应该简单地跳过lambda以使其更短
(convCoords(camX, 0, 2), convCoords(0, camY, 3), 20, 20)