简体   繁体   English

每当我尝试重新运行游戏循环时都会出错

[英]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.

相关问题 每当我尝试运行它时,我都会收到一条错误消息 - I am getting an error message whenever I try and run this 每当我尝试“发射子弹”时,我都会收到错误消息 - I keep getting an error whenever I try to "fire a bullet" 每当我尝试安装新的python软件包时遇到错误 - Whenever I try to install a new python package getting error 每当我尝试访问语音识别程序中的文件时,我总是收到权限拒绝错误 - I keep getting a permission denial error whenever I try to access a file in a speech recognition program 每当我尝试安装lxml时都会收到错误消息? - gets an error whenever I try to install lxml? 为什么每次尝试运行 VS Code python 终端时都会出现语法错误? - Why am I getting a syntax error whenever I try to run VS code python terminal? Python-每当我尝试运行程序“找不到模块”时都会出错 - Python - Getting error whenever I try to run program “Module cannot be found” 每当我尝试使用 pptx 库在 Python 中更新 powerpoint 图表时出现错误 - Getting error whenever I try updating powerpoint chart in Python using pptx library 为什么我会收到属性错误:每当我尝试抓取此电子商务商店时,n.netype object 没有属性 get_text - Why am I getting Attribute error: nonetype object has no attribute get_text whenever I try to scrape this ecommerce store 每当我尝试创建环境时,我都会收到此错误 - whenever I try to create environment I get this error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM