Drawing a Christmas Tree 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:

MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
left()angleTurns the turtle counterclockwise
penup()NonePicks up the turtle’s Pen
pendown()NonePuts down the turtle’s Pen
up()NonePicks up the turtle’s Pen
down()NonePuts down the turtle’s Pen
color()Color nameChanges the color of the turtle’s pen
fillcolor()Color nameChanges the color of the turtle will use to fill a polygon
heading()NoneReturns the current heading
position()NoneReturns the current position
goto()x, yMove the turtle to position x,y
begin_fill()NoneRemember the starting point for a filled polygon
end_fill()NoneClose the polygon and fill with the current fill color
dot()NoneLeave the dot at the current position
stamp()NoneLeaves an impression of a turtle shape at the current location
shape()shapenameShould be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Christmas Tree using Python Turtle Module

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 chess game board, the fundamentals are mandatory. If you think about it a Christmas tree is only a brown colored rectangle as the trunk and three green colored triangles stacked on top of each others. Enough of explaining, let’s get coding!

The following is the code for drawing a Christmas tree using python turtle module:

# Python program to draw a tree using turtle
# Importing required modules
import turtle
import math


# Function to draw rectangle
def drawRectangle(t, width, height, color):
	t.fillcolor(color)
	t.begin_fill()
	t.forward(width)
	t.left(90)
	t.forward(height)
	t.left(90)
	t.forward(width)
	t.left(90)
	t.forward(height)
	t.left(90)
	t.end_fill()

	
# Function to draw triangle	
def drawTriangle(t, length, color):
	t.fillcolor(color)
	t.begin_fill()
	t.forward(length)
	t.left(135)
	t.forward(length / math.sqrt(2))
	t.left(90)
	t.forward(length / math.sqrt(2))
	t.left(135)
	t.end_fill()

	
# Set the background color
screen = turtle.Screen ( )
screen.bgcolor("skyblue")


# Creating turtle object
tip = turtle.Turtle()
tip.color ("black")
tip.shape ("turtle")
tip.speed (2)


# Code for the Tree base
tip.penup()
tip.goto(100, -130)
tip.pendown()
drawRectangle(tip, 20, 40, "brown")


# Code for the Tree top
tip.penup()
tip.goto(65, -90)
tip.pendown()
drawTriangle(tip, 90, "lightgreen")
tip.penup()
tip.goto(70, -45)
tip.pendown()
drawTriangle(tip, 80, "lightgreen")
tip.penup()
tip.goto(75, -5)
tip.pendown()
drawTriangle(tip, 70, "lightgreen")

turtle.done()

From above code to draw a Christmas Tree using Python Turtle Module, we can observe:

  • Import the math and turtle modules.
  • Set the size and color of the screen.
  • Too begin, make a turtle object.
  • Make a tree out of stacked triangles and a rectangle.
  • The triangles being light green in color through the drawTriangle() function:

def drawTriangle(t, length, color):
	t.fillcolor(color)
	t.begin_fill()
	t.forward(length)
	t.left(135)
	t.forward(length / math.sqrt(2))
	t.left(90)
	t.forward(length / math.sqrt(2))
	t.left(135)
	t.end_fill()

  • The above function takes t which is our turtle object, length and color.
  • The length is set to 90, 80 and 70. This is because we want o observe a increasing order of our pine slopes from below to above. If you look closely in our output you can observe that our pine slopes (triangle) are decreasing in size.


tip.penup()
tip.goto(65, -90)
tip.pendown()
drawTriangle(tip, 90, "lightgreen")
tip.penup()
tip.goto(70, -45)
tip.pendown()
drawTriangle(tip, 80, "lightgreen")
tip.penup()
tip.goto(75, -5)
tip.pendown()
drawTriangle(tip, 70, "lightgreen")

Output

The output for the code above is:

SHARE Drawing a Christmas Tree using Python Turtle Module

You may also like...

Leave a Reply

Your email address will not be published.

Share