Draw a house using Python Turtle

Draw a house using Python Turtle

Introduction

Hello everyone, welcome to violet-cat-415996.hostingersite.com. We all have drawn a small house using Python Turtle with green grass mountains, sun, etc. in our childhood using paper and pen. Here we are drawing the same but the source is different. We are going to use the python turtle library. The code is very simple to understand, Trust me!!!.  You just have to understand it line by line and you will get it.

For more understanding, we have provided a detailed explanation of the code. Comments are provided so that all the new python learners understand the code quickly.

Code to Draw a house using Python Turtle

#import turtle
import turtle


#import turtle as t
t=turtle.Turtle()
scr=turtle.getscreen()
scr.bgcolor("SkyBlue1")

#create the body of house
t.penup()
t.pensize(3)
t.color("black","yellow")
t.begin_fill()
t.goto(-200,-150)
t.pendown()
t.forward(400)
t.left(90)
t.forward(200)
t.left(90)
t.forward(400)
t.left(90)
t.forward(200)
t.left(90)
t.end_fill()

#create partiton
t.forward(100)
t.left(90)
t.forward(200)
t.left(90)
t.forward(100)
t.end_fill()

#create roof
t.color("black","brown")
t.begin_fill()
t.right(120)
t.forward(100)
t.right(120)
t.forward(100)
t.end_fill()
t.begin_fill()
t.backward(100)
t.left(60)
t.forward(300)
t.right(60)
t.forward(100)
t.end_fill()

#create door
t.penup()
t.goto(-200,-150)
t.setheading(0)
t.pendown()
t.color("black","pink")
t.forward(230)
t.left(90)
t.begin_fill()
t.forward(90)
t.right(90)
t.forward(50)
t.right(90)
t.forward(90)
t.end_fill()
t.penup()

#create left window1
t.goto(-60,-50)
t.pendown()
t.color("black","pink")
t.begin_fill()
t.backward(50)
t.left(90)
t.forward(60)
t.right(90)
t.forward(50)
t.right(90)
t.forward(60)
t.right(90)
t.forward(25)
t.right(90)
t.forward(60)
t.backward(30)
t.right(90)
t.forward(25)
t.backward(50)
t.end_fill()


#creqte right window
t.penup()
t.setheading(270)
t.goto(100,-50)
t.pendown()
t.color("black","pink")
t.begin_fill()
t.backward(50)
t.left(90)
t.forward(60)
t.right(90)
t.forward(50)
t.right(90)
t.forward(60)
t.right(90)
t.forward(25)
t.right(90)
t.forward(60)
t.backward(30)
t.right(90)
t.forward(25)
t.backward(50)
t.end_fill()
t.penup()

#grass
t.color("black","green")
t.goto(-650,-150)
t.left(90)
t.pendown()
t.begin_fill()
t.forward(1300)
t.right(90)
t.forward(180)
t.right(90)
t.forward(1300)
t.right(90)
t.forward(180)
t.end_fill()
t.penup()

#create circle of sun
t.goto(-500,150)
t.pendown()
t.color("yellow","yellow")
t.begin_fill()
t.circle(45)
t.end_fill()
t.goto(-545,150)

#create sunrays
t.pensize(5)
for i in range(12):
        t.forward(80)
        t.backward(80)
        t.left(30)
t.penup()

#create clouds
t.goto(500,160)
t.pendown()
t.color("white","white")
t.setheading(90)
t.begin_fill()
t.circle(60,180)
t.end_fill()
t.goto(440,160)
t.setheading(90)
t.begin_fill()
t.circle(80,180)
t.end_fill()

turtle.done()

Output:

Output of House from Python Turtle

Now, let’s understand the code Draw a house using Python Turtle by breaking it into parts.

Import Turtle

import turtle

Importing the turtle module allows us to use its inbuilt methods and functions in our program.

Setting the Turtle

#import turtle as t
t = turtle.Turtle()
scr = turtle.getscreen()
scr.bgcolor("SkyBlue1")

Here we have created the turtle object as t, and set the screen object to scr. The background color is set to Skyblue with the help of screen object scr.

Drawing the body of the house using a python turtle

#create the body of house
t.penup()
t.pensize(3)
t.color("black","yellow")
t.begin_fill()
t.goto(-200,-150)
t.pendown()
t.forward(400)
t.left(90)
t.forward(200)
t.left(90)
t.forward(400)
t.left(90)
t.forward(200)
t.left(90)
t.end_fill()

#create partiton
t.forward(100)
t.left(90)
t.forward(200)
t.left(90)
t.forward(100)
t.end_fill()

