PyGame: pygame.image.load()不显示任何内容
我正在做一个关于pyGame的作业,这个作业非常简单,我们需要在窗口中加载一张图片,但图片就是不加载!而且IDLE没有显示任何错误信息。我尝试了相对路径来引用我的图片(room.png),也尝试了绝对路径(C:...\room.png),但都没有用。以下是我的代码。
import pygame, sys #import pygame and system library
from pygame import * #import all pygame sublibs
pygame.init()
screen = display.set_mode((385,384)) #set screen size
display.set_caption("Blit example")
#this one gets the current working directory
background_file_name = "room.png" #import bg
background_surface = pygame.image.load(background_file_name) #use bg
while True:
for e in pygame.event.get():
if e.type==QUIT: #break the loop and quit
pygame.quit()
sys.exit()
break
screen.fill((255,0,255)) #fill the screen with magenta
screen.blit(background_surface, (0,0))
display.update()
2 个回答
1
因为你在评论中提到你对编程完全不了解,所以你可能不知道在Python中缩进是很重要的。你的while循环一直在运行,等待一个退出的信号。但是显示的内容只有在那之后才会执行...
while True:
for e in pygame.event.get():
if e.type==QUIT: #break the loop and quit
pygame.quit()
sys.exit()
break
screen.fill((255,0,255)) #fill the screen with magenta
screen.blit(background_surface, (0,0))
display.update()
现在显示的代码是在while循环里面的
1
你的缩进有问题。
你只有在应用程序结束后才开始绘图。
试着把绘图的代码放到你的主循环里。