2017-09-26 112 views
0

運行我的程序後,我不斷收到錯誤「字體未初始化」。這是該計劃。我知道我正在初始化pygame。我試着同時初始化pygame,pygame.font和他們兩個。我做了一切。我嘗試使用pygame默認字體作爲pygame.font.Font('freesansbold.ttf',115)。那也行不通。字體未初始化

import pygame 
import time 
import random 
pygame.init 
pygame.font.init 


display_width = 800 
display_height = 600 

black = (0,0,0) 
white = (255,255,255) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 

car_width = 100 
car_height = 100 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('A Bit Racey') 
clock = pygame.time.Clock() 

carImg = pygame.image.load ('racecar.png') 
gameOver = pygame.image.load ('gameover.png') 

def gO (x,y): 
    gameDisplay.blit(gameOver, (x,y)) 

def things(thingx, thingy, thingw, thingh, color): 
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) 

def text_objects(text, font): 
    textSurface = font.render (text, True, black) 
    return textSurface, textSurface.get_rect() 

def message_display(text): 
    font = pygame.font.SysFont("C:\Windows\Fonts\Arial.ttf", 115) 
    TextSurf, TextRect = text_objects(text, font) 
    TextRect.center = ((display_width/2), (display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 

    time.sleep(2) 
    game_loop() 

def crash(): 
    message_display('Game Over') 

def car(x,y): 
    gameDisplay.blit(carImg,(x,y)) 

def game_loop(): 

    x = (display_width * 0.45) 
    y = (display_height * 0.8) 

    x_change = 0 

    thing_startx = random.randrange(0, display_width) 
    thing_starty = -600 
    thing_speed = 7 
    thing_width = 100 
    thing_height = 100 

    gameExit = False 

    while not gameExit: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        x_change = -5 
       elif event.key == pygame.K_RIGHT: 
        x_change = 5 

      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 
        x_change = 0 


     x += x_change 

     gameDisplay.fill(white) 

     #thingx, thingy, thinkw, thingh, color) 
     things(thing_startx, thing_starty, thing_width, thing_height, black) 
     thing_starty += thing_speed 
     car (x,y) 

     if x > display_width - car_width or x < 0: 
      crash() 


     if thing_starty > display_height: 
      thing_starty = 0 - thing_height 
      thing_startx = random.randrange(0, display_width) 


     if y < thing_starty+thing_height: 
      print('y crossover') 

      if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and + car_width < thing_startx+thing_width: 
       print('x crossover') 
       gO (display_width/2 - 145.5, display_height/2-200) 
       time.sleep(2) 
       game_loop() 


     if gameExit == True: 
      pygame.quit() 
      quit() 


     pygame.display.update()  
     clock.tick(45) 

game_loop() 
pygame.quit() 
quit() 
+0

無法重現,不會得到任何錯誤 – davedwards

+0

也許錯誤的字體路徑?還是錯字? – NeoVe

回答

0

你只是沒有真正在你的代碼段中運行init函數。

# didn't run function 
pygame.init 
pygame.font.init 
# instead 
pygame.init() 
pygame.font.init() 
+0

謝謝。我從字面上昨天開始python,這是我寫的唯一的一個笑話。我會去做。再次感謝。 –

0

你的問題很奇怪。真奇怪。它甚至沒有任何問題。您未調用功能pygame.init()。你正在評估它。你正在檢查它的存在。如果函數不存在,Python將引發異常,但如果它存在,它將不會執行任何操作。要調用函數,請使用<function_name>(<parameters>)。這個。是。不。紅寶石。

+0

你說得對。謝謝。 (我從來沒有用RUBY寫過,但我明白你的意思)我也應該說我只是在昨天才開始寫Python。再次感謝。 –