Nov-07-2019, 09:39 AM
Hi everyone,
I am working on a game with python 3.6 and PYGAME and with the Geany text editor.
The problem is in the last line. Can someone please explain why I am getting this error?
[inline]
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "catch!.py", line 38, in <module>
run_game()
File "catch!.py", line 22, in run_game
sb = Scoreboard(screen, c_settings, stats)
File "C:\Users\ASUS\Documents\python\scoreboard.py", line 17, in __init__
self.prep_score()
File "C:\Users\ASUS\Documents\python\scoreboard.py", line 23, in prep_score
rounded_score = round(self.stats.score, -1)
AttributeError: 'GameStats' object has no attribute 'score'
[/inline]
thanks if anyone can help me
catch!.py
I am working on a game with python 3.6 and PYGAME and with the Geany text editor.
The problem is in the last line. Can someone please explain why I am getting this error?
[inline]
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "catch!.py", line 38, in <module>
run_game()
File "catch!.py", line 22, in run_game
sb = Scoreboard(screen, c_settings, stats)
File "C:\Users\ASUS\Documents\python\scoreboard.py", line 17, in __init__
self.prep_score()
File "C:\Users\ASUS\Documents\python\scoreboard.py", line 23, in prep_score
rounded_score = round(self.stats.score, -1)
AttributeError: 'GameStats' object has no attribute 'score'
[/inline]
thanks if anyone can help me
catch!.py
import pygame
from pygame.sprite import Group
from settings import Settings
import game_functions as gf
from character import Character
#from background import Background
from gamestats import GameStats
#from button import Button
from scoreboard import Scoreboard
def run_game():
pygame.init()
c_settings = Settings()
screen = pygame.display.set_mode(
(c_settings.screen_width, c_settings.screen_height))
pygame.display.set_caption("Welcome to Catch")
catch = pygame.mixer.Sound("sounds/jumping2.ogg")
end = pygame.mixer.music.load("sounds/end.ogg")
pygame.mixer.music.load("sounds/music.ogg")
stats = GameStats(c_settings)
sb = Scoreboard(screen, c_settings, stats)
background = Background(screen)
character = Character(screen)
balls = Group()
gf.create_balls(screen, c_settings, balls)
play_button = Button(c_settings, screen, "Play")
while True:
gf.check_key_events(character, screen, stats, c_settings, balls,
play_button, sb)
if stats.game_active == true:
gf.update_balls(screen, balls, c_settings, character, stats, sb, catch, end)
character.update(c_settings)
gf.update_screen(screen, c_settings, character, balls,
background, stats, play_button, sb)
run_game()
scoreboard.pyimport pygame.font from pygame.sprite import Group from character import Character class Scoreboard(): def __init__(self, screen, c_settings, stats): self.screen = screen self.screen_rect = screen.get_rect() self.c_settings = c_settings self.stats = stats self.text_color = (30, 30, 30) self.font = pygame.font.SysFont(None, 48) self.prep_score() self.prep_high_score() self.prep_lives_left() def prep_score(self): rounded_score = round(self.stats.score, -1) score_str = 'Score: ' + "(:,)".format(rounded_score) self.score_image = self.font.render(score_str, True, self.text_color) self.score_rect = self.score_image.get_rect() self.score_rect.roght = self.screen_rect.right - 20 self.score_rect.top = 20 def prep_high_score(self): high_score = round(self.stats.high_score, -1) high_score_str = 'High: ' + "(:,)".format(high_score) self.high_score_image = self.font.render(high_score_str, True, self.text_color) self.high_core_rect = self.high_score_image.get_rect() self.high_score_rect.top = self.score_rect.top self_high_score_rect.left = 20 def prep_lives_left(self): self.lives = Group() for life_number in range(self.stats.lives_left): life = Character(self.screen) life.rect.right = self.screen_rect.right life.rect.y = self.score_rect.bottom + 10 self.lives.add(life) def show_score(self): self.screen.blit(self.score_image, self.score_rect) self.screen.blit(self.high_score_image, self.high_score_rect) self.lives.draw(self.screen)gamestats.py
class GameStats(): def __init__(self, c_settings): self.c_settings = c_settings filename = 'high_score.txt' with open(filename) as file_object: self.high_score = int(file_object.read()) self.game_active = False self.dynamic_settings() def dynamic_settings(self): self.lives_left = 2 self_score = 0
