Python for Kids: Draw a Square and a Star with Turtle (Code and Guide)

Python for Kids: Draw a Square and a Star with Turtle (Code and Guide)

Bu yazıyı Türkçe okuyun · Leer en español

What does this project do?

In the first episode of the Python for Kids series, we use the turtle library to make a turtle draw a square and a filled star. We learn forward, left, the for loop, penup, goto and colors. For ages 10 and up who are new to Python.

  • Age group: 10 and up
  • Level: Beginner
  • Time: About 30 minutes
  • What you will learn: the turtle library, forward and left, the for loop, colors and fill

What you need

ItemNotes
ComputerWindows, macOS or Linux
Python 3Free from python.org; IDLE comes with it

Code

In IDLE, open a new window with File → New File, type the code, save it as shapes.py and press F5 to run it.

# Python Turtle: Square and Star - ozandamci.com
import turtle

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

pen = turtle.Turtle()
pen.shape("turtle")
pen.pensize(5)
pen.speed(3)

# 1) Square: 4 sides, 90 degrees at each corner
pen.penup()
pen.goto(-250, -75)
pen.pendown()
pen.color("green")
for i in range(4):
    pen.forward(150)
    pen.left(90)

# 2) Star: 5 points, 144 degrees at each point
pen.penup()
pen.goto(50, 50)
pen.pendown()
pen.color("orange", "gold")
pen.begin_fill()
for i in range(5):
    pen.forward(200)
    pen.right(144)
pen.end_fill()

pen.hideturtle()
turtle.done()

Step by step

  1. The library and the screen: import turtle loads Python’s drawing library. Screen() opens the drawing window and bgcolor() picks the background color.
  2. Get the pen ready: Turtle() puts a turtle on the screen; we named it pen. shape sets its shape, pensize the line width and speed how fast it moves.
  3. Lift the pen and move it: penup() lifts the pen so the turtle moves without drawing. goto(-250, -75) takes it to a point on the screen. pendown() puts the pen down again.
  4. A square with a for loop: range(4) repeats four times. Each time the turtle moves 150 steps forward and turns 90 degrees to the left. The lines inside the loop must start with four spaces.
  5. The star and the fill: color("orange", "gold") makes the line orange and the inside gold. Whatever is drawn between begin_fill() and end_fill() gets filled. We turn 144 degrees for the sharp points of the star.
  6. Finishing: hideturtle() hides the turtle. turtle.done() keeps the window from closing right away.

Try it yourself

  • Draw a hexagon: change 4 and 90 in the square to 6 and 60
  • A bigger star: write 300 instead of 200
  • Your own colors: try colors such as pink, skyblue or purple

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Python for Kids: Draw a Square and a Star with Turtle (Code and Guide)” için 2 yorum

Bir Cevap Yazın