Drawing a Star, Hippie Flower and Astro Flower with Python Turtle module
Introduction
The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.
The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.
Overview of the syntax
A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).
“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.
Basic syntax:
import turtle // start of the program
//body
//of the main
//code
turtle.done() //end of the program
Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.
Methods
A Python method is a label that can be applied to an object and is a piece of code that may be run on that object.
The most frequently, used turtle methods are:
Method | Parameter | Description |
---|---|---|
Turtle() | None | Creates and returns a new turtle object |
forward() | amount | Moves the turtle forward by the specified amount |
backward() | amount | Moves the turtle backward by the specified amount |
right() | angle | Turns the turtle clockwise |
left() | angle | Turns the turtle counterclockwise |
penup() | None | Picks up the turtle’s Pen |
pendown() | None | Puts down the turtle’s Pen |
up() | None | Picks up the turtle’s Pen |
down() | None | Puts down the turtle’s Pen |
color() | Color name | Changes the color of the turtle’s pen |
fillcolor() | Color name | Changes the color of the turtle will use to fill a polygon |
heading() | None | Returns the current heading |
position() | None | Returns the current position |
goto() | x, y | Move the turtle to position x,y |
begin_fill() | None | Remember the starting point for a filled polygon |
end_fill() | None | Close the polygon and fill with the current fill color |
dot() | None | Leave the dot at the current position |
stamp() | None | Leaves an impression of a turtle shape at the current location |
shape() | shapename | Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’ |
Star
import turtle
tom = turtle.Turtle()
tom.speed(10)
tom.color("red", "pink")
tom.begin_fill()
for i in range (20):
tom.forward(200)
tom.left(135)
tom.end_fill()
turtle.done()
Output
Hippie Flower
Method 1:
import turtle
tom = turtle.Turtle()
tom.color("red", "yellow")
tom.speed(10)
tom.begin_fill()
for i in range(100):
tom.forward(200)
tom.left(165)
tom.end_fill()
turtle.done()
Output
Method 2:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
Output
Astro Flower
from turtle import *
from random import randint
bgcolor('black')
x = 1
speed(0)
while x < 400:
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
colormode(255)
pencolor(r,g,b)
fd(50 + x)
rt(90.991)
x = x+1
exitonclick()