Python 名称错误:名称';textsface';没有定义

Python 名称错误:名称';textsface';没有定义,python,pygame,Python,Pygame,播放器矩形与窗口宽度发生冲突后,我现在收到一个错误,提示textSurface尚未定义,即使它已在def消息_显示(文本)下定义: 非常感谢您的帮助 以下是我的源代码: 以下是源视频: 以下是我的代码: # This just imports all the Pygame modules import pygame import time pygame.init() display_width = 800 display_height = 600 # This initates yo

播放器矩形与窗口宽度发生冲突后,我现在收到一个错误,提示textSurface尚未定义,即使它已在def消息_显示(文本)下定义: 非常感谢您的帮助

以下是我的源代码:

以下是源视频:

以下是我的代码:

# This just imports all the Pygame modules 
import pygame
import time

pygame.init()

display_width = 800
display_height = 600

# This initates your colors
black = (0,0,0)
# You have 256 color options so you only use 255 because one of those is 0
white = (255,255,255)
red = (255,0,0)

plyrImg_width = 30

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Bit Racer')
clock = pygame.time.Clock()

plyrImg = pygame.image.load('Sprite-01 double size.png')

def player(x,y):
    gameDisplay.blit(plyrImg,(x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSuface, textSurface.get_rect()


def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    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 killed():
    message_display('You Died')


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    gameExit = False

    while not gameExit:
#         This handles any and all events in the game
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            print(event)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if 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

#        Make sure to write the background before the player
        gameDisplay.fill(white)
        player(x,y)

        if x > display_width - plyrImg_width or  x < 0:
            killed()


        pygame.display.update()
        clock.tick(60)
#     This is how you un-initiate pygame
#     This is what closes the window so always have this

# If you want to end your game loop enter this
game_loop()
pygame.quit()
quit()
#这只是导入所有Pygame模块
导入pygame
导入时间
pygame.init()
显示宽度=800
显示高度=600
#这将初始化您的颜色
黑色=(0,0,0)
#您有256个颜色选项,因此仅使用255,因为其中一个是0
白色=(255255)
红色=(255,0,0)
plyrImg_宽度=30
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('Bit Racer'))
clock=pygame.time.clock()
plyrImg=pygame.image.load('Sprite-01 double size.png')
def播放器(x,y):
游戏显示.blit(plyrImg,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textsface,textssurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def killed():
信息显示(“你死了”)
def game_loop():
x=(显示宽度*0.45)
y=(显示高度*0.8)
x_变化=0
gameExit=False
不退出游戏时:
#这将处理游戏中的所有事件
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
打印(事件)
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-5
如果event.key==pygame.K_RIGHT:
x_变化=5
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x+=x_变化
#确保在播放机之前写下背景
游戏显示。填充(白色)
玩家(x,y)
如果x>display_width-plyrImg_width或x<0:
杀死
pygame.display.update()
时钟滴答(60)
#这就是你如何取消pygame的
#这就是关闭窗口的原因,所以请始终保持此状态
#如果你想结束游戏循环,请输入
博弈(循环)
pygame.quit()
退出

试着拼写正确。表面,而不是表面。

您有一个输入错误。在问题的标题里
textsface
应该是
textsface
。对不起!我是个笨蛋,我已经在电脑前坐了10多个小时,但我的大脑没有意识到这一点!阅读错误消息的次数多于阅读代码的次数。如果您查看错误消息并排除其中的内容,然后尝试通过向下扫描查找错误来调试程序,您将不会非常成功。学习理解错误消息实际告诉您的内容。他们在那里帮助你。