Applying a Background Image to Pygame

I am having difficulties setting a background image for my Pygame window. I’ve looked for other sources, but it seems I cannot find anything helpful or useful.
I am currently aiming to make a 2D Platformer, but I want to start with the background first.
Here is my code:

import pygame, sys
from pygame.locals import QUIT
pygame.init()

DISPLAYSURF = pygame.display.set_mode((500, 500))

#background
background = pygame.load.image("GRID.jpg")
background = pygame.transform.scale("GRID.jpg",(500,500))

# caption and icon
pygame.display.set_caption('pallor TEST')
icon = pygame.image.load("char.png")
pygame.display.set_icon(icon)
# Displays it?
pygame.display.flip()

runing = True
while runing:
    DISPLAYSURF.blit("GRID.jpg",(0,0))
    for event in pygame.event.get():
        if event.type == QUIT:
            runing = False
    pygame.display.update()
pygame.quit()

I keep getting:
Screenshot 2023-03-21 12.26.01 PM

Could someone please help me?
I don’t know what is wrong.

pygame.load.image does not exist. Perhaps you were looking for pygame.image.load?pygame.image — pygame v2.4.0 documentation

3 Likes

It pygame.image.load not pygame.load.image. Check the docs, they are really helpful :slight_smile:

3 Likes

I fixed it just now, thank you.
However, I am still having issues.
Screenshot 2023-03-21 12.31.08 PM

background = pygame.transform.scale(background,(500,500))
Instead of

1 Like

The first argument for pygame.transform.scale should be a pygame.Surface, not a string.
The function you used earlier, pygame.load.image, returns a pygame.Surface instance, hence you can rewrite your code like this:

background = pygame.image.load("GRID.jpg")
background = pygame.transform.scale(background,(500,500))

or do it as a one-liner, like so:

background = pygame.transform.scale(pygame.image.load("GRID.jpg"), (500, 500))
2 Likes

It seems to work in terms of the area, but I still seem to have an issue in displaying it.
Screenshot 2023-03-21 1.24.22 PM
Do I need to change the function, or did I typo something?
Or do I need to set the image into a folder and use it as a path?

It’s the same problem as above. Try using DISPLAYSURF.blit(background, (0, 0)) instead

3 Likes

I really recommend looking at the docs in the future, it will tell you exactly what type goes there. It will tell you it takes surface instead of string. And even if it did take string, it would be a waste to load it every time.

2 Likes

Yeah for sure, I was looking through it and found some really useful stuff.

1 Like

It WORKED! Thank you and dragonhunter1.

1 Like

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.