1

I am making a snake game in python (using pygame) but when drawing the tail, the error comes up. The map is bigger than the screen so I created a coordinate conversion function (put in game coordinates and get out screen coordinates) but that breaks as soon as I move the snake/update the tail location list.

I have absolutely no idea what is going on so I have no idea what to try.


# 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

The print prints out (240, 220, 20, 20) (260, 220, 20, 20) (280, 220, 20, 20) until it updates when it outputs:

(<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
Olliroxx
  • 111
  • 1
  • 6
  • Why is that unexpected? You appended those two lambdas to the list explicitly (in a weird way; use `foo.append("bar")`, not `list.append(foo, "bar")`). – jonrsharpe Sep 30 '19 at 17:57
  • What does `print(object)` print? Do your arguments conform to [the docs](https://www.pygame.org/docs/ref/draw.html#pygame.draw.rect)? – wwii Sep 30 '19 at 18:05
  • you append lambda `convCoordsInserterX, convCoordsInserterY` to list but you should use `()` to run lambdas and append results from these lambdas `(convCoordsInserterX(), convCoordsInserterY(), 20, 20)`. OR maybe you should simply skip lambdas - `(convCoords(camX, 0, 2), convCoords(0, camY, 3), 20, 20)` – furas Sep 30 '19 at 19:02
  • if i have the error `FullOmniglot.__init__..` does this mean the function `__init__` in the class above has some local variable that is a lambda function? – Charlie Parker Oct 27 '22 at 23:04

1 Answers1

0

You append lambdas in

(convCoordsInserterX, convCoordsInserterY, 20, 20)

but you should run lambdas - using () - and append results

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

or you should simply skip lambdas to make it shorter

(convCoords(camX, 0, 2), convCoords(0, camY, 3), 20, 20)
furas
  • 134,197
  • 12
  • 106
  • 148
  • if i have the error `FullOmniglot.__init__..` does this mean the function `__init__` in the class above has some local variable that is a lambda function? – Charlie Parker Oct 27 '22 at 23:04
  • @CharlieParker I never met this type of error. Better create question on new page and put your code and FULL error message. – furas Oct 28 '22 at 12:31
  • @CharlieParker searching `__init__..` in Google I found it means you have local variable with `lambda` function in `__init__` - but error may happen when you want to `pickle` data because `pickle` can't save `lambda`. See [python - AttributeError: Can't pickle local object 'pre\_datasets..' when implementing Pytorch framework - Stack Overflow](https://stackoverflow.com/questions/68679806/attributeerror-cant-pickle-local-object-pre-datasets-locals-lambda-when) – furas Oct 28 '22 at 12:36