Dibuja una casa usando la programación Turtle en Python

Prerrequisito: Programación Turtle en Python

Paso 1: Importe el módulo de turtle y matemáticas en Python.

import turtle
import math

Paso 2: Elija un color de fondo para su pantalla de salida. Puede elegir cualquier color, usaremos el color amarillo solo para hacerlo atractivo.

screen = turtle.Screen()
screen.bgcolor("yellow")

Paso 3: Elija el color y la velocidad de su Turtle (bolígrafo) que dibujará la casa en la pantalla.

t.color("black")
t.shape("turtle")
t.speed(1)

Paso 4: Ahora, necesitamos dibujar la base de tu casa y para eso, necesitas dibujar un rectángulo.

Puede rellenar cualquier color de su elección simplemente cambiando el nombre del color en el comando t.fillcolor(‘ ‘). 

t.fillcolor('cyan')
t.begin_fill( )
t.right(90)
t.forward(250)
t.left(90)
t.forward(400)
t.left(90)

t.forward(250)

t.left(90)
t.forward(400)
t.right(90)
t.end_fill()

La base de la casa se verá así:

Paso 5: Ahora que creó la base, el siguiente paso es crear la parte superior de la casa. Dibuja un triángulo para la parte superior, solo para mantenerlo simple.

# for creating triangle
# i.e top of the house
t.fillcolor('brown')
t.begin_fill()
t.right(45)
t.forward(200)
t.right(90)
t.forward(200)
t.left(180)
t.forward(200)
t.right(135)
t.forward(259)
t.right(90)
t.forward(142)
t.end_fill()

Paso 6: Debemos asegurar nuestra casa Poniendo la Puerta y también ventanas para ventilación. Aquí está el código para eso-

# for windows and
# for creating door
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(180)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)

t.right(90)
t.forward(100)
t.right(90)
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
t.forward(75)
t.right(90)
t.forward(200)

Código completo:

Python3

import turtle
  
  
t = turtle.Turtle()
  
# for background
screen = turtle.Screen()
screen.bgcolor("yellow")
  
#color and speed
# of turtle
# creating the house
t.color("black")
t.shape("turtle")
t.speed(1)
  
# for creating base of
# the house
t.fillcolor('cyan')
t.begin_fill()
t.right(90)
t.forward(250)
t.left(90)
t.forward(400)
t.left(90)
t.forward(250)
t.left(90)
t.forward(400)
t.right(90)
t.end_fill()
  
# for top of
# the house
t.fillcolor('brown')
t.begin_fill()
t.right(45)
t.forward(200)
t.right(90)
t.forward(200)
t.left(180)
t.forward(200)
t.right(135)
t.forward(259)
t.right(90)
t.forward(142)
t.end_fill()
  
# for door and
# windows
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(180)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
t.forward(75)
t.right(90)
t.forward(200)
t.right(180)
t.forward(200)
t.right(90)
t.forward(75)
t.left(90)
t.forward(15)
t.left(90)
t.forward(200)
t.right(90)
t.forward(15)
t.right(90)
t.forward(75)

Producción:

Publicación traducida automáticamente

Artículo escrito por anshitaagarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *