【问题标题】:PYGAME: nameError global name font is not definedPYGAME:nameError 全局名称字体未定义
【发布时间】:2016-06-14 10:25:08
【问题描述】:

所以我在 Pygame 中制作了这个简单的游戏,但是当我尝试在屏幕上显示死亡结束文本时,我收到错误消息 nameError: global name font is not defined。请帮忙,因为我看不到其他人在互联网上报告此问题。 错误出现在第 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()

【问题讨论】:

  • 你永远不会定义一个名为font的变量

标签: python-2.7 pygame


【解决方案1】:

你指的是一个全局变量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])

【讨论】:

  • 非常感谢您的快速回复,我按照您所说的进行了操作,但出现了另一个错误,attributeError: 'module' object has no attribute render。对不起,如果我唠叨你,但这是我自己编写的第一个游戏,如果事情不起作用,我往往会很快失去动力,无论如何,谢谢。
  • @JakeTraynor 你能链接到你正在学习的教程吗?
  • 我没有在学习教程:/
  • @JakeTraynor 我从来没有用过 PyGame,所以我不能给你一个明确的答案。 pygame.font 模块中似乎没有 render 函数 - renderpygame.font.Font 对象 (see here in the docs) 上的方法。抱歉,我帮不上忙!
  • 这是我正在开发的第一款游戏,不是来自教程。
猜你喜欢
  • 2018-05-04
  • 2015-12-22
  • 2014-04-18
  • 2015-08-08
  • 2011-04-27
  • 2013-09-04
  • 2015-02-26
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多