My program will not run, when instead of using a black background i try to use a image as background ERROR: pygame.surface' object is not callable

I tried to run this program, however this error comes up:

Traceback (most recent call last):
  File "C:/Python/ComputingCW/ComputingCW.py", line 12, in <module>
    Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32)
TypeError: 'pygame.Surface' object is not callable

It used to be:
Surface = pygame.display.set_mode((W, H), 0, 32) and then using “Surface.fill(BLACK)” to make a black background.

I don’t understand why won’t it allow me to use a image as my background. Can someone please help me, and explain why is this happening.

import pygame, sys, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
# all_fonts = pygame.font.get_fonts()


W = 1280
H = 900

Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32)
pygame.display.set_caption('Physics in Motion')

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

game = True
mouse_pos = (0, 0)
mouse_click = (0, 0)
text1B = False
text2B = False
text3B = False
output = ''

while game == True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        if event.type == MOUSEMOTION:
            mouse_pos = event.pos
        if event.type == MOUSEBUTTONUP:
            mouse_click = event.pos


    color = WHITE
    Font = pygame.font.SysFont('arial', 72)
    if text1B:
        color = RED
    text = Font.render('Start Simulation', True, color)
    text_rect = text.get_rect()
    text_rect.center = (W / 2, H / 5)
    if text_rect.collidepoint(mouse_click):
        output = 'Simulation()'
    if text_rect.collidepoint(mouse_pos):
        text1B = True
    else:
        text1B = False
    Surface.blit(text, text_rect)

    color = WHITE
    if text2B:
        color = RED

    Font = pygame.font.SysFont('arial', 72)
    text = Font.render('Graph Generator', True, color)
    text_rect = text.get_rect()
    text_rect.center = (W / 2, H * 2 / 5)
    if text_rect.collidepoint(mouse_click):
        output = 'GraphG()'
    if text_rect.collidepoint(mouse_pos):
        text2B = True
    else:
        text2B = False
    Surface.blit(text, text_rect)

    color = WHITE
    if text3B:
        color = RED

    Font = pygame.font.SysFont('arial', 72)
    text = Font.render('Exit', True, color)
    text_rect = text.get_rect()
    text_rect.center = (W / 2, H * 3 / 5)
    if text_rect.collidepoint(mouse_click):
        pygame.quit()
        sys.exit()
    if text_rect.collidepoint(mouse_pos):
        text3B = True
    else:
        text3B = False
    Surface.blit(text, text_rect)

    Font = pygame.font.SysFont('arial', 72)
    text = Font.render(output, True, BLUE)
    text_rect = text.get_rect()
    text_rect.center = (W / 2, H * 4 / 5)
    Surface.blit(text, text_rect)

    pygame.display.flip()
    mainClock.tick(100000)

3

Answers


  1. skrx
    0 Votes

    This line pygame.image.load('Pygame_Background.jpg') loads an image from your hard disk and turns it into a pygame.Surface object. Now you have this surface and try to call it like a function with parentheses behind it ((W, H), 0, 32) and that causes the TypeError, because a surface object isn’t a function (it’s not callable).

    What you actually want to do is:

    1. Create the display window (I call it screen here).

      screen = pygame.display.set_mode((W, H), 0, 32)
      
    2. Load the background image (better call it background because Surface is the name of the pygame.Surface class).

      background = pygame.image.load('Pygame_Background.jpg').convert()
      

      Also, call the pygame.Surface.convert or convert_alpha method to improve the performance.

    3. Blit the background and everything else onto the screen in the main while loop.

      screen.blit(background, (0, 0))
      screen.blit(text, text_rect)
      
  2. Baka Baka
    0 Votes

    Try using the absolute path of the file of 'Pygame_Background.jpg'

    Trace your directory starting from your Drive’s letter

  3. Chun-Yen Wang
    0 Votes

    This looks like a name conflict. pygame.Surface is a module (or a sub module), but this line seems to overwrite this Surface object:

    Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32)
    

    I would try to use different variable in the left hand side, for example surf_bg.

Please signup or login to answer this question.

Signup for an account and start participating in our site today!




Social Signup

You can also login with your Facebook, Twitter or Linkedin account

Log in with Facebook