我有一个模拟赛车,驾驶和躲避街区的python pygame程序,这个程序100%工作,直到我今天尝试它,现在它给了我一个错误,错误是:
Warning (from warnings module):
File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 50
largeText = pygame.font.Font(None,75)
RuntimeWarning: use font: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory
(ImportError: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory)
Traceback (most recent call last):
File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 151, in <module>
begin()
File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 29, in begin
message_display(over)
File "/home/vandeventer/Documents/Pieter/pygame.resies.py", line 50, in message_display
largeText = pygame.font.Font(None,75)
File "/usr/local/lib/python3.4/dist-packages/pygame/__init__.py", line 74, in __getattr__
raise NotImplementedError(MissingPygameModule)
NotImplementedError: font module not available
(ImportError: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory)
这让我觉得我使用的字体有些不对,它真的很令人沮丧,我真的看不出有什么不对,这是我的代码:
import pygame
import time
import random
pygame.init()
display_width = 1500
display_height = 1000
black = (255,255,255)
white = (255,255,255)
red = (255,0,0)
back_ground = (25,25,25)
over = "Start your engins!"
car_width = 50
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("A bit Racey")
clock = pygame.time.Clock()
carImg = pygame.image.load("resieskar.png")
resized = pygame.transform.scale(carImg, (50, 100))
def begin():
message_display(over)
def you_win():
message_display("You Win!")
def things_dodged(count):
font = pygame.font.SysFont(None,25)
text = font.render("Dodged: "+str(count),True,white)
gameDisplay.blit(text, (0,0))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, block_color, [thingx, thingy, thingw, thingh])
def car(x,y):
gameDisplay.blit(resized,(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(None,75)
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('You Crashed!')
def game_loop():
global block_R
global block_G
global block_B
global block_color
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
block_R = random.randrange(100,200)
block_G = random.randrange(100,200)
block_B = random.randrange(30,200)
block_color = (block_R,block_G,block_B)
x = (display_width*0.45)
y = (display_height*0.8)
x_change = 0
thing_width = 100
thing_startx = random.randrange(0, display_width-thing_width)
thing_starty = -600
thing_speed = 5
thing_height = 100
dodged = 0
dificuilty = 2
bigger = 15
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = 50
elif event.key == pygame.K_LEFT:
x_change = -50
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(back_ground)
# things(thingx, thingy, thingw, thingh, color)
things(thing_startx, thing_starty, thing_width, thing_height, block_color)
thing_starty += thing_speed
car(x,y)
things_dodged(dodged)
if dodged > 34:
you_win()
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randint(0,(795-int(thing_width)))
dodged += 1
bigger = bigger * 0.9
dificuilty = dificuilty * 0.6
thing_speed += dificuilty
thing_width += bigger
if y < thing_starty+thing_height:
if x > thing_startx and x < thing_startx+thing_width or x+car_width > thing_startx and x+car_width<thing_startx+thing_width:
crash()
pygame.display.update()
clock.tick(60)
begin()
game_loop()
pygame.quit()
quit()
我使用该字体的唯一部分是message_display()和things_dodged() 我真的很喜欢任何帮助,谢谢
答案 0 :(得分:1)
尝试将largeText = pygame.font.Font(None,75)
更改为largeText = pygame.font.SysFont(None,75)
答案 1 :(得分:0)
非常奇怪,今天我决定尝试弄明白,它完全没问题,无论如何,它现在已经修好了