Basic Animation with Python

In this article we are going to learn how to do basic animation and collision detection with Python Turtle Graphics. The best thing to do here is to copy the code provided below and run it. Then read through the code to get an initial feel for what is happening, and then come back and read more. The comments in the code should give you a good idea what each section does.

The Game Loop

OK so it’s not really a game yet, but we have some of the main ingredients, and we have a main loop, which is an essential part of most games. Here our main loop is called move() since it controls the movement of the ball, but since it also performs collision detection, it could reasonably be called game_loop() or something similar. In a more complex game, many things would be controlled within the main game loop, including updating positions of game objects, updating scores, rendering images etc.

In this simplified example, the key is the ontimer() method. This calls a function after a given interval. Notice that the function it calls is the one currently being executed, so we get ourselves a game loop! The arguments to turtle.ontimer() are the name of the function to call, and the delay in milliseconds.

NB You will find some examples of Python code online which use while True for animation in Turtle Graphics. This is a bad idea, as it is conceptually flawed – Turtle Graphics itself is using a loop all the time to control the display. Our programs should tie into this loop in various ways, such as using the ontimer() method.

Global Variables

You may notice that I have used global variables in this program. This means that x, y, xdir, ydir are accessible to the whole program, outside of the move() function. Doing this makes things far simpler than attempting to avoid global variables due to a misconception that they are always evil. If you are concerned about this, I recommend this article for more information: Global Variables will Break the Internet.

Collision detection

Collision detection is a big deal in game development. Here we a simply interested in whether the ball has collided with the edge of the window. If it has, we change its direction.

There are many ways to check for the borders. I’ve used an inefficient version for clarity, which individually checks for right, left, top and bottom collisions by comparing the x and y coordinates of the turtle with the edge of the screen using turtle.window_height() and turtle.window_height(). You could certainly combine the x checks together and the y checks together using or, or use some other logic to achieve the same result.

Computer graphics provide a rich interplay between mathematics and programming. For example, knowledge of vectors is very helpful in developing computer games.

import turtle

SPEED = 5  # ms between frames
STEP = 5  # Pixels per "step"

def move():
    global x, y, xdir, ydir

    x = x + xdir
    y = y + ydir

    # Collision detection
    if x >= right_edge:
        xdir = - xdir
    if x <= left_edge:
        xdir = - xdir
    if y >= top_edge:
        ydir = - ydir
    if y <= bottom_edge:
        ydir = -ydir

    turtle.goto(x, y)
    # How the magic happens
    turtle.ontimer(move, SPEED)

# Create a Turtle Graphics Screen object, so we can control the window.
screen = turtle.Screen()
screen.setup(450, 450)

# We will use the default turtle and modify its properties.
# We could have created our own named turtles with `my_turtle = turtle.Turtle()`.
# turtle.shape("triangle")  # There are other options too. See the docs.

x, y = 65, 0  # More interesting with an initial offset...
xdir, ydir = STEP, STEP  # Vectors anyone?
right_edge = turtle.window_width() // 2
left_edge = -turtle.window_width() // 2
top_edge = turtle.window_height() // 2
bottom_edge = -turtle.window_height() // 2

# Set the thing in motion by making the first call to move().

# Exit cleanly

So that’s how you can do basic animation with Python and Turtle Graphics. There is a lot more to learn if you want to make full games with Turtle Graphics, but many of the basic principles have been covered here. I encourage you to put them into practice in your own programs and see what you can create.

Happy computing.

Sharing is caring!

Leave a Reply

Your email address will not be published.