Scoring in Python Crash Course

Asked by At

Thanks in advance for your help. Attempting to add Scoring feature to the alien invasion game however I am not able to make the code work. I am referring to pg. 301 of the book. Thanks again for your help.

Here is the error message I am getting.

Traceback (most recent call last):
  File "/Users/DD/Documents/Programming/Python 3/Alien Invasion/alian_invasion.py", line 46, in <module>
    run_game()
  File "/Users/DD/Documents/Programming/Python 3/Alien Invasion/alian_invasion.py", line 24, in run_game
    sb = Scoreboard(ai_settings, screen, stats)
  File "/Users/DD/Documents/Programming/Python 3/Alien Invasion/scoreboard.py", line 20, in __init__
    self.prep_score()
  File "/Users/DD/Documents/Programming/Python 3/Alien Invasion/scoreboard.py", line 25, in prep_score
    self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color)
AttributeError: 'Settings' object has no attribute 'bg_color'
[Finished in 0.7s with exit code 1]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/DD/Documents/Programming/Python 3/Alien Invasion/alian_invasion.py']]
[dir: /Users/DD/Documents/Programming/Python 3/Alien Invasion]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin]

Here's the code I have entered that follows pg. 301 guidance.

import pygame.font

class Scoreboard():
    """ A class to report scoring information. """

    def __init__(self, ai_settings, screen, stats):
        """ Initialize scorekeeping attributes. """
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.ai_settings = ai_settings
        self.stats = stats

        # Font settings for scoring information.
        self.text_color = (30, 30, 30)
        self.font = pygame.font.SysFont(None, 48)

        # Prepare the inital score image.
        self.prep_score()

    def prep_score(self):
        """ Turn the score into a rendered image. """
        score_str = str(self.stats.score)
        self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color)

        # Display the score at the top right screen.
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def show_score(self):
        """ Draw score to the screen. """
        self.screen.blit(self.score_image, self.score_rect)

Please find attached setting.py below. Thanks in advance for your help.

class Settings(): """ A class to store all settings fro Alien Invasion."""

   def __init__(self):
    """ Initialise the game's static settings."""

    # Screen settings
    self.screen_width = 1200
    self.screen_height = 800
    self.bg_colour = (230, 230, 230)

    # Ship settings
    self.ship_limit = 3

    # Bullet settings
    self.bullet_width = 3
    self.bullet_height = 15
    self.bullet_colour = 60, 60, 60
    self.bullets_allowed = 3

    # Alien settings
    self.fleet_drop_speed = 10

    # How quickly the game speeds up
    self.speedup_scale = 1.1

    self.initialize_dynamic_settings()

   def initialize_dynamic_settings(self):
    """ Initialize settings that change throughout the game. """
    self.ship_speed_factor = 10.5
    self.bullet_speed_factor = 30
    self.alien_speed_factor = 10

    # fleet_direction of 1 represents right; -1 represents left.
    self.fleet_direction = 1

   def increase_speed(self):
    """ Increase speed settings. """
    self.ship_speed_factor *= self.speedup_scale
    self.bullet_speed_factor *= self.speedup_scale
    self.alien_speed_factor *= self.speedup_scale

0 Answers