Pygame 屏幕输出不显示

Pygame screen output not displaying

我正在与 Pygame 合作开发一个游戏项目。我才刚刚开始从事这个项目,我有点卡住了。我制作了三个文件,每个文件都包含执行不同功能的代码。第一个文件 - “alien_apocalypse.py”包含 class 'AlienApocalypse',用于启动和监视用户事件在游戏中包含一些导入的模块,例如游戏设置 (这是第二个文件 - 'game_settings.py')和一个 class 文件,其中包含游戏角色之一的所有属性 'knight.py'。我正在尝试 运行 "alien_apocalypse.py" 这意味着显示我的角色骑士和显示器的底部中间但没有显示。我在 运行 上使用 macOS Mojave 在 Mac 上安装它,IDE 是 PyCharm。以下是文件:

import sys
import os
import pygame
from game_settings import GameSettings
from knight import Knight


class AlienApocalypse:
    """Overall class to manage game assets and behaviour"""

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

        drivers = ['directfb', 'fbcon', 'svgalib']

        found = False
        for driver in drivers:
            if not os.getenv('SDL_VIDEODRIVER'):
                os.putenv('SDL_VIDEODRIVER', driver)
            try:
                pygame.display.init()
            except pygame.error:
                print('Driver: {0} failed.'.format(driver))
                continue
            found = True
            break

        if not found:
            raise Exception('No suitable video driver found!')

        self.screen_window = pygame.display.set_mode((2880, 1800))
        pygame.display.set_caption("Alien Apocalypse")

        """Setting background color"""
        self.background_color = (230, 230, 255)

        self.knight = Knight(self)

    def run_game(self):
        """Start the main loop for the game"""
        while True:
            # Watch for keyboard and mouse actions
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Redraw the screen during each pass through the loop.
            self.screen_window.fill(self.settings.background_color)
            self.knight.blitme()

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


if __name__ == "__main__":
    """Make a game instance, and run the game"""
    ac = AlienApocalypse()
    ac.run_game()


class GameSettings:
    """This class stores all the game settings"""

    def __init__(self):
        """Initialize the game's settings attributes"""
        # Screen settings
        self.screen_width = 2880
        self.screen_height = 1800
        self.background_color = (230, 230, 255)

import pygame


class Knight:
    """A class that manages the character knight"""

    def __init__(self, ac_game):
        """Initialize the knight and set its starting position."""
        self.screen_window = ac_game.screen_window
        self.screen_window_rect = ac_game.screen_window.get_rect()

        # Load the character - Knight image and get its rect.
        image_file = "/Users/johnphillip/Downloads/craftpix-891165-assassin" \
                     "-mage-viking-free-pixel-art-game-heroes/PNG/Knight" \
                     "/knight.bmp "
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()

        # Start each new character at the bottom center of the screen.
        self.rect.midbottom = self.screen_window_rect.midbottom

    def blitme(self):
        """Draw the character at its current location."""
        self.screen_window.blit(self.image, self.rect)

到目前为止我检测到的问题(现在显示 window):

  • 您必须设置正确的视频驱动程序

  • 那你要初始化pygame.display

  • 您在 class (ac.run_game()) 中调用了 run_game() 函数,而不是其他函数。

  • class里面的run_game()什么都不做(pass)

  • 您必须将 class 内部的当前 run_game() 替换为外部的 run_game(),这样您就可以像访问变量和函数一样访问 "self things" .

  • 如果不存在,您不能将 self 等于 None 作为默认值(self 是 class 本身及其包含的所有内容,因此如果您将其等于 None 你是 "killing" 你自己 (class)!!!)

您的 alien_apocalypse.py 可能如下所示:

import pygame
from game_settings import GameSettings
from knight import Knight

class AlienApocalypse:
"""Overall class to manage game assets and behaviour"""

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

    drivers = ['windib', 'directx']

    found = False
    for driver in drivers:
        if not os.getenv('SDL_VIDEODRIVER'):
            os.putenv('SDL_VIDEODRIVER', driver)
        try:
            pygame.display.init()
        except pygame.error:
            print('Driver: {0} failed.'.format(driver))
            continue
        found = True
        break

    if not found:
        raise Exception('No suitable video driver found!')

    self.screen_window = pygame.display.set_mode((2880, 1800))
    pygame.display.set_caption("Alien Apocalypse")

    """Setting background color"""
    self.background_color = (230, 230, 255)

    self.knight = Knight(self)

def run_game(self):
    """Start the main loop for the game"""
    while True:
        # Watch for keyboard and mouse actions
        # for event in pygame.event.get():
        #     if event.type == pygame.QUIT:
        #         sys.exit()

        # Redraw the screen during each pass through the loop.
        self.screen_window.fill(self.settings.background_color)
        self.knight.blitme()

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

if __name__ == "__main__":
    """Make a game instance, and run the game"""
    ac = AlienApocalypse()
    ac.run_game()