Python Forum
I have an error and I do not know why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have an error and I do not know why
#1
Hello,
i am trying to make a platformer game but every time I press a key the following error message:

File "D:\python project\pygametest\pygametest.py", line 39, in redrawwindow
game_window.blit(walkleft(walkcount//3),(x,y))
TypeError: 'pygame.Surface' object is not callable

Here is my code if it helps.

import pygame
import sys
from pygame.constants import K_RIGHT

pygame.init()

screen_width = 800
screen_hight = 600

game_window = pygame.display.set_mode((screen_width, screen_hight))
pygame.display.set_caption("Window")

walkleft = (pygame.image.load("D:\python project\pygametest\idle.png"))
walkright = (pygame.image.load("D:\python project\pygametest\idleflip.png"))
idle = (pygame.image.load("D:\python project\pygametest\eidle.png"))
bg = (pygame.image.load("D:\python project\pygametest\ibg.png"))

clock = pygame.time.Clock()

x = 368
y = 268
width = 64
hight = 64
vel = 5
is_jump = False
jumpcount = 10
left = False
right = False
walkcount = 0

def redrawwindow():
    global walkcount

    game_window.blit(bg, (0,0))
    if walkcount >= 27:
        walkcount = 0

    if left:
        game_window.blit(walkleft(walkcount//3),(x,y))
        walkcount += 1
    elif right:
        game_window.blit(walkright(walkcount//3),(x,y))
        walkcount += 1
    else:
        game_window.blit(idle, (x,y))

    pygame.display.update()
    

game_running = True

while game_running:
    clock.tick(30)
    
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel:
            x -= vel
            right = False
            left = True
    elif keys[pygame.K_RIGHT] and x < screen_width - width - vel:
            x += vel
            right = True
            left = False
    else:
        right = False
        left = False
        walkcount = 0

    if not (is_jump):
        if keys[pygame.K_SPACE]:
            is_jump = True
            right = False
            left = False
            walkcount = 0

    else:
        if jumpcount >= -10:
            neg = 1
            if jumpcount < 0:
                neg = -1
            y -=(jumpcount ** 2) / 4 * neg
            jumpcount -= 1

        else:
            is_jump= False
            jumpcount = 10

    redrawwindow()





pygame.quit()
sys.exit()
Reply
#2
This is a function call.
walkleft(walkcount//3)
It is a function call because there is a word (walkleft) followed by parenthesis (). The "walkcount//3" is an argument provided to the function call.
But according to this code, walkleft is is an image, not a function.
walkleft = (pygame.image.load("D:\python project\pygametest\idle.png"))  # Why the extra ()?
There is also no place in your code where you define a function named "walkleft".

What are you trying to do with this bit of code?
    if left:
        game_window.blit(walkleft(walkcount//3),(x,y))
        walkcount += 1
    elif right:
        game_window.blit(walkright(walkcount//3),(x,y))
        walkcount += 1
    else:
        game_window.blit(idle, (x,y))
Reply
#3
I want to add sprites to my game. At first I wanted to do multipel frames in one animation but i got lazy and did not make additional frames.

if left:
    game_window.blit(walkleft(walkcount//3),(x,y))
    walkcount += 1
elif right:
    game_window.blit(walkright(walkcount//3),(x,y))
    walkcount += 1
else:
    game_window.blit(idle, (x,y))
Reply
#4
Was walkleft supposed to be a list of images? If so, you would use square brackets to index the list instead of parenthesis. Do you understand why you are getting the error now?
Reply
#5
Yes, thank you
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020