.在 0x00000234D43C68B8>,python"/>

首页 > 解决方案 > 涉及 lambda 给出代码的错误.在 0x00000234D43C68B8>

问题描述

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

标签: python

解决方案


您将 lambda 附加到

(convCoordsInserterX, convCoordsInserterY, 20, 20)

但你应该运行 lambdas - using ()- 并附加结果

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

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

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

推荐阅读