【问题标题】:Pygame troubleshooting black screen: Alien InvasionPygame排查黑屏:外星人入侵
【发布时间】:2020-06-25 20:48:07
【问题描述】:

我一直在关注 Python 速成课程项目来创建一个太空入侵者游戏。我已经能够编码到游戏窗口将打开的地步,但是我没有在屏幕的中间底部使用我的图像获取背景,而是遇到了黑屏。我是编码新手,并且已经看到这对其他人来说也是一个类似的问题。我正在使用 Python 3.8 也成功上传了正确的 pip 安装 (https://ehmatthes.github.io/pcc_2e/updates/python3_8/#pygame-on-python-38)。

这是外星人入侵的代码:

import sys

import pygame

class AlienInvasion:
    """Overall Class to manage game assests and behavior."""

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

        self.screen = pygame.display.set_mode((1100, 700))
        pygame.display.set_caption("Alien Invasion")

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

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

def __init__(self):
    pygame.display.set_caption("Alien Invasion")

    # Set the Background Color
    self.bg_color = (230, 230, 230)

def run_game(self):
    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.bg_color)

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

import pygame

from settings import Settings

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_caption("Alien Invasion")

    def run_game(self):
        # Redraw the screen during each pass through the loop.
        self.screen.fill(self.settings.bg_color)

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

from settings import Settings
from ship import Ship

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

    def __init__(self):
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

    def run_game(self):
        # Redraw the screen during each pass through the loop.
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()

        # Make most recently drawn screen visible
        pygame.display.flip()
    
    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            # Redraw the screen during each pass through the loop
            break

    def _check_events(self):
        """Respond to keypress and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    def run_game(self):
        """Start the main loop for the game"""
        while True:
            self._check_events()
            self._update_screen()

    def _check_events(self):
        --snip--

    def _update_sceen(self):
        """Update images on the screen, and flip to the new screen."""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()

        pygame.display.flip()

设置

 class Settings:
    """A class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initialize the game's static settings."""
        # Screen settings.
        self.screen_width = 1100
        self.screen_height = 700
        self.bg_color = (230, 230, 230)

宇宙飞船:

  import pygame

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

    def __init__(self, ai_game):
        """Initialize the ship and set its starting position."""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # Load the ship image and get its rect.
        self.image = pygame.image.load('images/ship.bmp')
        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 blitme(self):
        """Draw the ship at current location"""
        self.screen.blit(self.image, self.rect)

我目前正在使用 Eric Matthes 的 Python 速成课程。

【问题讨论】:

  • 你能分享一些关于你的环境的信息吗?什么操作系统、软件包及其版本等。

标签: python pygame display


【解决方案1】:

你有一个基本的误解。您似乎已尝试将绘图分发给名为AlienInvasion 的类的多个实现。了解Class“类提供了一种将数据和功能捆绑在一起的方法。”。类是一种类型,您必须将实现放在一个“类”中。实际上,您实现中的每个AlienInvasion 类都是不同模块中的不同类型。不可能将一个类的实现和一个类的方法分布到不同的模块。

AlienInvasion 类中创建SettingsShip 的实例:

self.settings = Settings()
self.ship = Ship(self)

AlienInvasion 类的实例方法run_game 实现了主应用程序循环。主应用程序循环必须:

例如:

import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall Class to manage game assests and behavior."""

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

        self.screen = pygame.display.set_mode((1100, 700))
        pygame.display.set_caption("Alien Invasion")

        self.settings = Settings()
        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()

            # clear display
            self.screen.fill(self.settings.bg_color)  

            # draw scene
            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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多