所以,我正在尝试制作一个逼真的弹跳功能,乌龟撞到墙上并以相应的角度弹开。我的代码如下所示:
def bounce(num_steps, step_size, initial_heading):
turtle.reset()
top = turtle.window_height()/2
bottom = -top
right = turtle.window_width()/2
left = -right
turtle.left(initial_heading)
for step in range(num_steps):
turtle.forward(step_size)
x, y = turtle.position()
if left <= x <= right and bottom <= y <= top:
pass
else:
turtle.left(180-2 * (turtle.heading()))
所以,这适用于侧壁,但我不知道如何让它从顶部/底部正确反弹。有什么建议么?