Now, let's write our Python code in main.py
! You'll be working on two main parts:
draw_simple_landmark
function: This function will draw one house (a blue square with a red triangle roof) at a specific spot.
populate_neighborhood
function: This function will use a loop to call draw_simple_landmark
many times to draw all the houses.
Here are some helpful code examples:
1. How to make a function that draws with a Turtle:
import turtle
def draw_star(my_turtle, x, y, size):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
my_turtle.color("gold")
my_turtle.begin_fill()
for _ in range(5):
my_turtle.forward(size)
my_turtle.right(144)
my_turtle.end_fill()
# You would call it like this:
# artist = turtle.Turtle()
# draw_star(artist, 0, 0, 50)
2. Drawing a square:
import turtle
my_pen = turtle.Turtle()
my_pen.color("blue")
my_pen.begin_fill()
for _ in range(4):
my_pen.forward(40)
my_pen.left(90)
my_pen.end_fill()
3. Drawing a triangle:
import turtle
my_pen = turtle.Turtle()
my_pen.color("green")
my_pen.begin_fill()
my_pen.goto(0, 0) # Start point
my_pen.goto(50, 100) # Peak
my_pen.goto(100, 0) # End point
my_pen.goto(0, 0) # Back to start to close
my_pen.end_fill()
4. Using a for
loop to draw many things:
import turtle
my_artist = turtle.Turtle()
spots = [(-100, 0), (0, 50), (100, -20)]
for x_coord, y_coord in spots:
my_artist.penup()
my_artist.goto(x_coord, y_coord)
my_artist.pendown()
my_artist.dot(10, "purple") # Draw a dot at each spot
5. Setting up your drawing pen (Turtle):
import turtle
my_pen = turtle.Turtle()
my_pen.hideturtle() # Make it invisible
my_pen.speed(0) # Make it super fast!
my_pen.penup() # Lift pen up
my_pen.pendown() # Put pen down
my_pen.color("red") # Set color
my_pen.begin_fill() # Start filling
my_pen.end_fill() # Stop filling