pygame实例化后不会绘制任何内容

2024-09-30 08:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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

之后什么也没发生


Tags: thefromimportselfeventgamesettingsdisplay
2条回答

^{}是一个函数,用于取消所有PyGame模块的初始化。当你这样做的时候

if event.type == pygame.quit():

该函数被调用,所有PyGame模块都未初始化

^{}对象的type属性指示事件的类型。您需要将事件类型与标识事件的枚举常量进行比较。退出事件由pygame.QUIT标识(请参见^{}模块):

因此,您必须与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()

带“####”的行是我的更正

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()

相关问题 更多 >

    热门问题