简化- if 语句

simplifying- if statements

我这里有这段代码,但我不明白如何简化代码,有没有人知道如何简化这些代码

if ball.ycor() > 340:
    ball.sety(340)
    ball.ycoor *= -1

if ball.ycor() < -340:
    ball.sety(-340)
    ball.ycoor *= -1

if ball.xcor() > 490:
    ball.goto(0,0)
    ball.xcoor *= -1
    scoreboard_a += 1
    score.clear()
    score.write("{}           {}".format(scoreboard_a,scoreboard_b), font=("Arial",104,"normal"))

if ball.xcor() < -490:
    ball.goto(0,0)
    ball.xcoor *= -1
    scoreboard_b += 1
    score.clear()
    score.write("{}           {}".format(scoreboard_a,scoreboard_b), font=("Arial",104,"normal"))


if (ball.xcor() > 440 and ball.xcor() < 450) and ball.ycor() < block_b.ycor() + 40 and ball.ycor() > block_b.ycor() - 40:
    ball.setx(440)
    ball.xcoor *= -1
   

if (ball.xcor() < -440 and ball.xcor() > -450) and ball.ycor() > block_a.ycor() - 40 and ball.ycor() < block_a.ycor() + 40:
    ball.setx(-440)
    ball.xcoor *= -1

您可以将一个值夹在多个比较运算符之间。您可以使用 f 字符串直接将值传递到花括号中。

if ball.ycor() > 340:
    ball.sety(340)
    ball.ycoor *= -1

if ball.ycor() < -340:
    ball.sety(-340)
    ball.ycoor *= -1

if ball.xcor() > 490:
    ball.goto(0, 0)
    ball.xcoor *= -1
    scoreboard_a += 1
    score.clear()
    score.write(f"{scoreboard_a}           {scoreboard_b}", font=("Arial", 104, "normal"))

if ball.xcor() < -490:
    ball.goto(0, 0)
    ball.xcoor *= -1
    scoreboard_b += 1
    score.clear()
    score.write("{scoreboard_a}           {scoreboard_b}", font=("Arial", 104, "normal"))

if 450 > ball.xcor() > 440 and block_b.ycor() - 40 < ball.ycor() < block_b.ycor() + 40:
    ball.setx(440)
    ball.xcoor *= -1
   

if -450 < ball.xcor() < -440 and block_a.ycor() + 40 > ball.ycor() > block_a.ycor() - 40:
    ball.setx(-440)
    ball.xcoor *= -1

此外,您可能希望将 font 值存储到变量中,以便您可以通过使用变量来使用它,例如 font = ("Arial", 104, "normal"),然后

score.write(f"{scoreboard_a}           {scoreboard_b}", font=font)

此外,如果您的代码可以更高效,请只对第一个语句使用if 语句,其余语句使用elif。这样一来,python一旦找到满足条件的语句,就不需要费心去检查其他语句了。

您应该将相互排斥的可能性与 if-else 语句结合起来。我的第一个倾向是评估 ball.ycor()ball.xcor() 一次,假设连续调用没有 return 不同的值:

x_coor = ball.xcor()
y_coor = ball.ycor()

但我注意到表达式 ball.ycoor *= -1,它表明这可能会影响后续调用 ball.ycor() 的 return 值。所以我决定不尝试优化这些调用:

if ball.ycor() > 340:
    ball.sety(340)
    ball.ycoor *= -1
elif ball.ycor() < -340:
    ball.sety(-340)
    ball.ycoor *= -1

if ball.xcor() > 490:
    ball.goto(0,0)
    ball.xcoor *= -1
    scoreboard_a += 1
    score.clear()
    score.write("{}           {}".format(scoreboard_a,scoreboard_b), font=("Arial",104,"normal"))
elif ball.xcor() < -490:
    ball.goto(0,0)
    ball.xcoor *= -1
    scoreboard_b += 1
    score.clear()
    score.write("{}           {}".format(scoreboard_a,scoreboard_b), font=("Arial",104,"normal"))
elif (-450 < ball.xcor() < -440) and ball.ycor() > block_a.ycor() - 40 and ball.ycor() < block_a.ycor() + 40:
    ball.setx(-440)
    ball.xcoor *= -1
elif (440 < ball.xcor() < 450) and ball.ycor() < block_b.ycor() + 40 and ball.ycor() > block_b.ycor() - 40:
    ball.setx(440)
    ball.xcoor *= -1

注意if x > y and x < z:可以改写为if y < x < z: