Rhys
Rhys

Reputation: 53

Turtle Mini-Project - Udacity - Python - Drawing / moving a shape around another shape

I am trying to make a flower, where yellow lines are drawn out from its approximate center, for the entire circumference.

The issue I have is getting a line to draw out, go to center, and draw out again the same length, but be 10 degrees greater than the previous one.

I can't wrap my mind around how to do this, the line just either bounces back and forth repeatedly, OR it pivots around the side (such as when I make a pointed shape).

So how do I get this yellow line to call back, and incrementally feather out / get that pointed shape to continually draw itself the full 360 degrees?

This is my first 'real' program I guess. I have no prior programing experience, and have only started doing this for a few days. The code I used in the function "draw_feather()" is from another site, because I didn't know how to pivot a triangle in a circle.

import turtle

def draw_fractal(ink):
    for i in range(1,37):
        ink.circle(50)
        ink.right(10)


def draw_feather(ink):
    while True:
        ink.forward(200)
        ink.left(170)
        if abs(ink.pos()) < 1:
            break

def draw_stem(ink):
    ink.goto(1,-400)
    ink.home()
    ink.goto(5,-400)
    ink.home()
    ink.goto(3,-400)

def Draw():
    window = turtle.Screen()
    window.bgcolor =("White")

    R = turtle.Turtle()
    R.shape("circle")
    R.color("Red")
    R.setpos(4,3)
    R.speed(100)

    G = turtle.Turtle()
    G.shape("circle")
    G.color("Green")
    G.setpos(3,3)
    G.speed(100)

    B = turtle.Turtle()
    B.shape("circle")
    B.color("Blue")
    B.setpos(2,3)
    B.speed(100)

    Stem = turtle.Turtle()
    Stem.shape("circle")
    Stem.speed(100)

    Feather = turtle.Turtle()
    Feather.shape("circle")
    Feather.speed(100)
    Feather.color("Yellow")

    draw_fractal(R)
    draw_fractal(G)
    draw_fractal(B)
    draw_feather(Feather)
    draw_stem(Stem)

    window.exitonclick()

Draw()  

Upvotes: 1

Views: 1511

Answers (1)

cdlane
cdlane

Reputation: 41872

Below is a rework of your code to literally do what you described:

import turtle

def draw_flower(ink):
    ink.up()
    ink.home()
    for degrees in range(10, 370, 10):
        ink.down()
        ink.forward(200)
        ink.up()
        ink.home()  # resets angle to 0 degrees
        ink.left(degrees)  # so absolute angle, not relative

def draw_stem(ink):
    ink.goto(1, -400)
    ink.home()
    ink.goto(5, -400)
    ink.home()
    ink.goto(3, -400)

def Draw():
    window = turtle.Screen()
    window.bgcolor("white")

    stem = turtle.Turtle()
    stem.color("green")
    stem.speed("fast")

    draw_stem(stem)

    flower = turtle.Turtle()
    flower.color("yellow")
    flower.speed("fastest")

    draw_flower(flower)

Draw()

turtle.done()

Simply go to the center of the screen, draw a yellow line, reset to the center of the screen, turn slightly and repeat. Note that the turtle.home() method affects the angle of the turtle so we have to adjust for that:

enter image description here

Also, 100 doesn't mean anything to turtle.speed() beyond that it's greater than 10 -- use the symbolic arguments to make clear your intention.

Upvotes: 1

Related Questions