Python -- pygame 错误 'str 对象没有 'render' 属性

0 投票
2 回答
2838 浏览
提问于 2025-05-01 08:59

我正在用Python制作一个猜字游戏,但在函数def drawGameOver中尝试显示“游戏结束”的消息时,遇到了一个错误:“'str'对象没有'render'属性”。我的代码哪里出问题了?我该如何在屏幕底部中间显示“游戏结束”?我提供了我认为唯一相关的代码。错误出现在“#显示游戏结束消息”注释下面的那一行。谢谢!

# this function draws the game over screen    
def drawGameOver(screen, fontObj, gameOverMsg, secretWord):
# draw a filled rectangle
pygame.draw.rect(screen,(0,0,255),(0,375,640,100))
# draw a border rectangle
# display the game over message
overMsg = fontObj.render(gameOverMsg, True, (255,255,255),(0,0,0))
overRect = overMsg.get_rect()
screen.blit(overMsg,overRect)
# display the secret word
print("")

def main():
# initialize pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((640,440))
# fill the screen w/ white
screen.fill((255,255,255))

fontObj = pygame.font.SysFont('bookantiqua', 28, True, False)
# here is the magic: making the text input
# create an input with a max length of 45,
# and a red color and a prompt saying 'type here: '
txtbx = eztext.Input(x=0, y=350, color=(0,0,0), prompt='You entered: ')

# get the secret word
secretWord = getRandomWord(words)
#variables to hold the incorrect and correct letters
missedLetters = ""
correctLetters = ""
#gameoverMsg is initialized to be empty
gameOverMsg = "Game Over"
#game is not over yet
gameIsDone = False
暂无标签

2 个回答

1

问题是我需要在这个特定的函数里重新定义fontObj。

1

看起来你传入的 fontObj 是一个字符串,而不是一个字体对象。你现在只是在尝试渲染这个字符串。

撰写回答