2014-11-03 145 views
0

我正在python中構建一個hang子手遊戲,並且由於某種原因,當試圖通過函數def drawGameOver中的消息顯示遊戲時,出現以下錯誤:「'str'對象沒有屬性'render'」。我的代碼有什麼問題?我將如何去顯示屏幕底部中間的「遊戲結束」?我已經給出了我認爲是唯一相關的代碼。錯誤是針對直接在「#display遊戲結束消息」評論之下的行。謝謝!Python - pygame錯誤'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 
+0

添加您遇到錯誤的行號。 – Stuart 2014-11-03 18:13:06

+0

這個錯誤幾乎意味着它說的是什麼;某處你實際上並沒有傳遞'Font.render()'返回的'pygame.Surface',你只是傳遞一個字符串。 – 2014-11-03 18:20:28

回答

1

它看起來像你傳遞的是fontObj是一個字符串而不是字體對象。這是你嘗試渲染的唯一東西。

+0

你能更具體嗎? fontObj被定義爲一種字體,render()的第一個參數是一個字符串,它應該是。 – jordpw 2014-11-03 18:37:51

+0

您正在此處正確定義fontObj fontObj = pygame.font.SysFont('bookantiqua',28,True,False) 我只能假定在某處之後,或者在調用drawGameOver時,fontObj被重新定義爲字符串。 – Stuart 2014-12-18 14:18:55

0

問題是我需要在這個特定的函數中重新定義fontObj。

相關問題