如何解决“未定义largeText”错误?,我不确定是否要在代码开头定义它

时间:2019-06-26 09:05:01

标签: python-3.x pygame

我正在制作一个关于在喷气式飞机巡航时躲避太空垃圾的python游戏,出于某种原因,尽管我因为没有定义名称“ largeText”而收到NameError。

我一直是在教程的帮助下创建的,我不确定是否错过了重要的事情。

这是针对软件设计和开发HSC课程评估任务的。

本来也应该是smallText,但是它还带有未定义的NameError。

def button(msg,x,y,w,h,ic,ac):
    mouse = pygame.mouse.get_pos()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smalltext = pygame.font.SysFont('freesansbold.tff', 20)
    textSurf, textRect = text_objects("GO!", largeText)
    textRect.center = ((x+(w/2)), (y+(h/2)))
    gameDisplay.blit(TextSurf, TextRect)

def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        largeText = pygame.font.SysFont('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Space Dodgers!", largeText)
        TextRect.center = ((display_width/2),(display_height/2))

        button("GO!",150,450,100,50,green,bright_green)
        button("Quit!",550,450,100,50,red,bright_red)

1 个答案:

答案 0 :(得分:1)

了解scopes and namespace

您定义:

from sympy import init_session

init_session()

x = 5
solve(x**2 - 33*x + 20*13 - 14**2, x)

largeText = pygame.font.SysFont('freesansbold.ttf',115) 函数内部。

然后在game_intro()函数中使用:

button()

我猜你的错误来自这一行。 textSurf, textRect = text_objects("GO!", largeText) 对其他函数中定义的变量一无所知,因此您得到了button

  

NameError:名称'largeText'未定义

一般建议是将NameError所需的任何内容作为函数的参数传递,但是在您的情况下,我认为这是一个更简单的错误。您可能想在button函数中将largeText替换为smalltext,因为您之前已经定义了button()

smalltext