我是Eric Matthes的Python崩溃课程
中学习Python的新手。在“外来入侵”项目中,我遇到了麻烦。我进行了很多调查,逐行进行了比较,但找不到问题。在最后几列中,如果出现大小写,我没有写书的逻辑?
alien_invasion.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)
settings.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
我是Eric Matthes的Python速成课程中学习Python的新手。在“外来入侵”项目中,我遇到了麻烦。我进行了很多调查,逐行比较了本书,但找不到...
您只需要通过两行切换这两行,就可以使它们按顺序排列,从: