importpygameclassDisplayboard():‘‘‘展示程序中的文本‘‘‘
def __init__(self, my_settings, screen):
self.screen=screen
self.my_settings=my_settings
self.screen_rect=screen.get_rect()#获取字体对象
self.s_font = pygame.font.Font(r‘fonts\freesansbold.ttf‘,
my_settings.s_font_size)
self.m_font= pygame.font.Font(r‘fonts\freesansbold.ttf‘,
my_settings.m_font_size)
self.l_font= pygame.font.Font(r‘fonts\freesansbold.ttf‘,
my_settings.l_font_size)#获取文本图片
self.top_text_image =self.s_font.render(my_settings.top_text, True,
my_settings.YELLOW)
self.typing_flag_image= self.s_font.render("typing!", True,
my_settings.DARKGRAY)
self.start_text_image=self.m_font.render(my_settings.start_text, True,
my_settings.GREEN, my_settings.OLIVE)#获取surface的rect
self.start_image_rect =self.start_text_image.get_rect()
self.start_image_rect.center=self.screen_rect.center
self.typing_image_rect=self.typing_flag_image.get_rect()
self.typing_image_rect.right= self.screen_rect.right - 20self.typing_image_rect.top= 20
#显示各个文本的函数
defdisplay_top_text(self):
self.screen.blit(self.top_text_image,
self.my_settings.top_text_position)defdisplay_typing_text(self):
self.screen.blit(self.typing_flag_image, self.typing_image_rect)defdisplay_start_text(self):
self.screen.blit(self.start_text_image, self.start_image_rect)defdisplay_speed_text(self, speed):
speed_text_image= self.s_font.render("Current Speed:" + str(speed) + "letters/min",
True, self.my_settings.YELLOW)
speed_text_rect=speed_text_image.get_rect()
speed_text_rect.centery= self.screen_rect.centery / 2speed_text_rect.right= self.screen_rect.right - 20self.screen.blit(speed_text_image, speed_text_rect)defdisplay_letter_text(self, letter):
letter_text_image=self.l_font.render(letter, True,
self.my_settings.RED)
letter_rect=letter_text_image.get_rect()
letter_rect.centerx=self.screen_rect.centerx
letter_rect.centery= self.screen_rect.bottom * 2 / 3self.screen.blit(letter_text_image, letter_rect)defdisplay_remaining_time_text(self, remaining_time):
remaining_time_image= self.s_font.render("Remaining Time:" +str(remaining_time),
True, self.my_settings.OLIVE)
remaining_time_rect=remaining_time_image.get_rect()
remaining_time_rect.top= self.screen_rect.top + 20remaining_time_rect.centerx=self.screen_rect.centerx
self.screen.blit(remaining_time_image, remaining_time_rect)defdisplay_fastest_text(self, fastest_speed):
fastest_speed_image= self.s_font.render("Fastest Speed:" +str(fastest_speed),
True, self.my_settings.YELLOW)
fastest_speed_rect=fastest_speed_image.get_rect()
fastest_speed_rect.centery= self.screen_rect.centery / 2fastest_speed_rect.left= 20self.screen.blit(fastest_speed_image, fastest_speed_rect)