NameError: при написании игры на pygame
вопрос таков.
Пишу по учебнику Э.Метиз Изучаем Python учебный проект при помощи Pygame.
При определении одной из переменных screen вылезает ошибка NameError.
Хотя переменная инициализирована. Не вижу своей ошибки.
Буду рад помощи.
Основной файл game.py
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion():
"""A class for managing resources and game behavior"""
def __init__(self):
"""Inizialization game and create games 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")
self.ship = Ship(screen)
def run_game(self):
"""Launch main cycle game"""
while True:
# keyboard and mouse
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Display the last drawn screen
pygame.display.flip()
if __name__ == '__main__':
# Create exemple and launch game
ai = AlienInvasion()
ai.run_game()
файл с классом корабля Ship.py
class Ship():
"""Class for control ship"""
def __init__(self, ai_game):
"""initialization ship and sets its initial position"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
#Load image ship
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
#Each new ship appears at the bottom of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw ship in current position"""
self.screen.blit(self.image, self.rect)
Текст сообщения об ошибке:
Traceback (most recent call last):
File "C:\Users\Gradysnik\Desktop\python_work\alien_invasion\game.py", line 33, in <module>
ai = AlienInvasion()
File "C:\Users\Gradysnik\Desktop\python_work\alien_invasion\game.py", line 15, in __init__
self.ship = Ship(screen)
NameError: name 'screen' is not defined
upd. файл settings.py
class Settings():
"""A class for save all settings game Alien Invasion"""
def __init__(self):
"""Inicialization settings game"""
# Display settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
я отметил строки, в которые внес изменения.
game.py
import sys # +++
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion():
"""A class for managing resources and game behavior"""
def __init__(self):
"""Inizialization game and create games 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")
self.ship = Ship(self.screen) # + self.
def run_game(self):
"""Launch main cycle game"""
while True:
# keyboard and mouse
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Display the last drawn screen
pygame.display.flip()
if __name__ == '__main__':
# Create exemple and launch game
ai = AlienInvasion()
ai.run_game()
ship.py
import pygame # +++
class Ship():
"""Class for control ship"""
def __init__(self, ai_game):
"""initialization ship and sets its initial position"""
self.screen = ai_game #.screen
self.screen_rect = ai_game.get_rect() #ai_game.screen.get_rect()
#Load image ship
# self.image = pygame.image.load('images/ship.bmp')
self.image = pygame.image.load('Ok.png')
self.rect = self.image.get_rect()
#Each new ship appears at the bottom of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw ship in current position"""
self.screen.blit(self.image, self.rect)
settings.py
class Settings():
"""A class for save all settings game Alien Invasion"""
def __init__(self):
"""Inicialization settings game"""
# Display settings
self.screen_width = 1200
self.screen_height = 600
self.bg_color = (230, 230, 230)
