Python Forum

Full Version: [pyGame] Load Image, Problem Found !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Programmers...

I Try to load an image in pygame... This is my script:

import pygame
import sys
 
screen = pygame.display.set_mode((800,600))
done = False
  
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True
                
    pygame.image.load(os.path.join('C:/Users/Gebruiker/Desktop/Renders', 'Render.png'))
    screen.fill((255,255,255))
    pygame.display.update()
pygame.quit()
sys.exit()

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            screen.fill((255,000,000))
            pygame.display.update()
If i run this script... the display screen of my game (simple test) is black and the image
load slowly.... the image is not so big, but my game (or simple test) are craching...

Can anyone help me ?...

Thanks for help, Jamie...
You load the image over and over.  Why not just do it once, outside the while loop?  That should speed it up.

As for the screen being black... what do you think screen.fill() does?  It... fills the entire screen with a solid color.
  • nothing should reside after pygame.quit() and sys.exit(). That is the end of the game.
  • you should only have one event loop, not two
  • you should only have one game loop, you should only have to type pygame.display.update() once in the whole game...even if it as big as minecraft. 
  • never load your images in your game loop as well as .convert() them to make it faster
  • You should probably make a package and put the image in a sub-directory of the root python script, not on your directory in your desktop. 
import pygame
import sys
  
screen = pygame.display.set_mode((800,600))
done = False
img = pygame.image.load(os.path.join('C:/Users/Gebruiker/Desktop/Renders', 'Render.png')).convert()
   
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True
            elif event.key == pygame.K_SPACE:
                pass #do something
                 
    
    screen.fill((255,255,255))
    pygame.display.update()
pygame.quit()
sys.exit()