1

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

asked Jun 4, 2021 at 21:14

1 Answer 1

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.

answered Jun 4, 2021 at 21:17
Sign up to request clarification or add additional context in comments.

2 Comments

Okay I fixed the typo. Now theres a name error of "screen.fill(ai_setting.bg_color) NameError: name 'ai_setting' is not defined" and "run_game gf.update(ai_setting, screen, ship)"
@code Another typo. You passed it into the 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.