Drawing a Multi Color Hut 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.

Hut

import turtle
import math

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

# Create our turtle
tom = turtle.Turtle()
tom.color("black")
tom.shape("turtle")
tom.speed(5)

# Define a function to draw and
# fill a rectangle with the given
# dimensions and color
def drawRectangle(t, width, height, color):

	tom.fillcolor(color)
	tom.begin_fill()
	tom.forward(width)
	tom.left(90)
	tom.forward(height)
	tom.left(90)
	tom.forward(width)
	tom.left(90)
	tom.forward(height)
	tom.left(90)
	tom.end_fill()


# Define a function to draw and fill an equalateral right
# triangle with the given hypotenuse length and color. This
# is used to create a roof shape.
def drawTriangle(tom, length, color):
	tom.fillcolor(color)
	tom.begin_fill()
	tom.forward(length)
	tom.left(135)
	tom.forward(length / math.sqrt(2))
	tom.left(90)
	tom.forward(length / math.sqrt(2))
	tom.left(135)
	tom.end_fill()


# Define a function to draw and fill a parallelogram, used to
# draw the side of the house
def drawParallelogram(t, width, height, color):
	tom.fillcolor(color)
	tom.begin_fill()
	tom.left(30)
	tom.forward(width)
	tom.left(60)
	tom.forward(height)
	tom.left(120)
	tom.forward(width)
	tom.left(60)
	tom.forward(height)
	tom.left(90)
	tom.end_fill()


# Draw and fill the front of the house
tom.penup()
tom.goto(-150, -120)
tom.pendown()
drawRectangle(tom, 100, 110, "blue")

# Draw and fill the front door
tom.penup()
tom.goto(-120, -120)
tom.pendown()
drawRectangle(tom, 40, 60, "lightgreen")

# Front roof
tom.penup()
tom.goto(-150, -10)
tom.pendown()
drawTriangle(tom, 100, "magenta")

# Side of the house
tom.penup()
tom.goto(-50, -120)
tom.pendown()
drawParallelogram(tom, 60, 110, "yellow")

# Window
tom.penup()
tom.goto(-30, -60)
tom.pendown()
drawParallelogram(tom, 20, 30, "brown")

# Side roof
tom.penup()
tom.goto(-50, -10)
tom.pendown()
tom.fillcolor("orange")
tom.begin_fill()
tom.left(30)
tom.forward(60)
tom.left(105)
tom.forward(100 / math.sqrt(2))
tom.left(75)
tom.forward(60)
tom.left(105)
tom.forward(100 / math.sqrt(2))
tom.left(45)
tom.end_fill()
turtle.done()

From above code to draw a multicolor hut using python turtle module we can observe we:

  • Turtle module imported.
  • Choose a color for the backdrop.
  • Create a function to draw the hut’s front part, which is a rectangle.
  • Create a function to draw the equilateral triangle at the top of the hut.
  • Create a Parallelogram function to draw the side portion of the hut.
  • To draw the hut’s windows and door, use the penup() and pendown() functions.
  • Fill the Hut with the relevant colors.

Output

SHARE Drawing a Multi Color Hut using Python Turtle Module

You may also like...

Leave a Reply

Your email address will not be published.

Share