在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  網(wǎng)絡安全/ pygame.error: font not initialized?

pygame.error: font not initialized?

給游戲《飛機大戰(zhàn)》添加Play按鈕,用到pygame的pygame.fond模塊,出現(xiàn)下面的提示,我在starkoverflow搜到同樣的問題,但那個人的代碼是未初始化的,而我在代碼中明顯初始化過,請問是什么原因?

> Traceback (most recent call last):
  File "/Users/Eahon/Desktop/Python_work/\u5916\u661f\u4eba\u5165\u4fb5/alien_invasion.py", line 55, in <module>
    run_game()
  File "/Users/Eahon/Desktop/Python_work/\u5916\u661f\u4eba\u5165\u4fb5/alien_invasion.py", line 20, in run_game
    play_button = Button(ai_settings, screen, "Play")
  File "/Users/Eahon/Desktop/Python_work/\u5916\u661f\u4eba\u5165\u4fb5/button.py", line 14, in __init__
    self.font = pygame.font.SysFont(None, 48)
  File "/Users/Eahon/Library/Python/3.6/lib/python/site-packages/pygame/sysfont.py", line 320, in SysFont
    return constructor(fontname, size, set_bold, set_italic)
  File "/Users/Eahon/Library/Python/3.6/lib/python/site-packages/pygame/sysfont.py", line 243, in font_constructor
    font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized

代碼:

import pygame.font

class Button():

    def __init__(self, ai_settings, screen, msg):
        """初始化按鈕的屬性"""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        #設置按鈕的尺寸和其他屬性
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 0, 0)
        self.font = pygame.font.SysFont(None, 48)

        #創(chuàng)建按鈕的rect對象,并使其居中
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        #按鈕的標簽只需創(chuàng)建一次
        self.prep_msg(msg)

    def prep_msg(self, msg):
        """將msg渲染為圖像,并使其在按鈕上居中"""
        self.msg_image = self.font.render(msg, True, self.text_color,
                                              self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.rect.center

    def draw_button(self):
        # 繪制一個用顏色填充的按鈕,再繪制文本
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)
回答
編輯回答
維她命

同問題怎么解決

2017年3月27日 01:42
編輯回答
柚稚
import pygame
pygame.font.init()
font = pygame.font.SysFont(None, 48)
2017年12月23日 01:01