Narin Chauhan
Narin Chauhan

Reputation: 21

My Pygame image not loading

I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame. I saved a png image in the same folder as my py script. When I run the script it shows no error but my image does not shows in pygame window. Kindly advice

Here's the script

import pygame,sys

pygame.init()
WIDTH,HEIGHT = 640,360

screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)

clock = pygame.time.Clock()
FPS = 24
dog_img = pygame.image.load("dog.png")

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

#PROCESS
#LOGIC         
#LOGIC                   
#DRAW
screen.blit(dog_img,(0,0))

pygame.display.flip()

#DRAW
clock.tick(FPS)

Upvotes: 2

Views: 281

Answers (1)

skrx
skrx

Reputation: 20438

Fix the indentation. screen.blit(dog_img, (0, 0)) and the two lines below should be inside of the while loop (indented with 4 spaces).

import pygame,sys

pygame.init()
WIDTH, HEIGHT = 640, 360

screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)

clock = pygame.time.Clock()
FPS = 24
# Always use `.convert()` or `.convert_alpha()`. It'll improve the performance.
dog_img = pygame.image.load("dog.png").convert_alpha()

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

    #LOGIC
    #DRAW
    screen.blit(dog_img, (0, 0))

    pygame.display.flip()

    clock.tick(FPS)

Upvotes: 3

Related Questions