Created
September 8, 2021 14:33
-
-
Save ShambhaviPataskar/0fb25aa41bd0a7f37e9dd38914adeaec to your computer and use it in GitHub Desktop.
I animate a tree using turtle module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
def draw_tree(turt, width, height): | |
draw_trunk(turt, width, height) | |
draw_leaves(turt, width, height) | |
def draw_trunk(turt, width, height): | |
turt.color('brown') | |
turt.begin_fill() | |
turt.setheading(0) #Head to the right | |
turt.forward(width) | |
turt.right(90) | |
turt.forward(height) | |
turt.right(90) | |
turt.forward(width) | |
turt.right(90) | |
turt.forward(height) | |
turt.end_fill() | |
def draw_leaves(turt, width, height, triangles=3): | |
# Draw 3 triangles | |
for i in range(triangles): | |
draw_triangles(turt, width, height) | |
height_increase = height/2 | |
turt.sety(turt.ycor() + height_increase) | |
""" | |
This is the code you wrote, nice try pal | |
:param turt: | |
:param width: | |
:param height: | |
:return: | |
turt.color('green') | |
turt.begin_fill() | |
turt.setheading(0) #Head to the right | |
turt.forward(height) | |
turt.right(60) | |
turt.forward(width) | |
turt.right(60) | |
turt.forward(height) | |
turt.end_fill() | |
""" | |
def draw_triangles(turt, width, height): | |
branch_overhang = height #length of branches overhang from trunk | |
triangle_height = 2*height #A single triangle is 2 times height of the trunk | |
turt.color('green') | |
turt.begin_fill() | |
x_init, y_init = (turt.xcor(), turt.ycor()) | |
x_middle = x_init + width/2.0 | |
x_bottom_left = x_init - branch_overhang | |
x_bottom_right = x_init + width + branch_overhang | |
y_top = y_init + triangle_height | |
turt.goto(x_bottom_left, y_init) | |
turt.goto(x_middle, y_top) | |
turt.goto(x_bottom_right, y_init) | |
turt.goto(x_init, y_init) | |
turt.end_fill() | |
width = 50 | |
height = 100 | |
turt = turtle.Turtle() | |
draw_tree(turt, width, height) | |
turtle.done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment