[英]Getting an error whenever I try to rerun the gameloop
while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False
I have no trouble when I run the game the first time around.我第一次运行游戏时没有问题。 I'm trying to get the game to reset after I've lost but I keep getting the error:我正在尝试在输掉比赛后重置游戏,但我不断收到错误消息:
if event.type == pygame.KEYDOWN:
UnboundLocalError: local variable 'event' referenced before assignment
I don't understand why this is giving me an error, and even less why it gives an error only on the second time around.我不明白为什么这会给我一个错误,更不明白为什么它只在第二次出现错误。 I've tried indenting the block so as to include it inside the for
loop, but it makes my game crash.我试过缩进块以便将它包含在for
循环中,但它使我的游戏崩溃。
The keydown if-else statement should be inside the for-loop keydown if-else 语句应该在 for 循环内
while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.