海龟遇到麻烦

时间:2016-10-11 02:53:28

标签: python turtle-graphics

我遇到了海龟的问题。

这是到目前为止的代码:

'''
Levi Davis
This program demonstrates my knowledge on turtles based on problem one in the
third set of homework questions
'''

import turtle
import math

def draw_arc(t, angle, num_segments, length):
    '''
    draws an arc with turtle 't' that has a number of segments equal to 'num_segments'
    and a length equal to 'length' with a turn angle of 'angle' divided by 'num_segments'
    '''

    #this is my calculations on the actual turning of the turtle
    turn_angle = angle // num_segments
    init_turn = ((num_segments - 1) * turn_angle) // 2

    #the initial turn of the turtle and the loop to make the arc
    t.right(init_turn)
    for count in range(num_segments):
        t.forward(length/num_segments)
        t.left(turn_angle)

    #undoing the final turn and the initial turn
    t.right(turn_angle + init_turn)

def draw_squiggle(t, angle, num_segments, length, num_squiggles):
    '''
    This function utilizes draw_arc() to create a squiggly line
    '''
    for x in range(num_squiggles):
        draw_arc(t, angle, num_segments, length)
        draw_arc(t, -(angle), num_segments, length)

def draw_petal(t, angle, num_segments, length):
    '''
    draws a filled in petal shape using 2 arcs and a 180 degree turn
    '''
    t.begin_fill()
    draw_arc(t, angle, num_segments, length)
    t.left(180)
    draw_arc(t, angle, num_segments, length)
    t.left(180)
    t.end_fill()

def draw_leaf(t, angle, num_segments, length):
    draw_petal(t, angle, num_segments, length)
    t.fillcolor(t.pencolor())
    draw_petal(t, angle, num_segments, (length / 2))

wn = turtle.Screen()


bob = turtle.Turtle()
bob.pencolor("tomato2")
bob.pensize(5)
bob.fillcolor("black")


#Because clare told me to I made her into a turtle
clare = turtle.Turtle()
clarepen = "green"
clarefill = "purple"
clare.pencolor(clarepen)
clare.pensize(3)
clare.fillcolor(clarefill)

#testing of functions

   #draw_arc(bob, 100, 10, 10)
   #draw_arc(bob, -100, 10, 10)
   #draw_squiggle(bob, -100, 10, 10, 2)
   #draw_petal(clare, 100, 10, 40)
   #draw_leaf(clare, 100, 10, 40)

#that one shape that is kind of trippy to look at... I think?...
size = 80
def draw_weird_shape(t1, t2):
    startx = -size / 2
    starty = -size / 2 * math.tan(math.radians(144 /2))
    t1.up()
    t1.goto(startx, starty)
    t1.down()
    t2.up()
    t2.goto(startx, starty)
    t2.down()

    t1_fill = "purple"
    t1_pen = "green"

    for count in range(10):
        t1.forward(size/2)
        t1.right(90)
        draw_squiggle(t1, 50, 10, 20, 2)
        draw_leaf(t1, 50, 10, 50)
        t1.pencolor(t1_pen)
        t1.fillcolor(t1_fill)
        t1.right(180)
        draw_squiggle(t1, 50, 10, 20, 2)
        t1.right(90)
        t1.forward(size/2)
        t1.left(36)        
        t2.up()
        t2.forward(size)
        t2.left(18)
        t2.right(90)
        t2.down()
        draw_petal(t2, 50, 5, 25)
        t2.up()
        t2.left(108)

draw_weird_shape(clare, bob)

'''
clare.forward(20)
clare.left(18)
clare.right(90)
clare.forward(20)
clare.left(180)
clare.forward(20)
clare.right(90)
clare.forward(20)
'''

wn.exitonclick()

问题似乎在第100行附近。

当波浪线开始回归时,它不会像它应该的那样在180度转弯时返回。

1 个答案:

答案 0 :(得分:0)

  

当波浪线开始回归时,它不会回到180度   程度转好了。

你的问题是这段代码:

#this is my calculations on the actual turning of the turtle
turn_angle = angle // num_segments
init_turn = ((num_segments - 1) * turn_angle) // 2

通过使用整数除法//,您引入了足够的错误,使得波浪线下面的弧形代码不会回溯相同的路径。而是使用浮点除法/,以使其更准确:

# this is my calculations on the actual turning of the turtle
turn_angle = angle / num_segments
init_turn = ((num_segments - 1) * turn_angle) / 2

enter image description here enter image description here