Python 正在尝试在pygame中创建切换类型按钮。有问题

Python 正在尝试在pygame中创建切换类型按钮。有问题,python,pygame,Python,Pygame,好的,这里的问题是,在恢复单击时,第一个条件得到满足,变量被设置为true。紧接着,事件就像。。。如果变量为false,则满足第二个条件,这会立即将变量重置为true。有没有办法解决这类问题 for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN : x, y = event.pos if TutorialOn == True: if x &

好的,这里的问题是,在恢复单击时,第一个条件得到满足,变量被设置为true。紧接着,事件就像。。。如果变量为false,则满足第二个条件,这会立即将变量重置为true。有没有办法解决这类问题

    for event in pygame.event.get():    
    if event.type == MOUSEBUTTONDOWN :
        x, y = event.pos
        if TutorialOn == True:
            if x >= 25 and x <= 175 and y >= 350 and y<= 450:
                TutorialOn = False 
        if TutorialOn == False:
            if x >= 25 and x <= 175 and y >= 350 and y<= 450:
                TutorialOn = True
pygame.event.get()中事件的
:
如果event.type==MOUSEBUTTONDOWN:
x、 y=event.pos
如果TutorialOn==True:

如果x>=25,x=350,y=25,x=350,y使用
elif
语句<代码>elif
表示“如果发生其他情况”。如果第一个条件为真,那么您的第二个条件根本不会被测试,那么您就不会有这个问题

for event in pygame.event.get():    
if event.type == MOUSEBUTTONDOWN :
    x, y = event.pos
    if TutorialOn == True:
        if x >= 25 and x <= 175 and y >= 350 and y<= 450:
            TutorialOn = False 
    elif TutorialOn == False:
        if x >= 25 and x <= 175 and y >= 350 and y<= 450:
            TutorialOn = True

正如@Paulo在评论中指出的,你可以做进一步的简化。因为你所要做的就是在条件
x>=25和x=350,y=25和x=350,而我觉得自己很笨的时候,翻转
TutorialOn
的布尔值。谢谢。它可以进一步简化为:
如果25@PauloAlmeida是的,可以使用布尔切换。很好的观点。我看到你添加了我的建议,但是如果TutorialOn:
,你不应该有
,因此如果
为False,它也会切换到
@Paulo抱歉,我已经修复了它。谢谢。:)
for event in pygame.event.get():    
if event.type == MOUSEBUTTONDOWN :
    x, y = event.pos
    if TutorialOn:
        if x >= 25 and x <= 175 and y >= 350 and y<= 450:
            TutorialOn = False 
    else:
        if x >= 25 and x <= 175 and y >= 350 and y<= 450:
            TutorialOn = True
for event in pygame.event.get():    
if event.type == MOUSEBUTTONDOWN :
    x, y = event.pos
    if x >= 25 and x <= 175 and y >= 350 and y<= 450:
        TutorialOn = not TutorialOn