1

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

1 回答 1

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()

在此处输入图像描述

于 2018-05-29T23:50:48.867 回答