发生异常:错误显示Surface退出

问题描述 投票:0回答:1

我正在按照Python Crash Course 2nd Edition这本书制作一个外星人入侵游戏,我遇到了一个书上似乎没有的错误。

确切的回溯是:

Exception has occurred: error
display Surface quit
  File "/workspaces/functions/alien_invasion/alien_invasion.py", line 34, in run_game
    self.screen.fill(self.settings.background_colour)
  File "/workspaces/functions/alien_invasion/alien_invasion.py", line 43, in <module>
    alien_invasion.run_game()
pygame.error: display Surface quit

这是我的代码:

import sys
import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall class to manage game assets and behaviour."""

    def __init__(self):
        """Initialise 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_caption("Alien Invasion")

        self.ship = Ship(self)

        # Set the backgroung colour.
        self.background_colour = self.settings.background_colour # Creates a light grey colour.

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # Watch for keyboard/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.background_colour) # Line 34
            self.ship.blit_me()

            # Make the most recently drawn screen visible.
            pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance and run it.
    alien_invasion = AlienInvasion()
    alien_invasion.run_game() # Line 43

我已经通过 pip 安装了 Pygame,所以这不是问题。我还清楚地导入了 Pygame 和 sys。

如果有任何帮助,这里是设置代码:

class Settings:
    """A class to store all of the settings for alien_invasion.py."""

    def __init__(self):
        """Initialise the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.background_colour = (230, 230, 230)

对于船来说:

import pygame

class Ship:
    """A class to manage the ship."""

    def __init__(self, alien_invasion_game):
        """Initialise the ship and set a starting position."""
        self.screen = alien_invasion_game.screen
        self.screen_rect = alien_invasion_game.screen.get_rect()
        self.image_filepath = 'alien_invasion/assets/spaceship.jpg'

        # Load the ship image and get its rect.
        self.image = pygame.image.load(self.image_filepath)
        self.rect = self.image.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def blit_me(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image, self.rect)
python pygame
1个回答
0
投票

该错误意味着您拨打了

pygame.quit()
,然后尝试继续游戏。

我相信这段代码是问题所在:

for event in pygame.event.get():
    if event.type == pygame.quit():

您检查退出事件的方式错误。

支票应该是这样的:

if event.type == pygame.locals.QUIT:

但是因为你在比较中错误地使用了

pygame.quit()
,这意味着pygame quit函数被实际调用了!

© www.soinside.com 2019 - 2024. All rights reserved.