Pygame窗口没有打开

时间:2020-02-02 09:14:12

标签: python pygame python-3.8

我暂时正在关注Python Crash Course 2nd Edition。 我停留在第12章,从pygame开始。 这是代码(来自书,所以应该可以工作)。 我在Mac上使用VSC。

import sys

import pygame

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

    def __init__(self):
        """Initialize the game, and create game resources"""
        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))
        pygame.display.set_caption("Alien Invasion")

        # set the background color.
        self.bg_color = (230, 230, 230)

    def run_game(self):
        """Start th emain loop for the game"""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print('Quitting...')
                    sys.exit()

            # Redraw the screen during each pass through the loop.
            self.screen.fill(self.bg_color)

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

if __name__ == '__main__':
    # Make a game instance and run the game.
    ai = AlienInvasion()
    print("running pygame...")
    ai.run_game()

print("running pygame...")语句运行,但是没有窗口打开。

关于这里出了什么问题的任何想法吗?

编辑: pygame是通过pip3安装的。 我尝试在空闲和VSC中运行此代码。 输出到我的终端如下

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
running pygame...

此后没有新窗口打开

2 个答案:

答案 0 :(得分:0)

您在AlienInvasion类之后忘记了()。它看起来应该像这样: 类AlienInvasion(): “”“用于管理游戏资产和行为的总体课程。”“”

答案 1 :(得分:0)

我遇到了这个问题,不得不从3.8降级到python 3.6。这为我解决了。在3.8中运行pygame似乎有很多问题。