本文介绍了为什么我的 PyGame 应用程序根本没有运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的 Pygame 程序:
#!/usr/bin/env python导入pygame从 pygame.locals 导入 *pygame.init()赢 = pygame.display.set_mode((400,400))pygame.display.set_caption(我的第一场比赛")
但是每次我尝试运行它时,我都会得到这个:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)来自 pygame 社区的您好.https://www.pygame.org/contribute.html
然后什么也没发生.为什么我不能运行这个程序?
解决方案
您的应用程序运行良好.但是,您还没有实现应用程序循环:
导入pygame从 pygame.locals 导入 *pygame.init()赢 = pygame.display.set_mode((400,400))pygame.display.set_caption(我的第一场比赛")时钟 = pygame.time.Clock()运行 = 真运行时:# 处理事件对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误# 更新游戏对象# [...]# 清除显示win.fill((0, 0, 0))# 绘制游戏对象# [...]# 更新显示pygame.display.flip()# 限制每秒帧数时钟滴答(60)pygame.quit()
典型的 PyGame 应用程序循环必须:
- 通过调用 另见 事件和应用程序循环
I have a simple Pygame program:
#!/usr/bin/env python import pygame from pygame.locals import * pygame.init() win = pygame.display.set_mode((400,400)) pygame.display.set_caption("My first game")
But every time I try to run it, I get this:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3) Hello from the pygame community. https://www.pygame.org/contribute.html
And then nothing happens.Why I can't run this program?
解决方案Your application works well. However, you haven't implemented an application loop:
import pygame from pygame.locals import * pygame.init() win = pygame.display.set_mode((400,400)) pygame.display.set_caption("My first game") clock = pygame.time.Clock() run = True while run: # handle events for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # update game objects # [...] # clear display win.fill((0, 0, 0)) # draw game objects # [...] # update display pygame.display.flip() # limit frames per second clock.tick(60) pygame.quit()
The typical PyGame application loop has to:
- handle the events by calling either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by calling either
pygame.display.update()
orpygame.display.flip()
- limit frames per second to limit CPU usage with
pygame.time.Clock.tick
See also Event and application loop
这篇关于为什么我的 PyGame 应用程序根本没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- handle the events by calling either