Home > database >  Im trying to upload an image in pygame with blit method but the image doesn't show up on the sc
Im trying to upload an image in pygame with blit method but the image doesn't show up on the sc

Time:01-29

So I'm basically just trying to upload an image in pygame with blit but it doesn't show up on the screen. I'm watching a youtube tutorial on this and I can't make the image go up on the screen. I double checked that I downloaded the image.

import pygame

# Define the background colour
# using RGB color coding.
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Define the dimensions of
# screen object(width,height)
screen = pygame.display.set_mode((900, 500))

# Set the caption of the screen
pygame.display.set_caption('Space game')

# Fill the background colour to the screen
screen.fill(WHITE)
pygame.display.update()

# Update the display using flip
pygame.display.flip()

# Variable to keep our game loop running
running = True
#maximum the FPS can go up to
FPS = (60)
clock = pygame.time.Clock()
#loading the spaceship images
YELLOW_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_yellow.png')
RED_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_red.png')

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE(300, 100))
# game loop
while running:
#telling the game to run only at 60 FPS
    clock.tick(FPS)

    # for loop through the event queue
    for event in pygame.event.get():

        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False

CodePudding user response:

Modifying the draw_window function

The blit function takes two non optional parameters: the image that you want to display on the screen and the coordinates of that image. In your code, I see that you forgot to split the two parameters with a comma.

So change this:

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE(300, 100))

To this:

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE, (300, 100))

Modifying the game loop

In the game loop (the while running loop), you aren't calling the draw_window function. In addition, you aren't updating the display. If you apply these changes, your sprite will be drawn on the screen as expected.

Another change you need to make:

Below your event loop, you will need to add the following lines:

pygame.quit()
sys.exit(0)

You have to add this because when running is False, you need the program to stop running. Don't forget to import the sys module, like this: import sys

Modified game loop:

# game loop
while running:
    # Fill the background colour to the screen
    screen.fill(WHITE)
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False
            
    draw_window()
    #telling the game to run only at 60 FPS
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()
sys.exit(0)

Full Code

import pygame
import sys
pygame.init()

# Define the background colour
# using RGB color coding.
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Define the dimensions of
# screen object(width,height)
screen = pygame.display.set_mode((900, 500))

# Set the caption of the screen
pygame.display.set_caption('Space game')

# Variable to keep our game loop running
running = True
#maximum the FPS can go up to
FPS = 60
clock = pygame.time.Clock()
#loading the spaceship images
YELLOW_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_yellow.png')
RED_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_red.png')

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE, (300, 100))

# game loop
while running:
    # Fill the background colour to the screen
    screen.fill(WHITE)
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False

    draw_window()
    #telling the game to run only at 60 FPS
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()
sys.exit(0)
  •  Tags:  
  • Related