Question

I'm quite new to Pygame and Python and I've just made one of my first codes but somehow I keep getting this error:

TypeError: 'pygame.Surface' object is not callable

I don't know whether there is something wrong in the code or just because Pygame/Python isn't installed correctly.

bif="bg.jpg"
mif="ball.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,360),0,32)

background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

while True:
   for event in pygame.event.get():
     if event.type == QUIT:
        pygame.quit()
        sys.exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2

screen.blit(mouse_c(x,y))

pygame.display.update()

After running this code the pygame window crashes.

Was it helpful?

Solution

You are missing a comma:

screen.blit(mouse_c(x,y))

should be

screen.blit(mouse_c, (x,y))
                 # ^

In the first version, mouse_c(x, y) is interpreted as an attempt to call mouse_c (which is a pygame.Surface and thus not callable) with arguments x and y, when they are in fact separate arguments (source and dest) to screen.blit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top