1
我最近下載了一個在pygame中製作的遊戲的字體,但是當使用字體的提示意味着彈出時,會顯示錯誤。 錯誤讀取:Pygame IOError,無法讀取字體文件
Traceback (most recent call last):
File "/home/pi/Desktop/Python Game.py", line 83, in <module> game_loop()
File "/home/pi/Desktop/Python Game.py", line 78, in game_loop crash()
File "/home/pi/Desktop/Python Game.py", line 29, in message_display largeText = pygame.font.Font
("/home/pi/.fonts/ARCADECLASSIC.TTF",110)
IOError: unable to read font file '/home/pi/.fonts/ARCADECLASSIC.TTF'
我的代碼如下:
def car (x, y)
gameDisplay.blit (carImg, (x, y))
def text_objects (text, font):
textSurface = font.render (text, True, black)
return textSurface, textSurface.get_rect()
def message_display (text):
largeText = pygame.font.Font ("/Home/pi/.fonts/ARCADECLASSIC.TTF, 110)
Textsurf, TextRect-text_objects (text, largeText)
TextRect.center = ((display-width/2), (display-height/2))
gameDisplay.blit (TextSurf, TextRect)
我不知道怎麼回事,因爲我已經下載從其他網站,這意味着它未被損壞相同的字體。
我正在運行Raspbian Wheezy的Raspberry Pi 2上。 這裏是我的代碼(如果必要):
import pygame
import time
pygame.init()
display_width=800
display_height=600
gameDisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Road Obstacles!")
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
car_width=50
clock=pygame.time.Clock()
carImg = pygame.image.load("Racecar.png")
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font("ARCADECLASSIC.TTF",110)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update
time.sleep(2)
game_loop
def crash():
message_display("Woops! You Crashed!")
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
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:
x_change += 5
if event.key == pygame.K_RIGHT:
x_change += -5
x += x_change
gameDisplay.fill(white)
car(x,y)
if x > display_width - car_width or x < 0:
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
我使用的教程由Sentdex在YouTube上教我pygame的幾件事情,但我已經打造成我停止進步這條路塊,因爲我試圖讓這個遊戲成爲他的教程的延伸,而不僅僅是一個副本。
我將不得不檢查一次,我回到家裏,即時在假期,但謝謝你的提示! –
是的!謝謝! –