-
Notifications
You must be signed in to change notification settings - Fork 1
/
OctoMultiTurtle.py
82 lines (69 loc) · 1.68 KB
/
OctoMultiTurtle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# License : GNU GPL v3 or later
# Author : Aurélien DESBRIERES
# Mail : aurelien@hackers.camp
# Project : Octo Multi Turtle
# Created on : Sat Nov 8 14:22:15 2014
#
# Write with Emacs-Nox
#
# References
#
# python2 env
# octo turtle
#
# Course material
#
# MOOC INRIA Turtle
#
# we need the turtle module
import turtle
# avoid calling range for each square
sides = ['east', 'north-east', 'north', 'south-west', 'west', 'south-est', 'south', 'south-west']
def octo(the_turtle, length):
"have the turtle draw a octo of side <length>"
for side in sides:
the_turtle.forward(length)
the_turtle.left(45)
# initialize
window = turtle.Screen()
window.title("Caroline, Chloe && Bob")
# create first turtle
caroline = turtle.Turtle()
caroline.color("hotpink")
caroline.reset()
# second turtle
chloe = turtle.Turtle()
chloe.color("lightgreen")
chloe.reset()
# create third turtle
bob = turtle.Turtle()
bob.color("blue")
bob.reset()
# alternate : turtle, twist and octo size
contexts = ((caroline, 15, 100, ),
(chloe, 60, 30 ),
(bob, 40, 60 ),
)
# initialize alternating contexts
cycle = len(contexts)
counter = -1
# the callback triggered when a user clicks in x,y
def clicked(x, y):
global counter
counter += 1
# alternate between the various contexts
(turtle, twist, size) = contexts[counter % cycle]
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.left(twist)
octo(turtle, size)
# arm callback
turtle.onscreenclick(clicked)
# user can quit by typing 'q'
turtle.onkey(turtle.bye, 'q')
turtle.listen()
# read & dispatch events
turtle.mainloop()