diff --git a/__pycache__/shapes.cpython-310.pyc b/__pycache__/shapes.cpython-310.pyc index a4d032c..701b84e 100644 Binary files a/__pycache__/shapes.cpython-310.pyc and b/__pycache__/shapes.cpython-310.pyc differ diff --git a/__pycache__/superturt.cpython-310.pyc b/__pycache__/superturt.cpython-310.pyc new file mode 100644 index 0000000..021c646 Binary files /dev/null and b/__pycache__/superturt.cpython-310.pyc differ diff --git a/drawing.py b/drawing.py index 95cebb8..954c5d3 100644 --- a/drawing.py +++ b/drawing.py @@ -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() \ No newline at end of file diff --git a/shapes.py b/shapes.py index 45ffddf..0bbdc75 100644 --- a/shapes.py +++ b/shapes.py @@ -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() - \ No newline at end of file +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) \ No newline at end of file diff --git a/superturt.py b/superturt.py new file mode 100644 index 0000000..8270ebd --- /dev/null +++ b/superturt.py @@ -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() \ No newline at end of file