Python速成课程评分

时间:2019-05-20 11:21:31

标签: python

在此先感谢您的帮助。尝试将评分功能添加到外星人入侵游戏中,但是我无法使代码正常工作。我指的是pg。这本书的301。再次感谢您的帮助。

这是我收到的错误消息。

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]

这是我在pg之后输入的代码。 301指南。

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)

请在下面找到附加的setting.py。预先感谢您的帮助。

class Settings():     “”“用于存储外来入侵的所有设置的类。”“”

   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 个答案:

没有答案