簡體   English   中英

Pygame 已安裝,但是當我嘗試運行我的程序時,window 不會打開

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM