外星人入侵项目中的问题,摘自Eric Matthes的Python速成班

2024-09-30 08:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是从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

ship.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_invision.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

Tags: andthepyrectselfgamesettingsinit
1条回答
网友
1楼 · 发布于 2024-09-30 08:30:25

您只需从以下位置切换这两条线路,使其按顺序排列:

    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对象,该对象想要访问AlienInvasionscreen,因此您需要在Ship之前定义它,否则它是未定义的


Here in the last columns, I didn't get the logic of writing if case?

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

And in ship.py could someone explain to me why we write ai_game in initial module.

Ship基本上想要访问原始的AlienInvasion对象,因为它保存与游戏相关的信息,就像在本例中绘制屏幕一样。然后AlienInvasion对象被传递为使用Ship(self)调用Ship.__init__(self, ai_game)(左self变为右ai_game

祝你学习顺利

相关问题 更多 >

    热门问题