如何修复此错误,这是我收到的消息:
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中遇到的这个问题
答案 0 :(得分:0)
您可以让drawText
方法返回textobj并使用textrect,然后按如下方式使用:
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)
答案 1 :(得分: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()