Draw on the page or use an extra window. under construction
If you like it, use it. If you have some suggestions, tell me (gkvoelkl@nelson-games.de).
The most examples are inspired by Harold Abelson, Andrea diSessa: Turtle Geometry, MIT Press 1986
In [ ]:
from ipyturtle import Turtle
t = Turtle()
t
The turtle is drawn on its own part of the screen.
Or integrate the turtle graphic into the page
In [ ]:
t = Turtle(fixed=False, width=100, height=100)
t
With width and height you can change the extension of the drawing canvas.
In [ ]:
t.right(90)
t.heading()
In [ ]:
t.forward(150)
t.left(45)
In [ ]:
t.back(100)
In [ ]:
t.left(45)
t.penup()
t.forward(100)
In [ ]:
t.reset() #clear canvas and start again
In [ ]:
t.back(40)
t.forward(100)
t.position()
In [ ]:
def square(size):
for i in range(4):
t.forward(size)
t.right(90)
In [ ]:
square(20)
In [ ]:
t.reset()
In [ ]:
def triangle(size):
for i in range(3):
t.forward(size)
t.right(120)
In [ ]:
triangle(100)
In [ ]:
t.reset()
In [ ]:
def house(size):
square(size)
t.forward(size)
t.right(30)
triangle(size)
In [ ]:
t.back(100)
house(100)
In [ ]:
t = Turtle(fixed=False, width=120, height=120)
def circle():
for i in range(360):
t.forward(1)
t.right(1)
t
In [ ]:
circle()
In [ ]:
t.reset()
In [ ]:
def poly(side, angle):
turn = 0
while turn == 0 or turn % 360 != 0:
t.forward(side)
t.right(angle)
turn += angle
In [ ]:
poly(44,135)
In [ ]:
t.reset()
Return the current pen color as RGB tuple or web color name
In [ ]:
t.pencolor()
Set pen color as web color name
In [ ]:
t.pencolor('Green')
Set pen color with RGB value
In [ ]:
t.pencolor(255,0,0)
In [ ]:
t.forward(40)
t.right(120)
t.pencolor('Blue')
t.forward(40)
In [ ]:
def lbranch(length, angle, level):
t.pencolor('Green')
t.forward(2*length)
node(length, angle, level)
t.back(2*length)
def rbranch(length, angle, level):
t.pencolor('Brown')
t.forward(length)
node(length, angle, level)
t.back(length)
def node(length, angle, level):
if level==0:
return
t.left(angle)
lbranch(length, angle, level-1)
t.right(2*angle)
rbranch(length, angle, level-1)
t.left(angle)
In [ ]:
t.reset()
node(8,24,7)
In [ ]:
def nested_triangle(size):
if size < 10:
return
for i in range(3):
nested_triangle(size/2)
t.forward(size)
t.right(120)
In [ ]:
t.reset()
nested_triangle(100)
In [ ]:
def snowflake(size, level):
for i in range(3):
side(size, level)
t.right(120)
def side(size, level):
if level == 0:
t.forward(size)
return
side(size/3, level - 1)
t.left(60)
side(size/3, level - 1)
t.right(120)
side(size/3, level - 1)
t.left(60)
side(size/3, level - 1)
In [ ]:
t.reset()
snowflake(100,4)
In [ ]:
t.reset()
sideLength = 40
for square in range(5):
for side in range(4):
t.forward(sideLength)
t.left(90)
sideLength += 10
0.2.0 First published version
0.2.4 More Examples. Moveable Windows.
More about Creating Custom Widgets in IPython https://github.com/ipython/ipywidgets
A template Widget Project https://github.com/ipython/ipywidgets
In [ ]: