问题描述
我正在尝试使用 pygame 创建一个 2d 视频游戏。
执行以下代码后:
from ship import Ship
from settings import Settings
import sys
import pygame
class Alien_invasion:
"""class to manage the game"""
def __init__(self):
"""initialise the game and create game ressources"""
pygame.init()
#print("ouzou")
self.settings=Settings()
self.screen = pygame.display.set_mode(self.settings.screen_height,self.settings.screen_width)
pygame.display.set_caption("Alien invasion")
self.ship = Ship(self)
def run_game(self):
"""start the game by calling a main loop"""
while True:
print("izann")
# wait keyboard or mouse event
for event in pygame.event:
if event.type == pygame.quit():
sys.exit()
# draw the screen after all the changes occured
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
print("izan")
pygame.display.flip()
print("izan ssin")
if __name__ == "main":
partie = Alien_invasion()
partie.run_game()
我希望有一个 pygame 窗口,但控制台打印:
pygame 2.0.1 (SDL 2.0.14,Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
之后什么也没有发生!
解决方法
带有“###”的行是我的更正。
from ship import Ship
from settings import Settings
import sys
import pygame
class Alien_invasion:
"""class to manage the game"""
def __init__(self):
"""initialise the game and create game ressources"""
pygame.init()
#print("ouzou")
self.settings=Settings()
self.screen =
pygame.display.set_mode(self.settings.screen_height,self.settings.screen_width)
pygame.display.set_caption("Alien invasion")
self.ship = Ship(self)
def run_game(self):
"""start the game by calling a main loop"""
while True:
print("izann")
# wait keyboard or mouse event
for event in pygame.event.get(): ###syntax
if event.type == pygame.QUIT: ###syntax
sys.exit()
# draw the screen after all the changes occurred ###the following should not in the pygame event get loop
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
print("izan")
pygame.display.flip()
print("izan ssin")
if __name__ == "__main__": ###syntax wrong
partie = Alien_invasion()
partie.run_game()
,
pygame.quit()
是一个函数,用于取消初始化所有 PyGame 模块。当你这样做
if event.type == pygame.quit():
函数被调用,所有 PyGame 模块都未初始化。
pygame.event.Event()
对象的 type 属性表示事件的类型。您需要将事件类型与标识事件的枚举常量进行比较。 quit 事件由 pygame.QUIT
标识(参见 pygame.event
模块):
因此,您必须与 pygame.QUIT
而不是 pygame.quit()
竞争:
for event in pygame.event:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
此外还有一个 Indentation 问题。您必须在应用程序循环而不是事件循环中绘制场景并更新显示:
from ship import Ship
from settings import Settings
import sys
import pygame
class Alien_invasion:
"""class to manage the game"""
def __init__(self):
"""initialise the game and create game ressources"""
pygame.init()
#print("ouzou")
self.settings=Settings()
self.screen = pygame.display.set_mode(self.settings.screen_height,self.settings.screen_width)
pygame.display.set_caption("Alien invasion")
self.ship = Ship(self)
def run_game(self):
"""start the game by calling a main loop"""
while True:
print("izann")
# wait keyboard or mouse event
for event in pygame.event:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# INDENTATION
#<--|
# draw the screen after all the changes occured
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
print("izan")
pygame.display.flip()
print("izan ssin")
if __name__ == "__main__":
partie = Alien_invasion()
partie.run_game()