I working with Python right now to build a space invaders project. The errors I'm currently facing is AttributeError: (ai_setting.screen_width, ai_setting.screen_height)) 'Settings' object has no attribute 'screen_width' in Python. There's also an error around the "run_game()" part at the bottom. The game is so far supposed to draw a screen and place a ship image at the bottom center of the screen. Thanks!
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
pygame.init()
ai_setting = Settings()
#Creating the screen
screen = pygame.display.set_mode(
(ai_setting.screen_width, ai_setting.screen_height))
#Creating title
pygame.display.set_caption("Space Invasion")
ship = Ship(screen)
#Creating the logo/icon
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
while True:
gf.check_events()
gf.update(ai_setting, screen,ship)
run_game()
Above: main.py file
class Settings():
def _init__(self):
self.screen_width = 800
self.screen_height = 600
self.bg_color = (0,255,0)
Above: settings.py file
import pygame
import sys
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update(ai_settings, screen, ship):
screen.fill(ai_setting.bg_color)
ship.blitme
pygame.display.flip()
Above: game_events.py file
import pygame
class Ship():
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load("space-invaders.py")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
self.screen.blit(self.image, self.rect)
Above: ship.py file
1 Answer 1
You have a typo in the Settings class. You're missing an underscore in its __init__. You have:
def _init__(self):
when instead you should have:
def __init__(self):
Because of this typo, the screen_width and screen_height properties will not be created or set when Settings() is called.
2 Comments
update function with the name ai_settings, but you're referring to it as ai_setting within that function. Either change ai_setting to ai_settings in update, or change the ai_settings parameter to ai_setting.