Pygame 回溯错误

-1 投票
2 回答
1051 浏览
提问于 2025-04-17 23:35

我该怎么解决这个错误?这是我收到的提示信息:

Traceback (most recent call last):
File "C:\Users\Games\Desktop\hendeagon.py", line 28, in <module>
font = pygame.font.SysFont(None, 48)
File "C:\Python33\lib\site-packages\pygame\sysfont.py", line 614, in SysFont
return constructor(fontname, size, set_bold, set_italic)
File "C:\Python33\lib\site-packages\pygame\sysfont.py", line 537, in font_constructor
font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized

我需要在明天之前修复这个错误,因为这是我的作业。

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
TEXTCOLOR = (255, 255, 255)
WINDOWWIDTH = 500
WINDOWHEIGHT = 400

windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

font = pygame.font.SysFont('comicsansms', 48)
basicFont = pygame.font.SysFont(None, 42)
drawText('8', font, windowSurface, (WINDOWWIDTH / 2.5), (WINDOWHEIGHT / 3))
windowSurface.fill(BLACK)

pygame.draw.polygon(windowSurface, GREEN, ((158, 80), (181, 88), (214, 111), (229, 153),     (216, 191), (181, 212), (135, 207), (102, 181), (89, 149), (102, 109), (130,88)))

pixArray = pygame.PixelArray(windowSurface)
pixArray[480][380] = BLACK
del pixArray
windowSurface.blit(text, textRect)
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

谢谢你帮我解决我在pygame中遇到的问题。

2 个回答

0

你可以让你的 drawText 方法返回你的文本对象和文本矩形,然后可以这样使用:

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
    return textobj, textrect

text, text_rect = drawText('8', font, windowSurface, (WINDOWWIDTH / 2.5), (WINDOWHEIGHT / 3))

...

windowSurface.blit(text, text_rect)
0

我觉得你可以试试下面这样的代码:

import pygame, random, sys
from pygame.locals import *

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (197, 153, 114)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
TEXT_COLOR = (255, 255, 255)
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 400

def draw_text(text, font, surface, x, y):
    text_obj = font.render(text, 1, TEXT_COLOR)
    text_rect = text_obj.get_rect()
    text_rect.topleft = (x, y)
    surface.blit(text_obj, text_rect)

pygame.init()
pygame.display.set_caption('polygon!')
windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
basic_font = pygame.font.SysFont(None, 42)
windowSurface.fill(BLACK)

POLYGON_COORDINATES = (
    (158, 80), (181, 88),
    (214, 111), (229, 153),
    (216, 191), (181, 212),
    (135, 207), (102, 181),
    (89, 149), (102, 109),
    (130, 88))

pygame.draw.polygon(windowSurface, GREEN, POLYGON_COORDINATES)

draw_text('8', basic_font, windowSurface,
          (WINDOW_WIDTH / 2.5), (WINDOW_HEIGHT / 3))

pixArray = pygame.PixelArray(windowSurface)
pixArray[480][380] = WHITE
del pixArray

pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

撰写回答