尝试使用 pygame.display.update 在 pygame 中显示 png 文件,它显示不到一秒钟然后消失. [英] Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.
问题描述
图片是一张扑克牌.我们使用的是 pygame 4.5 社区版和 pycharm 2.6.9,因为 2.7 不支持 pygame(这是一所学校).代码如下:
The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
pygame.display.update()
为什么窗口消失了?
推荐答案
问题是,在你用 pygame.display.update()
更新屏幕后,你什么都不做,你的程序只是结束.pygame.display.update()
不会阻塞.
The problem is, after you update the screen with pygame.display.update()
, you do nothing, and your program simply ends. pygame.display.update()
does not block.
您需要通常称为主循环的东西.这是一个简单的事件处理示例:
You need what is usually called a main loop. Here's a simple example with event handling:
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
# display.set_mode already returns the screen surface
screen = pygame.display.set_mode(picture.get_size())
# a simple flag to show if the application is running
# there are other ways to do this, of course
running = True
while running:
# it's important to get all events from the
# event queue; otherwise it may get stuck
for e in pygame.event.get():
# if there's a QUIT event (someone wants to close the window)
# then set the running flag to False so the while loop ends
if e.type == pygame.QUIT:
running = False
# draw stuff
screen.blit(picture, (0,0))
pygame.display.update()
这样,您的应用程序就不会,只有在有人关闭窗口时才会这样做.
This way, your application does not, only when someone closes the window.
这篇关于尝试使用 pygame.display.update 在 pygame 中显示 png 文件,它显示不到一秒钟然后消失.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!