In this block of code, we have set the turtle’s pen size to 3 and the pen color to black. The color is set to yellow. Set the position of the turtle to goto(-200,-150) which is the starting position of the turtle. We have made use of the forward and left functions twice to get the proper rectangular block of the house.

Also drawn a straight line that displays a partition of the house.

Draw the roof of the house

#create roof
t.color("black","brown")
t.begin_fill()
t.right(120)
t.forward(100)
t.right(120)
t.forward(100)
t.end_fill()
t.begin_fill()
t.backward(100)
t.left(60)
t.forward(300)
t.right(60)
t.forward(100)
t.end_fill()

In this block of code, we are drawing the roof of the house. We have set the color to brown and the pen color to black. We have made use of the right(120)  and forward(100) functions twice to draw the triangle of the roof. And the rest of the functions are used to complete the entire roof.

Draw the door

#create door
t.penup()
t.goto(-200,-150)
t.setheading(0)
t.pendown()
t.color("black","pink")
t.forward(230)
t.left(90)
t.begin_fill()
t.forward(90)
t.right(90)
t.forward(50)
t.right(90)
t.forward(90)
t.end_fill()
t.penup()

Here we have set the position of the turtle to the starting point, where we have begun drawing our house block i.e goto(-200,-150). Setting the head position to 0 degrees points to the right. We moved our turtle forward(230) and later we drew the door using the forward and right functions. We have used the penup function as we are not drawing anything.

Draw the left window of the house using python turtle

#create left window1
t.goto(-60,-50)
t.pendown()
t.color("black","pink")
t.begin_fill()
t.backward(50)
t.left(90)
t.forward(60)
t.right(90)
t.forward(50)
t.right(90)
t.forward(60)
t.right(90)
t.forward(25)
t.right(90)
t.forward(60)
t.backward(30)
t.right(90)
t.forward(25)
t.backward(50)
t.end_fill()

In this part, we have drawn the left window of the house and set the turtle’s position to goto(-60,-50). Here we have made use of the backward (50) function as our turtle was already pointing 270 degrees. So here we don’t have to set the heading of the turtle again, we just have to set the angle. The window length and width are 60 and 50. The size of the inner block is 30 and 25 respectively.

Draw the right window of the house using python Turtle

#creqte right window
t.penup()
t.setheading(270)
t.goto(100,-50)
t.pendown()
t.color("black","pink")
t.begin_fill()
t.backward(50)
t.left(90)
t.forward(60)
t.right(90)
t.forward(50)
t.right(90)
t.forward(60)
t.right(90)
t.forward(25)
t.right(90)
t.forward(60)
t.backward(30)
t.right(90)
t.forward(25)
t.backward(50)
t.end_fill()
t.penup()

Here we have drawn the right window of the house. Similar are the functions that we have used while drawing the left window. The only difference is we have set the heading position to 270 degrees. The turtle’s position is set to goto(100,-50).

Draw the grass

#grass
t.color("black","green")
t.goto(-650,-150)
t.left(90)
t.pendown()
t.begin_fill()
t.forward(1300)
t.right(90)
t.forward(180)
t.right(90)
t.forward(1300)
t.right(90)
t.forward(180)
t.end_fill()
t.penup()

In this block, we have drawn the grass for the house we have set the pen color to black and the color for the grass as green. We have set the position of our turtle to goto(-650,-150). We have drawn the grass by setting the length as 1300 and width as 180.

Draw the Sun

#create circle of sun
t.goto(-500,150)
t.pendown()
t.color("yellow","yellow")
t.begin_fill()
t.circle(45)
t.end_fill()
t.goto(-545,150)

#create sunrays
t.pensize(5)
for i in range(12):
        t.forward(80)
        t.backward(80)
        t.left(30)
t.penup()

In this block of code, we have set the position of our sun to goto(-500,150). The pen color and the color of the sun are the same as yellow with a radius of 45.

For generating sun rays, we have used for loop, which allows me to generate the rays 12 times. The forward and backward function to go and come back to their original position. And the angle between each ray is 30 degrees.

Drawing the clouds of the house

#create clouds
t.goto(500,160)
t.pendown()
t.color("white","white")
t.setheading(90)
t.begin_fill()
t.circle(60,180)
t.end_fill()
t.goto(440,160)
t.setheading(90)
t.begin_fill()
t.circle(80,180)
t.end_fill()


turtle.done()

In this last part code, we have drawn 2 semicircles. Each of the radius 60 and 80 respectively and drawn at 2 different positions i.e goto(500,160) and goto(440,160).

So here we have completed the drawing of the house successfully. Please let us know the comments for the article. Thank you for reading this article.


Also Read:

Share:

Author: Ayush Purawr