我是刚从埃里克·马特思的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:\User\User\PycharmProjects\alien Invasion\venv\Scripts\python.exe”“C:/User/User/PycharmProjects/alien入侵/外星入侵/外星入侵. the”pygame 1.9.6 .https://www.pygame.org/contribute.html跟踪(最近一次调用):文件“C:/User/User/PycharmProjects/Alien入侵/外侨_invasion.py”,第37行,在ai = AlienInvasion()文件“C:/User/User/PycharmProjects/Alien入侵/外星人_入侵”中,第15行,在__init__ self.ship = Ship(self)文件“C:\User\User\PycharmProjects\Alien Invasion\ship.py”中,第8行,在__init__ self.screen = ai_game.screen AttributeError中:'AlienInvasion‘对象没有使用退出代码1
完成的属性'screen’过程
发布于 2020-05-24 13:22:21
您只需两次切换这两行,以便它们符合顺序,从:
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对象,它希望访问AlienInvasion的screen,所以需要在Ship之前定义它,否则它是未定义的。
在最后几篇专栏文章中,我没有得到写作的逻辑,如果是案例呢?
if __name__ == '__main__'是Python中的一个特殊构造。这意味着只有在直接运行脚本时才会执行以下块,而不是在其他脚本中导入脚本。这并不是真正需要一场比赛,但这是很好的做法,以适应它无论如何。
和ship.py中可以有人向我解释为什么我们在初始模块中编写
ai_game。
Ship基本上想要访问原始的AlienInvasion对象,因为它保存着与游戏相关的信息,就像在本例中要绘制的屏幕一样。然后,AlienInvasion对象被传递为使用调用Ship.__init__(self, ai_game)的Ship(self) (左self变为右ai_game)。
祝学习好运!
https://stackoverflow.com/questions/61898982
复制相似问题