尝试使用 pygame.display.update 在 pygame 中显示 png 文件,它显示不到一秒钟然后消失。
Posted
技术标签:
【中文标题】尝试使用 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. 【发布时间】:2015-10-10 10:10:04 【问题描述】:图片是一张扑克牌。我们使用的是 pygame 4.5 社区版和 pycharm 2.6.9,因为 2.7 不支持 pygame(这是一个学校)。代码如下:
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()
为什么窗口消失了?
【问题讨论】:
【参考方案1】:试试这个:
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))
while True:
main_surface.blit(picture, (0,0))
pygame.display.update()
pygame.display.update() 更新一帧。根据您在表面上绘制的内容,每秒有多个帧。
【讨论】:
【参考方案2】:问题是,在你用pygame.display.update()
更新屏幕后,你什么都不做,你的程序就结束了。 pygame.display.update()
不会阻止。
您需要通常称为主循环的东西。下面是一个简单的事件处理示例:
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()
这样,您的应用程序不会,只有当有人关闭窗口时。
【讨论】:
以上是关于尝试使用 pygame.display.update 在 pygame 中显示 png 文件,它显示不到一秒钟然后消失。的主要内容,如果未能解决你的问题,请参考以下文章
当我尝试使用 ansible ping 我的 VM 时,它会尝试 ping 服务器 ID 而不是 IP