大佬们帮忙康康这是咋回事,昨天晚上编辑完还能正常运行的,今天早上一打开就报错了
import pygame.fontclass Scoreboard:
'''显示得分信息的类'''
def __init__(self,ai_game):
'''初始化显示得分涉及的属性'''
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# 准备包含最高得分和当前得分的图像
self.prep_score()
# self.prep_high_score()
#显示得分信息时使用的字体设置
self.text_color = (30,30,30)
self.font = pygame.font.SysFont(None,48)
# 准备初始得分图像
self.prep_score()
def prep_score(self):
'''将得分转换为一副渲染的图像'''
round_score = round(self.stats.score, -1)
score_str = '{:,}'.format(round_score)
self.score_image = self.font.render(score_str,True,self.text_color,self.settings.bg_color)
# 在屏幕右上角显示得分
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):
'''在屏幕上显示得分'''
self.screen.blit(self.score_image,self.score_rect)
pycharm总是提示第25行
File "D:\pythonclearn\pythonProject\alien_invasion\alien\case1\scoreboard.py", line 25, in prep_score
self.score_image = self.font.render(score_str,True,self.text_color,self.settings.bg_color)
AttributeError: 'Scoreboard' object has no attribute 'font'
球球大佬们帮帮孩子吧,谢谢啦 看起来好像是引用在变量前边了
你把这句放到 prep_score 前边试试
self.font = pygame.font.SysFont(None,48) 大马强 发表于 2021-10-17 09:38
看起来好像是引用在变量前边了
你把这句放到 prep_score 前边试试
import pygame.font
class Scoreboard:
'''显示得分信息的类'''
def __init__(self,ai_game):
'''初始化显示得分涉及的属性'''
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# 准备包含最高得分和当前得分的图像
self.prep_score()
self.prep_high_score()
#显示得分信息时使用的字体设置
self.text_color = (30,30,30)
self.font = pygame.font.SysFont(None,48)
# 准备初始得分图像
# self.prep_score()
def prep_score(self):
'''将得分转换为一副渲染的图像'''
round_score = round(self.stats.score, -1)
score_str = '{:,}'.format(round_score)
self.score_image = self.font.render(score_str,True,self.text_color,self.settings.bg_color)
# 在屏幕右上角显示得分
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):
'''在屏幕上显示得分'''
self.screen.blit(self.score_image,self.score_rect)
self.screen.blit(self.high_score_image,self.high_score_rect)
def prep_high_score(self):
# 将最高得分转换为渲染的图像
high_score = round(self.stats.high_score,-1)
high_score_str = '{:,}'.format(high_score)
self.high_score_image = self.font.render(high_score_str,True,self.text_color,self.settings.bg_color)
# 将最高得分放在屏幕顶部中央
self.high_score_rect = self.high_score_image.get_rect()
self.high_score_rect.center = self.screen_rect.center
self.high_score_rect.top = self.screen_rect.top
def check_high_score(self):
'''检查是否诞生了新的最高得分'''
if self.stats.score >self.stats.high_score:
self.stats.high_score = self.stats.score
self.prep_high_score()
大佬,把这句代码放前面,可以了,但是一把最高分的这段代码加上就又报这种错误了,是不是pycharm有bug鸭 我pygame没咋学,我是靠你报错信息来改的
发下你的报错信息 大马强 发表于 2021-10-17 09:58
我pygame没咋学,我是靠你报错信息来改的
发下你的报错信息
哈哈,解决了,果然时大佬说的,变量位置引发的错误,我把self.prep_sore()和self.prep_high_score()这两个方法都放在了所有属性的后面就ok了,谢谢大佬的提示{:5_95:} {:10_281:}
页:
[1]