【问题标题】:Bouncing ball in a circle (Python Turtle)弹跳球在一个圆圈(蟒蛇龟)
【发布时间】:2018-05-29 19:45:07
【问题描述】:

我目前正在使用 Turtle 开发一个圆形台球程序。我的问题是,一旦球到达圆的两侧以使其反弹,我无法弄清楚我需要给 Python 什么角度或位置。这是我的程序中需要修复的部分:

while nbrebonds>=0:
        forward(1)
        if (distance(0,y)>rayon): #rayon means radius 
            print(distance(0,y))
            left(2*angleinitial)  #I put this angle as a test but it doesn't work
            forward(1)
            nbrebonds+=(-1)

【问题讨论】:

标签: python turtle-graphics pool billiards


【解决方案1】:

根据我对这个问题的了解,您应该能够使用海龟的heading()towards() 方法计算出您需要什么:

from random import *
from turtle import *

radius = 100
nbrebonds = 10

# draw circle around (0, 0)
penup()
sety(-radius)
down()
circle(radius)

# move turtle to somewhat random position & heading inside circle
penup()
home()
setx(randrange(radius//4, radius//2))
sety(randrange(radius//4, radius//2))
setheading(randrange(0, 360))
pendown()

while nbrebonds >= 0:
    forward(1)

    if distance(0, 0) > radius:

        incoming = heading()
        normal = towards(0, 0)
        outgoing = 2 * normal - 180 - incoming

        setheading(outgoing)

        forward(1)

        nbrebonds -= 1

mainloop()

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 2018-08-24
相关资源
最近更新 更多