Skip to content

Commit f79e432

Browse files
committed
turtles
1 parent 852bf7a commit f79e432

8 files changed

Lines changed: 204 additions & 0 deletions

File tree

ftree.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pygame, math
2+
3+
pygame.init()
4+
window = pygame.display.set_mode((600, 600))
5+
pygame.display.set_caption("Fractal Tree")
6+
screen = pygame.display.get_surface()
7+
8+
def drawTree(x1, y1, angle, depth):
9+
if depth:
10+
x2 = x1 + int(math.cos(math.radians(angle)) * depth * 10.0)
11+
y2 = y1 + int(math.sin(math.radians(angle)) * depth * 10.0)
12+
pygame.draw.line(screen, (255,255,255), (x1, y1), (x2, y2), 2)
13+
drawTree(x2, y2, angle - 20, depth - 1)
14+
drawTree(x2, y2, angle + 20, depth - 1)
15+
16+
def input(event):
17+
if event.type == pygame.QUIT:
18+
exit(0)
19+
20+
drawTree(300, 550, -90, 9)
21+
pygame.display.flip()
22+
while True:
23+
input(pygame.event.wait())

turtle/turtle_crawl.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from turtle import *
2+
bgcolor("lightgreen")
3+
shape("turtle")
4+
5+
penup()
6+
size = 20
7+
for i in range(30):
8+
stamp() # Leave an impression on the canvas
9+
size = size + 3 # Increase the size on every iteration
10+
forward(size) # Move tess along
11+
right(24) # and turn her
12+
if i % 2 == 0:
13+
color("red")
14+
else:
15+
color("blue")
16+
17+
mainloop()

turtle/turtle_fractal.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from turtle import *
2+
3+
4+
def f(length, depth):
5+
if depth == 0:
6+
forward(length)
7+
else:
8+
f(length/3, depth-1)
9+
right(60)
10+
f(length/3, depth-1)
11+
left(120)
12+
f(length/3, depth-1)
13+
right(60)
14+
f(length/3, depth-1)
15+
16+
f(500, 4)

turtle/turtle_multi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from turtle import *
2+
3+
4+
def draw_multicolor_square(sz):
5+
"""Make turtle t draw a multi-color square of sz."""
6+
for i in ["red", "purple", "hotpink", "blue"]:
7+
color(i)
8+
forward(sz)
9+
left(90)
10+
11+
bgcolor("lightgreen")
12+
13+
pensize(3)
14+
15+
size = 20 # Size of the smallest square
16+
for i in range(15):
17+
draw_multicolor_square(size)
18+
size = size + 10 # Increase the size for next time
19+
forward(10) # Move along a little
20+
right(18) # and give her some turn
21+
22+
mainloop()

turtle/turtle_snowflake.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from turtle import *
2+
3+
4+
def koch(depth, size):
5+
if depth == 0:
6+
forward(size)
7+
else:
8+
recurse = lambda: koch(depth-1, size/3)
9+
recurse()
10+
left(60)
11+
recurse()
12+
right(120)
13+
recurse()
14+
left(60)
15+
recurse()
16+
17+
koch(3, 3**4)

turtle/turtle_star.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from turtle import *
2+
3+
color('red', 'yellow')
4+
begin_fill()
5+
while True:
6+
forward(200)
7+
left(170)
8+
if abs(pos()) < 1:
9+
break
10+
end_fill()
11+
done()

turtle/turtle_traffic.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from turtle import *
2+
3+
setup(400, 500)
4+
title("Tess becomes a traffic light!")
5+
bgcolor("lightgreen")
6+
7+
8+
def draw_housing():
9+
""" Draw a nice housing to hold the traffic lights """
10+
pensize(3)
11+
color("black", "darkgrey")
12+
begin_fill()
13+
forward(80)
14+
left(90)
15+
forward(200)
16+
circle(40, 180)
17+
forward(200)
18+
left(90)
19+
end_fill()
20+
21+
22+
draw_housing()
23+
24+
penup()
25+
# Position onto the place where the green light should be
26+
forward(40)
27+
left(90)
28+
forward(50)
29+
# Turn into a big green circle
30+
shape("circle")
31+
shapesize(3)
32+
fillcolor("green")
33+
34+
# A traffic light is a kind of state machine with three states,
35+
# Green, Orange, Red. We number these states 0, 1, 2
36+
# When the machine changes state, we change position and
37+
# her fillcolor.
38+
39+
# This variable holds the current state of the machine
40+
state_num = 0
41+
42+
43+
def advance_state_machine():
44+
global state_num
45+
if state_num == 0: # Transition from state 0 to state 1
46+
forward(70)
47+
fillcolor("orange")
48+
state_num = 1
49+
elif state_num == 1: # Transition from state 1 to state 2
50+
forward(70)
51+
fillcolor("red")
52+
state_num = 2
53+
else: # Transition from state 2 to state 0
54+
back(140)
55+
fillcolor("green")
56+
state_num = 0
57+
58+
# Bind the event handler to the space key.
59+
onkey(advance_state_machine, "space")
60+
61+
listen() # Listen for events
62+
mainloop()

turtle/turtle_xmas.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
n = 50.
2+
3+
from turtle import *
4+
speed("fastest")
5+
left(90)
6+
forward(3*n)
7+
color("orange", "yellow")
8+
begin_fill()
9+
left(126)
10+
for i in range(5):
11+
forward(n/5)
12+
right(144)
13+
forward(n/5)
14+
left(72)
15+
end_fill()
16+
right(126)
17+
color("dark green")
18+
backward(n*4.8)
19+
20+
21+
def tree(d, s):
22+
if d <= 0:
23+
return
24+
forward(s)
25+
tree(d-1, s*.8)
26+
right(120)
27+
tree(d-3, s*.5)
28+
right(120)
29+
tree(d-3, s*.5)
30+
right(120)
31+
backward(s)
32+
tree(15, n)
33+
backward(n/2)
34+
35+
while True:
36+
pass

0 commit comments

Comments
 (0)