0

I started learning Pygame today and tried out this code but the image won't load and it shows no error, even tried changing the dimensions of the window and the position of the image, nothing seems to be working.

import pygame, sys

clock = pygame.time.Clock()

from pygame.locals import *
pygame.init() #iniiates pygame

pygame.display.set_caption('My Program Window')

WINDOW_SIZE = (400,400)

screen = pygame.display.set_mode(WINDOW_SIZE,0,32) #initiate the window

player_image = pygame.image.load('player.png')

moving_right = False
moving_left = False

player_location = [50,50]

while True:

    screen.blit(player_image, player_location)
    if moving_right == True:
        player_location[0] += 4
    if moving_left == True:
        player_location[0] -= 4

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

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True

        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False


    pygame.display.update()
    clock.tick(60)

[Edit]: Solved the problem by reinstalling Pygame by following this thread: Problems getting pygame to show anything but a blank screen on Macos Mojave

1 Answers1

1

When I run your code it works (though I had to provide my own image obviously). Are you sure that the image you loading has something in it?

On different note. In your code you set

screen = pygame.display.set_mode(WINDOW_SIZE,0,32) #initiate the window

It is unusual to need to set the color depth.

From the docs on pygame.display.set_mode()

It is usually best to not pass the depth argument. It will default to the best and fastest color depth for the system. If your game requires a specific color format you can control the depth with this argument. Pygame will emulate an unavailable color depth which can be slow.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
  • Solved the problem by reinstalling Pygame by following this thread: https://stackoverflow.com/questions/52718921/problems-getting-pygame-to-show-anything-but-a-blank-screen-on-macos-mojave – UnwantedTachyon May 23 '20 at 18:35