Drawing a Snowman using 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’ |
Snowman
The principles governs everything in every subject. They’re the basis on which all incredible exploits are built. To execute acts that appear magical to us simple humans, great scientists and artists equally rely on their most basic skills. Same is the case for python turtle module.
In order to make any drawings in python turtle, for this instance a snowman, the fundamentals are mandatory. If you think about it a snowman is only a bunching of circles overlapping each others for the body and even more circles for buttons and lines for hands and hat . Enough of explaining, let’s get coding!
The following is the code for drawing a snowman using python turtle module
import turtle
SnowMan = turtle.Turtle()
SnowMan.speed(200)
def draw_circle(color, radius, x, y):
SnowMan.penup()
SnowMan.fillcolor(color)
SnowMan.goto(x,y)
SnowMan.pendown()
SnowMan.begin_fill()
SnowMan.circle(radius)
SnowMan.end_fill()
#Below three statements for drawing snowman body
draw_circle("white", 30, 0, -40)
draw_circle("white", 40, 0, -100)
draw_circle("white", 60, 0, -200)
draw_circle("white", 2, -10, -10) #Drawing left eye
draw_circle("white", 2, 10, -10) #Drawing right eye
draw_circle("skyblue", 3, 0, -15) #Drawing nose
#Below three statements for drawing buttons
draw_circle("white", 2, 0, -35)
draw_circle("white", 2, 0, -45)
draw_circle("white", 2, 0, -55)
#Code for drawing left arm
SnowMan.penup()
SnowMan.goto(-15,-35)
SnowMan.pendown()
SnowMan.pensize(5)
SnowMan.goto(-75, -50)
#Code for drawing right arm
SnowMan.penup()
SnowMan.goto(15,-35)
SnowMan.pendown()
SnowMan.pensize(5)
SnowMan.goto(75, -50)
#Code for drawing hat
SnowMan.penup()
SnowMan.goto(-35, 8)
SnowMan.color("black")
SnowMan.pensize(6)
SnowMan.pendown()
SnowMan.goto(35, 8)
SnowMan.goto(30, 8)
SnowMan.fillcolor("black")
SnowMan.begin_fill()
SnowMan.left(90)
SnowMan.forward(15)
SnowMan.left(90)
SnowMan.forward(60)
SnowMan.left(90)
SnowMan.forward(15)
SnowMan.end_fill()
turtle.done()
Explanation
The explanation of our draw_circle() function is given below:
def draw_circle(color, radius, x, y):
SnowMan.penup()
SnowMan.fillcolor(color)
SnowMan.goto(x,y)
SnowMan.pendown()
SnowMan.begin_fill()
SnowMan.circle(radius)
SnowMan.end_fill()
The draw_cirlce() function takes 4 arguments- color and radius of the circle, x and y coordinates where we want to move the turtle.