PYGAME:未定义nameError全局名称字体

时间:2016-03-01 20:08:10

标签: python-2.7 pygame

所以我在Pygame中制作这个简单的游戏但是当我尝试在屏幕上显示死亡结束文本时,我得到错误消息nameError:未定义全局名称字体。请帮助,因为我无法看到其他人在互联网上报告此事。 错误出现在第57行。 下面是代码:

import pygame
import time

pygame.font.init()
x = pygame.init()
FPS = 15

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

screenWidth = 600
screenHeight = 600

userX = 350
userY = 250

carX = 250
carY = -20

car2X = 300
car2Y = -80

car3X = 350
car3Y = -160

busX = 300
busY = -280

gameDisplay = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("Highway")
clock = pygame.time.Clock()

block_size = 10

gameOver = False
gameExit = False

def _cars_():
    pygame.draw.rect(gameDisplay, green, ((carX, carY), (10,20))) #car
    pygame.draw.rect(gameDisplay, red, ((car2X, car2Y), (10,20))) #car
    pygame.draw.rect(gameDisplay, yellow, ((car3X, car3Y), (10,20))) #car

    pygame.draw.rect(gameDisplay, blue, ((busX, busY), (10, 40))) #bus

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [screenWidth/2, screenHeight/2])


while not gameExit:

    message_to_screen("CRASH! Try again? [Y/N]", magenta)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_y:
                gameOver = False

    while not gameOver:
        gameDisplay.fill(green)
        pygame.draw.rect(gameDisplay, black, ((225,0),(150,600)))
        pygame.draw.rect(gameDisplay, blue, ((userX, userY), (10,20)))
        pygame.draw.rect(gameDisplay, white, ((275,0), (2.5,600)))
        pygame.draw.rect(gameDisplay, white, ((325,0), (2.5,600)))
        _cars_()



        pygame.display.update()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    userX -= 50

                if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    userX += 50

                if event.key == pygame.K_UP or event.key == pygame.K_w:
                    userY -= 20

                if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    userY += 20

        carY += 10
        car2Y += 10
        car3Y += 10
        busY += 10


        #car move
        if carY > 600 and carX == 250:
            carY = -20
            carX = 350
        if carY > 600 and carX == 350:
            carY = -20
            carX = 300
        if carY > 600 and carX == 300:
            carY = -20
            carX = 250
        #car2 move
        if car2Y > 600 and car2X == 250:
            car2Y = -10
            car2X = 350
        if car2Y > 600 and car2X == 350:
            car2Y = -20
            car2X = 300
        if car2Y > 600 and car2X == 300:
            car2Y = -20
            car2X = 250

            #car3 move
        if car3Y > 600 and car3X == 350:
            car3Y = -30
            car3X = 300
        if car3Y > 600 and car3X == 300:
            car3Y = -30
            car3X = 250
        if car3Y > 600 and car3X == 250:
            car3Y = -30
            car3X = 350
            #bus move
        if busY > 600 and busX == 300:
            busY = -40
            busX = 350
        if busY > 600 and busX == 350:
            busY = -40
            busX = 250
        if busY > 600 and busX == 250:
            busY = -40
            busX = 300

        if userX == carX and userY == carY:
            gameExit = True
        if userX == car2X and userY == car2Y:
            gameOver = True
        if userX == car3X and userY == car3Y:
            gameOver = True
        if userX == busX and userY == busY:
            gameOver = True

        clock.tick(FPS)

pygame.quit()

1 个答案:

答案 0 :(得分:0)

您指的是全局变量font;我怀疑你的意思是改为引用pygame.font

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [screenWidth/2, screenHeight/2])

会变成:

def message_to_screen(msg,color):
    screen_text = pygame.font.render(msg, True, color)
    gameDisplay.blit(screen_text, [screenWidth/2, screenHeight/2])