Python Forum
[pyGame] Load Image, Problem Found ! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [pyGame] Load Image, Problem Found ! (/thread-5351.html)



[pyGame] Load Image, Problem Found ! - JamieVanCadsand - Sep-29-2017

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...


RE: [pyGame] Load Image, Problem Found ! - nilamo - Sep-29-2017

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.


RE: [pyGame] Load Image, Problem Found ! - metulburr - Sep-29-2017

  • 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()