我一直收到错误"字体未初始化"运行我的程序后。这是该计划。我知道我正在初始化pygame。我尝试过同时初始化pygame,pygame.font和它们两者。我做了一切。我尝试过将pygames默认字体用作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 :(得分:0)
您刚才没有在代码段中运行init
功能。
# didn't run function
pygame.init
pygame.font.init
# instead
pygame.init()
pygame.font.init()
答案 1 :(得分:0)
你的问题很奇怪。真奇怪。它甚至没有任何问题。您没有调用函数pygame.init()
。你正在评估它。你正在检查它的存在。如果函数不存在,Python将引发异常但是如果它存在则不会做任何事情。要调用函数,请使用<function_name>(<parameters>)
。这个。是。不。红宝石。