im just started learning python with “Python crash course”
and there is code in this book, but it’s not working, or probably i’m missing smt
in line “self.ship = Ship(screen)” there is an error:
Traceback (most recent call last):
File “c:\Users\Куку\Desktop\Space Invaders\first_step.py”, line 40, in
ai = AlienInvasion()
^^^^^^^^^^^^^^^
File “c:\Users\Куку\Desktop\Space Invaders\first_step.py”, line 23, in init
self.ship = Ship(screen)
^^^^^^
NameError: name ‘screen’ is not defined. Did you mean: ‘self.screen’?
My code:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
def __init__(self):
"""
The function initializes the Pygame module and sets up the game window.
"""
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):
while True:
# Отслеживание событий клавиатуры и мыши
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# При каждом проходе цикла перерисовывается экран
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Отображение последнего прорисованного экрана
pygame.display.flip()
if __name__ == "__main__":
# Создание экземпляра и запуск игры
ai = AlienInvasion()
ai.run_game()
code for class Ship
import pygame
class Ship:
"""Класс для управления кораблем"""
def __init__(self, ai_game, screen):
"""Инициализация корабля и его начально позиции"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Загружает изображение корабля и получает прямоугольник
self.image = pygame.image.load("images/ship.bmp")
self.rect = self.image.get_rect()
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Отрисовка корабля"""
self.screen.blit(self.image, self.rect)
code for settings
class Settings():
def __init__(self):
"""Инициализация параметров игры."""
# Параметры экрана
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (248, 124, 86)
What i need to change