繁体   English   中英

外星人入侵项目中的问题,来自 Eric Matthes 的 Python Crash Course

[英]Problem in Alien Invasion project, from Python Crash Course by Eric Matthes

我是从 Eric Matthes 的Python 速成课程学习 Python 的新手。 在异形入侵项目中我遇到了麻烦。 查了这么多,逐行对比书,还是没找到问题所在。

在最后几列中,我没有得到写 if case 的逻辑?

外星人入侵.py

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.ship = Ship(self)
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")
        # Set the background color.
        # self.bg_color = (self.settings.bg_color)

    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()
            # Redraw the screen during each pass through the loop.
            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()
if __name__ == '__main__':  # investigate the logic of this line of code
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

在 ship.py 中,有人可以向我解释为什么我们在初始模块中编写ai_game

船.py

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 its current location."""
        self.screen.blit(self.image, self.rect)

设置.py

class Settings:
    """A class to store all settings for Alien Invasion."""
    def __init__(self):
        """Initialize the game settings."""
        # Screen settings
        self.screen_width = 1000
        self.screen_height = 650
        self.bg_color = (230, 230, 230)

当我运行 alien_invasion.py 时,我收到以下错误:

 "C:\Users\User\PycharmProjects\Alien Invasion\venv\Scripts\python.exe" "C:/Users/User/PycharmProjects/Alien Invasion/alien_invasion.py" pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "C:/Users/User/PycharmProjects/Alien Invasion/alien_invasion.py", line 37, in <module> ai = AlienInvasion() File "C:/Users/User/PycharmProjects/Alien Invasion/alien_invasion.py", line 15, in __init__ self.ship = Ship(self) File "C:\Users\User\PycharmProjects\Alien Invasion\ship.py", line 8, in __init__ self.screen = ai_game.screen AttributeError: 'AlienInvasion' object has no attribute 'screen' Process finished with exit code 1

您只需要两个切换这两条线,以便它们按顺序排列,来自:

    self.ship = Ship(self)
    self.screen = pygame.display.set_mode(
        (self.settings.screen_width, self.settings.screen_height))

至:

    self.screen = pygame.display.set_mode(
        (self.settings.screen_width, self.settings.screen_height))
    self.ship = Ship(self)

原因是你创建了一个Ship object 想要访问AlienInvasionscreen ,所以你需要在Ship之前定义它,否则它是未定义的。


在最后几列中,我没有得到写 if case 的逻辑?

if __name__ == '__main__'是 Python 中的一个特殊结构。 这意味着只有在您直接运行脚本时才会执行以下块,而不是在其他脚本中导入脚本。 游戏并不真正需要它,但无论如何习惯它是一种很好的做法。

在 ship.py 中,有人可以向我解释为什么我们在初始模块中编写ai_game

Ship基本上希望能够访问原始的AlienInvasion object,因为它包含与游戏相关的信息,就像在这种情况下要绘制的屏幕一样。 AlienInvasion object 然后被传递为使用Ship(self)调用Ship.__init__(self, ai_game) (左边的self变成右边的ai_game )。

祝你学习顺利!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM