![](/img/trans.png)
[英]Why isn't my pygame window not white and not staying open and quitting when I try to exit it?
[英]Pygame is installed, but when I try to run my program the window won't open
我正在尝试从我找到的 Python 书的介绍做一个项目。 我安装了 pygame 2.1.2 和 Python 3.10.5,并且我的代码运行时没有显示任何错误。 问题是当我尝试运行我的代码时,pygame window 没有打开。 这是我的代码:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_cpation("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__ main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
进行一些更改后,代码应该可以工作: pygame.display.set_caption() 拼写错误,但更重要的是,由于 '__ main__' 中的空间,代码甚至无法运行。 最后,您应该在 sys.exit() 之前调用 pygame.quit() 以避免 pygame 崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.