I am in trouble because I cannot display the image with image.load("") on python pygame.

Asked 1 years ago, Updated 1 years ago, 58 views

I am currently studying python programming to enjoy learning while making games.

I am troubled that I cannot do image.load("") in this document.
I would like to enter the following code (pythonlogo.jpg), but for some reason, I only see a black interface screen called pygame window, but I can't see any images at all.

I thought the location of the executable file and the image was wrong, so I tried putting the executable file and the image (pythonlogo.jpg) in the same hierarchy, but it didn't work. I started 3.6.3 Shell with "idle" from the command prompt, opened File(Run.py) and tried with Run module f5, but nothing came up.

Is this the wrong way or is the file path wrong?

import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE=pygame.display.set_mode(400,300))
FPSCLOCK=pygame.time.Clock() 
defmain():
    logo=pygame.image.load("pythonlogo.jpg")
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
    SURFACE.fill((255,255,255))
    SURFACE.blit(logo, (20,50))
    pygame.display.update()
    FPSCLOCK.tick(30) 
if__name__=='__main__':
    main() 

Windows This is a Windows environment.

I look forward to your kind cooperation.

python image pygame

2022-09-29 21:55

2 Answers

(It may be a misstatement, such as a new line is wrong)
logo must be displayed before entering the event loop.

Specifically

logo=pygame.image.load("pythonlogo.jpg")
   while True:
       for event in pygame.event.get():
           if event.type==QUIT:
               pygame.quit()
               sys.exit()
   SURFACE.fill((255,255,255))
   SURFACE.blit(logo, (20,50))
   pygame.display.update()

not in the order shown in , but in the order shown in .

logo=pygame.image.load("pythonlogo.jpg")
   while True:
       SURFACE.fill((255,255,255))
       SURFACE.blit(logo, (20,50))
       pygame.display.update()
       for event in pygame.event.get():
           if event.type==QUIT:
               pygame.quit()
               sys.exit()

I think it's in order.


2022-09-29 21:55

pygame.display.update()
FPSCLOCK.tick(30)

The two lines of the are

while True:

If you do not have
The initial display is a black screen.
However, once you minimize it, it should be displayed properly.


2022-09-29 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.