Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python turtle graphics crash course
robro12910 (1)

Hi! Welcome to a simple python turtle crash course! In this course, you will be learning the basics of python turtle graphics

First, you will need to import the turtle module

import turtle

Next, you will need to make a variable for the turtle and define it

t=turtle.Turtle()

Ok, now that you have done that, we now can define the shape you want your pen to be (if you don't want any specific shape, then just don't do this part)

t.shape("turtle")

If you want, you can change the color of the pen with this line of code(You can use the color of your choice):

t.color("blue")

Good! Now we can start drawing! Here are some lines of code for moving the pen:

t.forward(100)
t.right(90)
t.left(180)
t.back(200)

Now that you know some basic direction commands, we can start drawing shapes!(Remember to enter a number in paranthases to define the radius of the circle).

t.circle(90) ## For circle

For a star:

t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)

Now that we know all that, it's time to learn how to fill in your shape with a color of your choice!(In the middle of the stant and end commands, you can put code for the shape you would like to draw)

t.fillcolor("red")
t.begin_fill() ## Starts the fill commands
t.circle(90) ## Just and example, you can make code for whatever shape you like
t.end_fill() ## Ends the fill command

##Great! Now it's time for the first task! Your fist task is the draw a square that has a different color on each side.
t.clear()

##Below is the answer to the task(It's ok if you used different colors):
t.fillcolor("green")
t.begin_fill()
t.forward(150)
t.right(90)
t.forward(150)
t.end_fill()
t.fillcolor("magenta")
t.begin_fill()
t.right(90)
t.forward(150)
t.right(90)
t.forward(150)
t.end_fill()

##Kudos to those who have got it correct! but if you didn't, that's still ok! There will be two more tasks that you can do to make up for your mistakes. Ok, lets move on! The next thing is the up and down feature. This lifts the pen up or down. They work like this:

t.up() ## Lifts the pen up which will make it so that if you were to use a direction command, then it would go to that spot but not draw
t.down() ## Puts the pen back down so that you can draw in another place

##Now that you know pretty basic things, it's time to get into things that are a little harder. In turtle graphics, you can make the pen move by allocating keybinds such as the arrow keys. But before you do this, you have to create a variable for the screen.
scr=turtle.Screen()

We can now give the computer the command to LISTEN

scr.listen()

We need to now make some kind of variable so that it knows what to do when we press a button

def f1():
t.forward(175)
t.right(90)
t.forward(175)
t.right(90)
t.forward(175)
t.right(90)
t.forward(175)

Now, we write the code for the button!

scr.onkey(f1, "b")

Using this, you can make any button on the keyboard do what you want (Including the mouse buttons!!) Now, get ready, because it's time for the second task! The second task is to make a program that can make 4 different shapes of your choice when you click a specific button.

Here is the answer for the first task

def square():
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(200)

def star():
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)
t.forward(150)
t.right(144)

def rectangle():
t.forward(200)
t.left(90)
t.forward(40)
t.left(90)
t.forward(200)
t.left(90)
t.forward(40)

def circle():
t.circle(90)

def clear():
t.clear()

scr.onkey(square, "a")
scr.onkey(star, "s")
scr.onkey(rectangle, "d")
scr.onkey(circle, "f")
scr.onkey(clear, "c")

Ok! I hope you learned a lot from the second task because soon, you will be doing the third task! We will now learn about combining normal Python with turtle graphics. When you combine normal python with turtle graphics, then you can make many cool apps! I myself have made a few games and n app using turtle graphics with python. The first this is the print command. If you have coded python before then you would most likely know about the print command. This command displays text in the output box.

print("Hi") ## Remember to always add the quatations but you can also use single quotes instead of double

#Using this, you can make a useable game or app for drawing. You can also make a cool looking fun tutorial!

Comments
hotnewtop
robro12910 (1)

@Aphmeta idk what u mean but if its bad, ik and i did not finish it yet so yea lol

[deleted]

@robro12910 oh I see np then :D

InvisibleOne (3226)

This is good, but you should put three back ticks ` around your code so that you can format it like code. Like this:

formated in a code block
pretty cool

and if you want it to highlight the code, then after the first three back ticks put the name of the file extension for that programming language (python is py) and it will format it like python

print("See, formated like python")
while True:
  print("This is cool!")
[deleted]

ew ew ew ew