你好,我试图运行该功能时,不断收到此错误,并且, 选择一种颜色,在这种情况下,它是粉红色的。python NameError:全球名称
import pygame
进口SYS 进口菜单 进口rgbcolors 从pygame.locals导入* 从rgbcolors导入rgbcolor
高清的main():
# start screen
pygame.init()
screen1 = pygame.display.set_mode((500, 400))
# Fill background
background = pygame.Surface(screen1.get_size())
pygame.draw.line(screen1, (255, 0, 0), (230, 230), (100, 211), 4)
background = background.convert()
background.fill((0, 0,0))
# Displaying text
font = pygame.font.Font(None, 36)
text = font.render("Alien", 23, ((rgbcolor(pink))))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
screen1.blit(background, (0, 0))
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen1.blit(background, (0, 0))
pygame.display.flip()
pygame.time.get_ticks()
if __name__ == "__main__":
main()
,这是我rgbcolor功能。
def rgbcolor(color):
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
darkBlue = (0, 0, 128)
white = (255, 255, 255)
black = (0, 0, 0)
pink = (255, 200, 200)
pass
错误代码。
C:\Python27\python.exe C:/Users/Peter/PycharmProjects/game1/game.py
Traceback (most recent call last):
File "C:/Users/Peter/PycharmProjects/game1/game.py", line 44, in <module>
main()
File "C:/Users/Peter/PycharmProjects/game1/game.py", line 27, in main
text = font.render("Alien", 23, ((rgbcolor(pink))))
NameError: global name 'pink' is not defined
Process finished with exit code 1
你有没有考虑与我们分享整个错误消息? – TigerhawkT3
C:\ Python27 \ python.exe C:/Users/Peter/PycharmProjects/game1/game.py 回溯(最近调用最后一次): 文件“C:/Users/Peter/PycharmProjects/game1/game.py” ,第43行,在 main() 文件“C:/Users/Peter/PycharmProjects/game1/game.py”,第26行,主要在 text = font.render(“Alien”,23,((rgbcolor (粉色)))) NameError:没有定义全局名称'粉色' 过程完成退出代码1 –
您有一个函数不执行任何操作,只是定义局部变量并将它们丢弃,并使用从未使用过的参数,称为有一个参数是一个未定义的变量。我想你会从阅读教程中受益。 – TigerhawkT3