我得到的错误名称没有定义它显示了这:在
Traceback (most recent call last):File "C:/Users/Shashank Kumar/AppData/Local/Programs/Python/Python37/ssss.py", line 1084, in changeBackgroundAnimation DISPLAYSURF.fill(bgcolor) NameError: name 'bgcolor' is not defined
def changeBackgroundAnimation(animationSpeed=40):
global bgcolor
newBgColor=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
newBgSurf=pygame.Surface((WINDOWWIDTH,WINDOWHEIGHT))
newBgSurf=newBgSurf.convert_alpha()
r,g,b=newBgColor
for alpha in range(0,255,animationSpeed):
checkForQuit()
DISPLAYSURF.fill(bgcolor)
newBgSurf.fill((r,g,b,alpha))
DISPLAYSURF.blit(newBgSurf,(0,0))
drawButtons()
pygame.display.update()
FPSCLOCK.tick(FPS)
bgcolor=newBgColor
NameError: name 'bgcolor' is not defined
global bgcolor
不定义、声明或以其他方式创建变量。它只是声明应该使用一个全局变量,而不是一个局部变量。您仍然需要确保在DISPLAYSURF.fill(bgcolor)
中第一次使用之前定义了它。具体来说,您需要在第一次调用changeBackgroundAnimation
之前为变量赋值。在相关问题 更多 >
编程相关推荐