2015-02-08 187 views
1

我正在製作一個遊戲,其中您點擊一個硬幣並獲得硬幣,而我正在嘗試顯示您獲得的硬幣數量,但不會顯示。Pygame如何顯示一個變量?

代碼:

text = basicFont.render(str(coins), True, WHITE, BLACK) 
textRect = text.get_rect() 
textRect.centerx = windowSurface.get_rect().centerx 
textRect.centery = windowSurface.get_rect().centery 

回答

0

您可以使用類似這樣的東西:

score = 0 
score_font = pygame.font.Font(None, 50) 
score_surf = score_font.render(str(score), 1, (0, 0, 0)) 
score_pos = [10, 10] 

score這裏是可變的,它可以通過函數的類(ES)來改變。 score_font將確定文本將要處理的字體和大小。將使用score_surf將文本渲染到表面上。它將需要帶有必要字符串的變量,數字1(我不太確定原因)以及文本的顏色。將使用score_pos將文本粘貼到給定的特定座標上。以下是你如何將文字投射到屏幕上:

screen.blit(score_surf, score_pos) 

我希望這可以幫助你!

1

一旦你讓你的對象,繪製到你的屏幕windowSurface.blit(text, (x1, y1)。然後致電pygame.display.flip()來顯示它。

如:

import pygame, sys 
from pygame.locals import * 

pygame.init() 
windowSurface = pygame.display.set_mode() 

myfont = pygame.font.SysFont("monospace", 15) 

while True 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
    # render text 
    label = myfont.render("Some text!", 1, (255,255,0)) 
    windowSurface.blit(label, (100, 100)) 
    pygame.display.flip() 
+0

你的答案沒有用。我用下面的代碼:#中設置的文本 文本= basicFont.render(STR(硬幣),真,白,黑) textRect = text.get_rect() textRect.centerx = 240 textRect.centery = 240 windowSurface.blit(text,(x1,y1) pygame.display.flip()它說它有無效的語法 – AnonymousPerson 2015-02-09 23:15:53

+0

@AnonymousPerson請在你的問題中發佈完整的回溯作爲編輯 – 2015-02-10 00:10:50

+0

當我運行它時,它說「語法無效「,pygame.display.flip中的pygame顯示爲紅色。 – AnonymousPerson 2015-02-11 21:18:21