2013-11-01 57 views
1

我在下面的教程,我試圖讓我的文字出現在屏幕上,這裏是我的代碼,但文本將不會出現:pygame的:文本沒有出現

from __future__ import division 
import math 
import sys 
import pygame 


class MyGame(object): 
    def __init__(self): 
     pygame.mixer.init() 
     pygame.mixer.pre_init(44100, -16, 2, 2048) 
     pygame.init() 

     self.width = 800 
     self.height = 600 
     self.screen = pygame.display.set_mode((self.width, self.height)) 

     self.bg_color = 0, 0, 0 

     font = pygame.font.Font(None, 100) 

     text = font.render("text that should appear", True, 238, 58, 140) 


     self.FPS = 30 
     self.REFRESH = pygame.USEREVENT+1 
     pygame.time.set_timer(self.REFRESH, 1000//self.FPS) 


    def run(self): 
     running = True 
     while running: 
      event = pygame.event.wait() 

      if event.type == pygame.QUIT: 
       running = False 

      elif event.type == self.REFRESH: 
       self.draw() 

      else: 
       pass 

    def draw(self): 
     self.screen.fill(self.bg_color) 

     screen.blit(text, [400,300]) 

     pygame.display.flip() 


MyGame().run() 
pygame.quit() 
sys.exit() 

任何想法,爲什麼這是否發生?我是否忘記導入一個圖書館,或者有我的繪製方法嗎?

回答

1

看起來好像您將color這三個RGB值作爲三個單獨的值傳遞給render。它們應該作爲單個元組傳遞。

您還缺少一些self s。 screen.blit(text, [400,300])應該是self.screen.blit(text, [400,300]),並且如果您希望在__init__draw中都可以訪問,則需要將text的所有實例設置爲self.text

from __future__ import division 
import math 
import sys 
import pygame 


class MyGame(object): 
    def __init__(self): 
     pygame.mixer.init() 
     pygame.mixer.pre_init(44100, -16, 2, 2048) 
     pygame.init() 

     self.width = 800 
     self.height = 600 
     self.screen = pygame.display.set_mode((self.width, self.height)) 

     self.bg_color = 0, 0, 0 

     font = pygame.font.Font(None, 100) 

     self.text = font.render("text that should appear", True, (238, 58, 140)) 


     self.FPS = 30 
     self.REFRESH = pygame.USEREVENT+1 
     pygame.time.set_timer(self.REFRESH, 1000//self.FPS) 


    def run(self): 
     running = True 
     while running: 
      event = pygame.event.wait() 

      if event.type == pygame.QUIT: 
       running = False 

      elif event.type == self.REFRESH: 
       self.draw() 

      else: 
       pass 

    def draw(self): 
     self.screen.fill(self.bg_color) 

     self.screen.blit(self.text, [400,300]) 

     pygame.display.flip() 


MyGame().run() 
pygame.quit() 
sys.exit() 

結果:

enter image description here