elif
ステートメントを使用します。elif
「そうでなければ」という意味です。最初の条件が true であることが判明した場合、2 番目の条件はまったくテストされないため、この問題は発生しません。
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
また、注意すべきもう 1 つの点は、ロジックをさらに単純化できることです。する必要はありませんif TutorialOn == True:
。if
ステートメントはそれを行います。そして、TutorialOn
たまたま true でない場合、それに対して可能なもう 1 つのブール値は False だけです! したがって、2 番目の条件をテストする必要はelse
なく、条件をまったく指定しない単純なステートメントを使用できます。条件なしでステートメントを使用すると、前のorステートメントが False であることが判明したelse
場合、それに続くコード ブロックがすぐに実行されます。コードの次の単純化を参照してください。if
elif
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
@Paulo がコメントで指摘したように、さらに単純化することができます。TutorialOn
条件x >= 25 and x <= 175 and y >= 350 and y<= 450
がたまたま真である場合のブール値を反転するだけなので、単純なnot
ステートメントを使用してそれを「トグル」できます。
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