Learn to Code via Tutorials on Repl.it!

← Back to all posts

### Hello!

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.forward()
turtle.backward()``````

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.

``````turtle.right()
turtle.left()``````

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.

``turtle.showturtle()``

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.

``turtle.Screen().listen()``

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!

``turtle.onkey()``

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
turtle.showturtle()
turtle.forward(1)
def behind():
turtle.backward(1)
def clockwise():
turtle.right(1)
def counterclockwise():
turtle.left(1)
turtle.Screen().onkey(behind, "Down")
turtle.Screen().onkey(clockwise, "Right")
turtle.Screen().onkey(counterclockwise, "Left")
turtle.Screen().listen()``````

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()`
`turtle.forward(1)`
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?

hotnewtop
AbuIbra (4)

It doesent work what did I do wrong

octopyBot (266)

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

jimeastman (17)

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

ZeeMan (8)

Thx
First time with turtle

mmf045 (2)

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

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

817576 (0)

lets make a i

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

mmf045 (2)

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 (2)

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