Now, let's write our Python code in main.py
! We need to create two special helpers (functions) for drawing different shapes, and then use them to draw both shapes on the screen.
Here are some helpful code examples:
1. How to make a function that draws a square:
import turtle
def draw_square(my_turtle, x, y, size):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
my_turtle.setheading(0) # Face right
for _ in range(4):
my_turtle.forward(size)
my_turtle.left(90)
my_turtle.penup()
2. How to make a function that draws a triangle:
import turtle
def draw_triangle(my_turtle, x, y, size):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
my_turtle.setheading(0) # Face right
for _ in range(3):
my_turtle.forward(size)
my_turtle.left(120)
my_turtle.penup()
3. How to call your drawing functions:
import turtle
my_artist = turtle.Turtle()
# Call the square drawing function
draw_square(my_artist, -50, 50, 30)
# Call the triangle drawing function
draw_triangle(my_artist, 50, -50, 40)
4. 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!