Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making A Game With Turtle Graphics!
2IsAPrimeNumber (29)


It's time to move on from those text adventures, now. We're going to make a game using Turtle Graphics with only 15 lines of code! Also, this tutorial recommends that you have basic knowledge of Python, but if you don't, that's perfectly fine. So, without further ado, let's make a Turtle Graphics game!

Turtle Functions!


Turtle Graphics has a lot of different functions to use for making games. Let's start with the basics. turtle.forward() moves the turtle forward the number of pixels given in the parameters. I bet you'll never guess what turtle.backward() does: moves the turtle backward the number of pixels given in the parameters.


Alright, we're going faster than I thought! turtle.right() rotates the turtle right, or clockwise, the number of pixels given in the parameters. Now, I'm going to blow your mind: turtle.left() rotates the turtle left, or counterclockwise, the number of pixels given in the parameters.


Only one turtle function this time, so I'll just start with the sarcastic comment: turtle.showturtle() hides the turtle...just kidding! turtle.showturtle() shows the turtle so we can see the turtle, because we don't like invisible turtles.


With Turtle Graphics, you need to tell the script to listen to keyboard (and mouse) input using this piece of code: turtle.Screen().listen(). Now you've got Turtle Graphics listening to you!


This piece of code is very important. There's 2 parameters: the first parameter is the function that will execute when the second parameter, the key that needs to be pressed, is pressed. For instance, if I were to define a function called "function," and I wanted the function to execute when I press the Space bar, I would use the following piece of code: turtle.Screen().onkey(function, "Space").

Let's Make A Game!

import turtle
def ahead():
def behind():
def clockwise():
def counterclockwise():
turtle.Screen().onkey(ahead, "Up")
turtle.Screen().onkey(behind, "Down")
turtle.Screen().onkey(clockwise, "Right")
turtle.Screen().onkey(counterclockwise, "Left")

Alright, enough learning already! All this learning is making my head hurt. I guess I just tossed all that code at you. Sorry about that. Well, we'll get through it. First, we need to import the turtle module with import turtle. Wait! Hold on! We're moving too fast! I completely forgot to even tell you what game we're making! It's a good thing I caught myself. We'll be making a drawing game where we use the arrow keys on our keyboard to control and draw with a "turtle." Okay, let's skip ahead a little in our code. In our functions we defined, we move or rotate the turtle when those functions are ran. We need those functions for our turtle.Screen().onkey() code where we move our turtles when these buttons are pressed. Let's look at an example: we have the function ahead():
def ahead()
And this function will run when we press the up arrow using the code: turtle.Screen().onkey(ahead, "Up"). Lastly, we have turtle.Screen().listen(), which tells the script to listen to our keyboard inputs. If you've tried running this code already, you'll see that the turtle doesn't show until we add turtle.showturtle() which shows our turtle's beautiful face to the world! Well, actually, it's just an arrow, but it still looks okay. Hopefully, now, you understand the code in the script. If you've ran this code, you've probably discovered that the turtle draws while it moves. Great! Just what we wanted! Now we have uncovered all the basic secrets of making games with Turtle Graphics using a script with just 15 lines of code!

Let's Take It Up A Notch!

So you might be saying that our turtle moves way too slow. Well guess what? It's a turtle! They're supposed to move slowly! But, if you don't care what kind of animal they are and just want them to move, you can change the numbers in the parameters for the turtle.forward(), turtle.backward(), turtle.right(), and turtle.left() statements to make the turtle move more pixels at a time. Also, if you're saying something like "What kind of turtle is this! It looks like some pointy thingy! I want a real turtle!" then you need to know that not all turtles look like what you may expect, and these turtles just so happen to be super rare and endangered species of turtles called "python turtles." But, if you really, really want a "real turtle," use the following code: turtle.shape(). The parameters in this piece of code determine what kind of shape the turtle will be. Here are the different parameters, or shapes, that can be given in the parenthesis: "arrow", "turtle", "circle", "square", "triangle", and "classic". Okay, I've finished now. I'm done. You can go. Why are you still reading this?

2IsAPrimeNumber (29)

@John_WardWard Really?

Thank you!

Did you think it was weird? Do you have any recommendations on making this tutorial better?

John_WardWard (393)

@2IsAPrimeNumber Nah, It was really Good. It's not too long or too short, you have an example, and have used markdown. Maybe use an image with it, but as a user of python with turtle, it's a good tutorial.

jimeastman (18)

how do u make it so that when u hold a key down, it keeps moving.

AbuIbra (5)

It doesent work what did I do wrong

octopyBot (264)

pretty cool tutorial! I didn't know some of this stuff!

ZeeMan (8)

First time with turtle

mmf045 (3)

it isn't working. it can't move

math_genius (7)

I only get this:

What am i doing wrong?

math_genius (7)

Never mind, i realized the problem was that i was using python without turtle

Beads (2)

@math_genius I think it's the part where you want da turtle shape..mayb delete dat?

817576 (0)

lets make a i

AdamBallard2 (1)

does anyone know what im doing wrong to toggle the pen, https://repl.it/@AdamBallard2/GummyYoungToolbox

mmf045 (3)

I just figured out why I'm not moving. It needs turtle.speed(x) x equaling how fast you want the turtle to go.

mmf045 (3)

@AdamBallard2 you added a space were it says turtle.Screen(). you added a space after the .