There is a turtle with a pen. When you move the turtle using a simple command, the pen that the turtle has will draw a line that traces the movement of the turtle. Since you can move while lifting the pen, you can draw figures that are not drawn with a single stroke. You can also change the color and thickness of the pen.
Turtle graphics first appeared in 1967 with the LOGO language and are also used in programming education for children. It is easy to understand because the operation by the instruction is visible, and it will be quicker to understand programming by combining various instructions. http://en.wikipedia.org/wiki/Turtle_graphics
On Windows, you can also use Turtle Graphics by installing Python. Other than Window, you may need to install additional external graphics libraries.
First, make sure you have python installed. For Windows OS, please refer to the following and check Add python.exe to Path to install python.
Install python on windows environment http://qiita.com/maisuto/items/404e5803372a44419d60
Next, let's check if the turtle moves.
C:\Users\home> python -m turtle
It should work fine on Windows OS. In other environments, you may see an error similar to the following:
$ python -m turtle
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/lib-tk/turtle.py", line 107, in <module>
import Tkinter as TK
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package
If you get this error, you need to install the python-tk package as described on the last line. For Debian / Ubuntu Linux, install the package as follows.
$ sudo apt-get install python-tk
If it doesn't work at all, try installing the latest version of Python.
Launch the Python interpreter and import the turtle package.
C:\Users\home> python
>>> from turtle import *
You can see what you can command the turtle with dir () and help ().
>>> dir()
['Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'Terminator', 'Turtle', 'TurtleScreen', 'Vec2D', '__builtins__', '__doc__', '__name__', '__package__', 'acos', 'addshape', 'asin', 'atan', 'atan2', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'ceil', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'cos', 'cosh', 'degrees', 'delay', 'distance', 'done', 'dot', 'down', 'e', 'end_fill', 'end_poly', 'exitonclick', 'exp', 'fabs', 'fd', 'fill', 'fillcolor', 'floor', 'fmod', 'forward', 'frexp', 'get_poly', 'getcanvas', 'getpen', 'getscreen','getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'hypot', 'isdown', 'isvisible', 'ldexp', 'left', 'listen', 'log', 'log10', 'lt', 'mainloop', 'mode', 'modf', 'onclick', 'ondrag', 'onkey', 'onrelease', 'onscreenclick', 'ontimer', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pi', 'pos', 'position', 'pow', 'pu', 'radians', 'register_shape', 'reset', 'resetscreen','resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'showturtle', 'sin', 'sinh', 'speed', 'sqrt', 'st', 'stamp', 'tan', 'tanh', 'tilt', 'tiltangle', 'title', 'towards', 'tracer','turtle', 'turtles', 'turtlesize', 'undo', 'undobufferentries', 'up', 'update','width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'ycor']
>>> help(Turtle)
>>> help(foward)
>>> help(back)
>>> help(left)
>>> help(right)
>>> help(up)
>>> help(down)
>>> help(color)
>>> help(clear)
>>> help(bye)
For more information, please refer to the online documentation. http://docs.python.jp/2/library/turtle.html
As a starting point, let's simply move the turtle.
>>> forward(100) #Take 100 steps
>>> circle(100) #Draw a circle with a radius of 100
>>> left(90) #90 degrees to the left
>>> color('red') #Hold a red pen
>>> forward(100) #Take 100 steps
Let's repeat the process and let it draw a pattern.
>>> for i in range(0, 360, 30): #Every 30 degrees from 0 degrees to 360 degrees
... circle(50) #Enter the TAB key at the beginning of the line to indent(Step down)
... left(30) #Enter the TAB key at the beginning of the line to indent(Explanation omitted hereafter)
... #Operation starts by entering a line break at the beginning of the line
>>> color('green'):
>>> for i in range(0, 360, 30):
... circle(30)
... left(30)
... forward(10)
... #Operation starts by entering a line break at the beginning of the line
Let's draw a star using the operation of drawing a star as a function.
>>> def star(size):
... for i in 1,2,3,4,5:
... forward(size)
... right(180 - 180/5)
... #Function definition ends by entering a line break at the beginning of the line
>>> reset()
>>> star(100)
>>> color('green')
>>> begin_fill()
>>> star(50)
>>> end_fill()
>>> def fractal(size, depth=0):
... if depth <= 0:
... forward(size) #Straight line
... else:
... fractal(size/3, depth-1); left(60) #1/Fractal drawing with a length of 3, facing 60 degrees to the left
... fractal(size/3, depth-1); left(-120) #1/Fractal drawing with a length of 3, facing 120 degrees to the right
... fractal(size/3, depth-1); left(60) #1/Fractal drawing with a length of 3, facing 60 degrees to the left
... fractal(size/3, depth-1) #1/Fractal drawing with length of 3
... (Blank line input) #Function definition ends by entering a line break at the beginning of the line
... reset(); fractal(200)
... reset(); fractal(200, 1)
... reset(); fractal(200, 2)
... reset(); fractal(200, 3)
>>> reset(); goto(-100,-100); clear()
>>> for i in "12345":
... fractal(200, 3)
... left(360/5)
... (Blank line input)
That is all for the first part. In the second part, I will play with multiple turtles and add new movements to the turtles.
Recommended Posts