2014-03-25 40 views
1

有誰知道我怎樣才能讓分數顯示在我的遊戲畫面上?印刷分數 - Pygame

到目前爲止,我有這樣的代碼:

for bullet in bullet_list: 

     block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) 

     for block in block_hit_list: 
     explosion.play() 
     bullet_list.remove(bullet) 
     all_sprites_list.remove(bullet) 
     score += 10 
     font = pygame.font.Font(None, 36) 
     text = font.render(score, 1, (WHITE)) 
     textpos = text.get_rect(centerx=background.get_width()/2) 
     background.blit(text, textpos) 

    if bullet.rect.y < -10: 
     bullet_list.remove(bullet) 
     all_sprites_list.remove(bullet) 

不過,我收到此錯誤當我運行遊戲和射擊子彈:

「文本= font.render(得分,1,( WHITE))

類型錯誤:文本必須是Unicode或字節」

有誰知道如何解決這個問題呢?

謝謝

回答

4

行,所以首先你問到如何正確繪製這將是就像以前的答案是怎麼做的網站,但你是一個錯,但你BitBlt到屏幕上的方式。

您每次通過for循環時都會在屏幕上顯示分數,這會導致它遇到您遇到的問題。這應該在for循環之後。 例如:

for bullet in bullet_list: 
    block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) 
    for block in block_hit_list: 
     explosion.play() 
     bullet_list.remove(bullet) 
     all_sprites_list.remove(bullet) 
     score += 10 
     #removed this line!!!! ~ font = pygame.font.Font(None, 36) 
     #removed this line to!!!! ~ text = font.render(score, 1, (WHITE)) 
     #remove this line also!!! ~ textpos = text.get_rect(centerx=background.get_width()/2) 
     #finally remove this line!!!! ~ background.blit(text, textpos) 
    if bullet.rect.y < -10: 
     bullet_list.remove(bullet) 
     all_sprites_list.remove(bullet) 
#add in those removed lines after your for loop. 
font = pygame.font.Font(None, 36) 
text = font.render(score, 1, (WHITE)) 
textpos = text.get_rect(centerx=background.get_width()/2) 
background.blit(text, textpos) 

這應該有效。請告訴我,如果你需要任何進一步的幫助。

+0

感謝您的回覆,這款作品完美無瑕,但我仍然存在一個問題,分數連續顯示在彼此之上?在顯示20之前清除分數,而不是顯示10,然後在20等之上顯示10,從而給我留下難以辨認的文字 – Oscar

+0

@Oscar你可以發佈當前的代碼,最好多一點主循環嗎? – KodyVanRy

+0

當然,我把它放在Pastebin上:http://pastebin.com/gLMXz4W6 – Oscar

2

您需要將得分轉換爲字符串。

text = font.render(str(score), 1, (WHITE)) 
+0

完美的作品!只有一個輕微的問題,如果得分超過1分,他們自己的得分就會出現,有什麼方法可以解決這個問題嗎? :)非常感謝您的回覆 – Oscar