【问题标题】:Boucing ball in a stadium弹跳球在体育场
【发布时间】:2018-06-09 01:15:34
【问题描述】:

我正在尝试使用 Turtle 让球在类似体育场的表面的两侧反弹。我有正确的弹跳角度,但有一些问题:有时,当球碰到圆边时,它不会直接弹跳,而是越过边缘并持续一段时间。

这是程序的一部分:

while nbrebonds>=0: #rebonds means bounce
    forward(1)

    if (xcor()<-150 and distance(-150,50)>100) or (xcor()>150 and distance(150,50)>100):
        trajectoire=heading() 
        normale = towards(0,0)                   #direction de la normale
        trajectoire = 2*normale-180-trajectoire
        print(trajectoire) #trajectoire du rebond
        setheading(trajectoire)
        forward(1)
        nbrebonds+=-1
        print(nbrebonds)


    if ycor()<-50 or ycor()>150:
        trajectoire=heading()
        trajectoire=360-trajectoire
        setheading(trajectoire)
        nbrebonds+=-1
        print(nbrebonds)            

【问题讨论】:

  • 如果没有完整的工作代码,很难重现该问题。
  • @arsho, this answer 填补了一些(尽管显然不是全部)缺失的代码。

标签: python turtle-graphics


【解决方案1】:

您计算normale 方向的方式是错误的:它应该是朝向曲线中心的方向,而不是(0, 0) 点,在这种情况下没有特殊意义。

因此,您可以将代码更改为:

while nbrebonds >= 0: #rebonds means bounce
    forward(1)

    if (xcor() < -150 and distance(-150, 50) > 100) or (xcor() > 150 and distance(150,50) > 100):
        trajectoire = heading() 
        if xcor() < -150:
            normale = towards(-150, 50)  #direction de la normale : vers le centre de la courbe
        else:
            normale = towards(150, 50)
        trajectoire = 2*normale-180-trajectoire
        print(trajectoire)  # trajectoire du rebond
        setheading(trajectoire)
        forward(1)
        nbrebonds -= 1  # plutôt que += -1
        print(nbrebonds)


    if ycor() < -50 or ycor() > 150:
        trajectoire = heading()
        trajectoire = 360-trajectoire
        setheading(trajectoire)
        nbrebonds -= 1
        print(nbrebonds)

【讨论】:

  • 我要补充一点,if ycor() &lt; -50 or ycor() &gt; 150: 可能真的想成为elif ycor() &lt; -50 or ycor() &gt; 150:,因为这两种类型的反弹不应该在同一个更新中发生。
猜你喜欢
  • 2013-05-23
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 2013-10-28
相关资源
最近更新 更多