在0x00000234D43C68B8>处涉及lambda给定代码<function main.<locals><lambda>的错误

2024-09-27 21:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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

Tags: thelambdainforreturnifobjectmain
1条回答
网友
1楼 · 发布于 2024-09-27 21:27:36

你把lambdas加进去了

(convCoordsInserterX, convCoordsInserterY, 20, 20)

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

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

或者你应该跳过lambdas让它变短

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

相关问题 更多 >

    热门问题