Search code examples
pythoncompiler-errorspygame

TypeError: argument 1 must be pygame.surface.Surface, not str


I keep getting this error message when trying to run my code in pygame, it allows me to open to window but when I try to input a direction key it gives me this error code.

in redraw_game_window
    win.blit(walk_right[walkcount//3], (x, y))
TypeError: argument 1 must be pygame.surface.Surface, not str

it is giving the error for the code win.blit(walk_right[walkcount//3], (x, y)) which is under the elif right statement, but I imagine that it will have the same issue under the if left statement since they are identical

def redraw_game_window():
    global walkcount
    #win.blit(bg, (0, 0))
    win.fill(GREY)

    if walkcount + 1 >= 60:
        walkcount = 0

    if left:
        win.blit(walk_left[walkcount//3], (x, y))
        walkcount += 1
    elif right:
        win.blit(walk_right[walkcount//3], (x, y))
        walkcount += 1
    else:
        win.blit(idle_down, (x, y))

    pygame.display.update

in a other project I have practically identical code and it runs perfectly fine, I've compared the two projects to see any differences but I can not find a solution, I am newer to pygame and coding in general so I appreciate any help I can get.

the definition of walk left is the sprite animation for walking left, and it is all one line.

walk_left = [pygame.image.load('art/Knight/move/KnightL1.png'), ('art/Knight/move/KnightL2.png'), ('art/Knight/move/KnightL3.png'), ('art/Knight/move/KnightL4.png')]

Solution

  • The error is here:

    walk_left = [pygame.image.load('art/Knight/move/KnightL1.png'), ('art/Knight/move/KnightL2.png'), ('art/Knight/move/KnightL3.png'), ('art/Knight/move/KnightL4.png')]
    

    Only the first item is a call to pygame.image.load, the other items in your list are just strings.

    If pygame.image.load('art/Knight/move/KnightL1.png') is how you load an image, you have to do this for each item in your list, like so:

    walk_left = [pygame.image.load('art/Knight/move/KnightL1.png'), pygame.image.load('art/Knight/move/KnightL2.png'), pygame.image.load('art/Knight/move/KnightL3.png'), pygame.image.load('art/Knight/move/KnightL4.png')]