Python Forum
[pyGame] Load Image, Problem Found !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyGame] Load Image, Problem Found !
#1
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...
Reply
#2
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.
Reply
#3
  • 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()
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with pygame.event.clear qq12346 1 2,119 Oct-05-2023, 08:39 AM
Last Post: patriciainman
  pygame double jump problem Yegor123 3 2,590 May-02-2023, 09:34 PM
Last Post: sudoku6
  Pygame Zero - no actor image toiling_away 2 1,807 Oct-14-2022, 12:16 AM
Last Post: toiling_away
  [PyGame] Sprite image rotation problem XavierPlatinum 4 2,319 Jul-25-2022, 01:31 PM
Last Post: Ruslan8819
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,311 Nov-10-2021, 03:38 PM
Last Post: metulburr
  (HELP GREATLY APPRECIATED) New user- Huge Pygame Installation Problem! Jbomb 1 2,836 Jan-12-2021, 07:32 PM
Last Post: MK_CodingSpace
  problem with pygame Aladdin 3 4,223 Jun-25-2020, 01:41 PM
Last Post: Aladdin
  pygame module not found on Idle after installing on Mac crunchypen 1 3,534 May-09-2020, 11:03 PM
Last Post: metulburr
  Pycharm dosnt load pygame Matres 1 1,969 Mar-16-2020, 02:15 AM
Last Post: Larz60+
  open cv ->pygame: turn image into location Gamedeveloper 1 2,068 Jan-20-2020, 05:00 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020