Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a basic tree help
#1
Hi,

I'm new to python and trying to make a basic tree, I have made this code to draw the triangles on top of each other but I am unable to work out how to make them larger as they go down. Can any one help?

number_of_shapes = 3
for shape in range(1, number_of_shapes + 1):
	# Draw a Triangle
    for sides in range(1, 4):
        forward(60)
        left(120)
    # Move forward to start position of next triangle
    penup()
    right(120)
    forward(60)
    left(120)
    forward(30)
    pendown()
Thanks

Charley
Reply
#2
Can you share a working example we can look at? There's at least something you're importing, right?
Reply
#3
Hi,

The only thing I'm importing is

from turtle import *
full code

from turtle import *
number_of_shapes = 3
for shape in range(1, number_of_shapes + 1):
    # Draw a Triangle
    for sides in range(1, 4):
        forward(60)
        left(120)
    # Move forward to start position of next triangle
    penup()
    right(120)
    forward(60)
    left(120)
    forward(30)
    pendown()
This makes 3 triangles stacked on top of each other.

Charley

I can do it this way but I was hope to shorten the code.

from turtle import *
# Draw top of tree
forward(30)
left(120)
forward(60)
left(120)
forward(60)
left(120)
forward(30)
# Move forward to start position of next triangle
penup()
right(90)
forward(70)
left(90)
pendown()
# Draw middle of tree
forward(40)
left(120)
forward(80)
left(120)
forward(80)
left(120)
forward(40)
# Move forward to start position of next triangle
penup()
right(90)
forward(90)
left(90)
pendown()
# Draw bottom of tree
forward(50)
left(120)
forward(100)
left(120)
forward(100)
left(120)
forward(50)
Reply
#4
for shape in range(1, number_of_shapes + 1):
    # Draw a Triangle
    for sides in range(1, 4):
        forward(60 * shape)
        left(120)
Try heading in this direction, where you're basing the amount of movement off of which iteration you're in.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020