Sunday, December 12, 2021

Python / Turtle: Draw a house


 Code:

import turtle
import math

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

# Create your turtle
hay = turtle.Turtle()
hay.color("black")
hay.shape("turtle")
hay.speed(10)

# Define a function to draw and fill a rectangle with
# dimensions and color are given.
def dessinerRectangle(t, largeur, hauteur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  for i in range(2):
      t.forward(largeur)
      t.left(90)
      t.forward(hauteur)
      t.left(90)
  t.end_fill()


# Define a function to draw and fill an isosceles right triangle
# with the length of the hypotenuse and the color are given.
def dessinerTriangle(t, longueur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  t.forward(longueur)
  t.left(135)
  t.forward(longueur / math.sqrt(2))
  t.left(90)
  t.forward(longueur / math.sqrt(2))
  t.left(135)
  t.end_fill()

# Define a function to draw and fill a parallelogram,
# with dimensions and color are given.
def dessinerParallelogram(t, largeur, hauteur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  t.left(30)
  t.forward(largeur)
  t.left(60)
  t.forward(hauteur)
  t.left(120)
  t.forward(largeur)
  t.left(60)
  t.forward(hauteur)
  t.left(90)
  t.end_fill()



# Draw and fill the front of the house
hay.up()
hay.goto(-150, -120)
hay.down()
dessinerRectangle(hay, 100, 110, "blue")

# Draw and fill the front door
hay.up()
hay.goto(-120, -120)
hay.down()
dessinerRectangle(hay, 40, 60, "orange")

# Roof of the house
hay.up()
hay.goto(-150, -10)
hay.down()
dessinerTriangle(hay, 100, "magenta")

# Side of the house
hay.up()
hay.goto(-50, -120)
hay.down()
dessinerParallelogram(hay, 60, 110, "green")

# Window
hay.up()
hay.goto(-30, -60)
hay.down()
dessinerParallelogram(hay, 20, 30, "orange")

# Roof side
hay.penup()
hay.goto(-50, -10)
hay.pendown()
hay.fillcolor("magenta")
hay.begin_fill()
hay.left(30)
hay.forward(60)
hay.left(105)
hay.forward(100 / math.sqrt(2))
hay.left(75)
hay.forward(60)
hay.left(105)
hay.forward(100 / math.sqrt(2))
hay.left(45)
hay.end_fill()




Create a scene that contains a house, a tree, and a sun


import turtle
import math

# Définir la couleur de fond
screen = turtle.Screen()
screen.bgcolor("skyblue")

# Créer votre tortue
hay = turtle.Turtle()
hay.color("black")
hay.shape("turtle")
hay.speed(10)

# Définir une fonction pour dessiner et remplir un rectangle avec
# les dimensions et la couleur sont données.
def dessinerRectangle(t, largeur, hauteur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  for i in range(2):
      t.forward(largeur)
      t.left(90)
      t.forward(hauteur)
      t.left(90)
  t.end_fill()


# Définir une fonction pour dessiner et remplir un triangle rectangle isocèle
# avec la longueur de l'hypoténuse et la couleur sont données.
def dessinerTriangle(t, longueur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  t.forward(longueur)
  t.left(135)
  t.forward(longueur / math.sqrt(2))
  t.left(90)
  t.forward(longueur / math.sqrt(2))
  t.left(135)
  t.end_fill()

# Définir une fonction pour dessiner et remplir un parallélogramme,
# avec les dimensions et la couleur sont données.
def dessinerParallelogram(t, largeur, hauteur, couleur):
  t.fillcolor(couleur)
  t.begin_fill()
  t.left(30)
  t.forward(largeur)
  t.left(60)
  t.forward(hauteur)
  t.left(120)
  t.forward(largeur)
  t.left(60)
  t.forward(hauteur)
  t.left(90)
  t.end_fill()

# Définir une fonction pour dessiner quatre rayons de soleil
# de longueur donnée, pour le soleil du rayon donné.
# La tortue commence au centre du cercle.
def dessinerRayons(t, longueur, rayon):
  t.color("yellow")
  for i in range(4):
    t.up()
    t.forward(rayon)
    t.down()
    t.forward(longueur)
    t.up()
    t.backward(longueur + rayon)
    t.left(90)

# Définir une fonction pour dessiner un oiseau de taille donnée
def dessinerOiseau(t, taille):
  hay.color("black")
  hay.left(45)
  hay.width(4)
  hay.down()
  hay.forward(taille)
  hay.back(taille)
  hay.left(90)
  hay.forward(taille)
  hay.right(135)


# Dessiner et remplir le devant de la maison
hay.up()
hay.goto(-150, -120)
hay.down()
dessinerRectangle(hay, 100, 110, "blue")

# Dessiner et remplir la porte d'entrée
hay.up()
hay.goto(-120, -120)
hay.down()
dessinerRectangle(hay, 40, 60, "orange")

# Toit de la maison
hay.up()
hay.goto(-150, -10)
hay.down()
dessinerTriangle(hay, 100, "magenta")

# Coté de la maison
hay.up()
hay.goto(-50, -120)
hay.down()
dessinerParallelogram(hay, 60, 110, "green")

# Fenêtre
hay.up()
hay.goto(-30, -60)
hay.down()
dessinerParallelogram(hay, 20, 30, "orange")

# Coté du toit
hay.penup()
hay.goto(-50, -10)
hay.pendown()
hay.fillcolor("magenta")
hay.begin_fill()
hay.left(30)
hay.forward(60)
hay.left(105)
hay.forward(100 / math.sqrt(2))
hay.left(75)
hay.forward(60)
hay.left(105)
hay.forward(100 / math.sqrt(2))
hay.left(45)
hay.end_fill()

# Base d'arbre
hay.up()
hay.goto(100, -130)
hay.pendown()
dessinerRectangle(hay, 20, 40, "brown")

# Sommet de l'arbre
hay.penup()
hay.goto(65, -90)
hay.down()
dessinerTriangle(hay, 90, "lightgreen")
hay.up()
hay.goto(70, -50)
hay.down()
dessinerTriangle(hay, 80, "lightgreen")
hay.up()
hay.goto(75, -15)
hay.down()
dessinerTriangle(hay, 70, "lightgreen")
hay.up()
hay.goto(80, 15)
hay.down()
dessinerTriangle(hay, 60, "lightgreen")

# Soleil
hay.up()
hay.goto(55, 110)
hay.fillcolor("yellow")
hay.pendown()
hay.begin_fill()
hay.circle(24)
hay.end_fill()

# Rayons du soleil
hay.up()
hay.goto(55, 134)
hay.down()
dessinerRayons(hay, 25, 24)
hay.right(45)
dessinerRayons(hay, 15, 24)
hay.left(45)

# Oiseaux
hay.up()
hay.goto(100,70)
dessinerOiseau(hay, 20)
hay.up()
hay.goto(80,70)
dessinerOiseau(hay, 10)



# Amenez la tortue jusqu'à la porte d'entrée, et nous avons terminé!
hay.up()
hay.goto(-100, -150)
hay.left(90)
hay.color("black")


No comments:

Post a Comment