蹦弹圈(Python龟)

时间:2018-05-29 19:45:08

标签: python turtle-graphics pool billiards

我目前正在使用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)

1 个答案:

答案 0 :(得分: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()

enter image description here