I made a python program file with the superturtle functions I needed,

because I couldn't figure out how to get superturtle to import right.
I also added the leaves in, added a branch because the leaves didn't
look thick enough, and then I colored in the leaves and the trunk.

I'm not sure if it was the best way to do it, but I couldn't figure out
how to color in the woody part of the tree and the leaves at first (I
was trying to do like nested fillcolors, but only the leaves would
color in still), so I duplicated all of the tree drawing functions and
made one without leaves and one with leaves, and I colored in the wood
in the former, and colored in the leaves in the latter.
This commit is contained in:
root 2024-09-24 19:57:54 -04:00
parent 3876534fb3
commit 1ab20f18ff
5 changed files with 162 additions and 19 deletions

Binary file not shown.

Binary file not shown.

View File

@ -2,12 +2,17 @@
# ----------
# By Stacy S
#
# (Trying to draw a tree.)
# (Drew a tree.)
from turtle import *
from math import sqrt
from shapes import *
from superturt import *
draw_tree(20)
with no_delay():
with restore_state_when_finished():
draw_tree_nl(20)
with restore_state_when_finished():
draw_tree_wl(20)
input()

102
shapes.py
View File

@ -1,23 +1,53 @@
from turtle import *
from math import sqrt
from superturt import *
def draw_leaf(size):
pencolor('black')
fillcolor('green')
begin_fill()
circle(size,90)
right(270)
circle(.75*size,90)
circle(-.25*size,90)
end_fill()
def draw_leaves():
with restore_state_when_finished():
for number in range(11):
draw_leaf(20)
penup()
forward(10)
right(115)
pendown()
def draw_trunk(size):
forward(10*size)
circle(size/8,90)
def draw_branch(size):
angles = [135, 135, 135, 135, 90]
def draw_branch_nl(size):
angles = [150, 150, 150, 150, 150, 150, 90]
for angle in angles:
branch_end(size)
branch_end_nl(size)
circle(size/8,angle)
def tip(size):
def draw_branch_wl(size):
angles = [150, 150, 150, 150, 150, 150, 90]
for angle in angles:
branch_end_wl(size)
circle(size/8,angle)
draw_leaves()
def tip_nl(size):
forward(size)
right(45)
forward(size/10)
right(90)
forward(size/10)
right(45)
forward(size)
def tip_wl(size):
forward(size)
right(45)
forward(size/10)
@ -25,32 +55,70 @@ def tip(size):
forward(size/10)
right(45)
forward(size)
draw_leaves()
def branching(ang1, d1, ang2, d2, ang3):
def branching_nl(ang1, d1, ang2, d2, ang3):
right(ang1)
forward(d1)
right(ang2)
forward(d2)
right(ang3)
def branch_end(size):
forward(4*size)
def branching_wl(ang1, d1, ang2, d2, ang3):
right(ang1)
forward(d1)
right(ang2)
forward(d2)
right(ang3)
draw_leaves()
def branch_end_wl(size):
for number in range(3):
forward(size)
draw_leaves()
forward(size)
right(300)
tip(size)
branching(240,size,330,size,330)
tip(size)
branching(30,size,240,size,30)
tip(size)
branching(330,size,330,size,240)
tip(size)
tip_wl(size)
branching_wl(240,size,330,size,330)
tip_wl(size)
branching_wl(30,size,240,size,30)
tip_wl(size)
branching_wl(330,size,330,size,240)
tip_wl(size)
right(300)
forward(4*size)
def draw_tree(size):
def branch_end_nl(size):
forward(4*size)
right(300)
tip_nl(size)
branching_nl(240,size,330,size,330)
tip_nl(size)
branching_nl(30,size,240,size,30)
tip_nl(size)
branching_nl(330,size,330,size,240)
tip_nl(size)
right(300)
forward(4*size)
def draw_tree_nl(size):
fillcolor('brown')
begin_fill()
circle(size/4,90)
draw_trunk(size)
draw_branch(size)
draw_branch_nl(size)
forward(10*size)
circle(size/4,90)
right(180)
forward(2*size)
end_fill()
def draw_tree_wl(size):
penup()
circle(size/4,90)
draw_trunk(size)
draw_branch_wl(size)
forward(10*size)
circle(size/4,90)
right(180)
forward(2*size)

70
superturt.py Normal file
View File

@ -0,0 +1,70 @@
from turtle import *
from itertools import chain, cycle
class no_delay:
"""A context manager which causes drawing code to run instantly.
For example::
from turtle import forward, right
from superturtle.movement import fly, no_delay
fly(-150, 150)
with no_delay():
for i in range(720):
forward(300)
right(71)
input()
"""
def __enter__(self):
self.n = tracer()
self.delay = delay()
tracer(0, 0)
def __exit__(self, exc_type, exc_value, traceback):
update()
tracer(self.n, self.delay)
def update_position(x, y=None):
"""
Updates the turtle's position, adding x to the turtle's current x and y to the
turtle's current y.
Generally, this function should be called with two arguments, but it may
also be called with a list containing x and y values::
from superturtle.movement import update_position
update_position(10, 20)
update_position([10, 20])
"""
if y is None:
x, y = x
current_x, current_y = position()
penup()
goto(x + current_x, y + current_y)
pendown()
class restore_state_when_finished:
"""
A context manager which records the turtle's position and heading
at the beginning and restores them at the end of the code block.
For example::
from turtle import forward, right
from superturtle.movement import restore_state_when_finished
for angle in range(0, 360, 15):
with restore_state_when_finished():
right(angle)
forward(100)
"""
def __enter__(self):
self.position = position()
self.heading = heading()
def __exit__(self, *args):
penup()
setposition(self.position)
setheading(self.heading)
pendown()