Tron Classic Arcade Game in Python

In this article we will have more fun and games with the Python Turtle Graphics Module. There are so many things you can do with this module from very basic programs to quite sophisticated applications, making it an ideal learning tool for GCSE Computer Science.

One resource based on Python Turtle Graphics that is not as well known as it should be is the Free Python Games Project, which has some really fun and well-written games that you can play, examine the code for and modify for yourself. In this article we will look a version of the classic arcade game Tron from this collection.

To get the program to run, the author recommends that you install freegames via pip. If you don’t know how to do this, you can find out here. Another way, which I prefer, is to download the utilities file and save it as freegames.py in the same folder as the actual games, and you are good to go.

One thing I like about this project from an educational point of view, is that a lot of the implementation datails are abstracted away into the a utilities module (consisting of a vector class and a few handy functions), so students can focus on the game logic itself. The coding style is very succinct – almost like poetry, some might say. Below is the code for the tron game.

Tron Game with Python Turtle

"""Tron, classic arcade game."""

from turtle import *
from freegames import square, vector

p1xy = vector(-100, 0)
p1aim = vector(4, 0)
p1body = set()

p2xy = vector(100, 0)
p2aim = vector(-4, 0)
p2body = set()

def inside(head):
    "Return True if head inside screen."
    return -200 < head.x < 200 and -200 < head.y < 200

def draw():
    "Advance players and draw game."
    p1xy.move(p1aim)
    p1head = p1xy.copy()

    p2xy.move(p2aim)
    p2head = p2xy.copy()

    if not inside(p1head) or p1head in p2body:
        print('Player blue wins!')
        return

    if not inside(p2head) or p2head in p1body:
        print('Player red wins!')
        return

    p1body.add(p1head)
    p2body.add(p2head)

    square(p1xy.x, p1xy.y, 3, 'red')
    square(p2xy.x, p2xy.y, 3, 'blue')
    update()
    ontimer(draw, 50)

setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: p1aim.rotate(90), 'a')
onkey(lambda: p1aim.rotate(-90), 'd')
onkey(lambda: p2aim.rotate(90), 'j')
onkey(lambda: p2aim.rotate(-90), 'l')
draw()
done()

Several exercises are given to modify the program:

<br />Exercises

1. Make the tron players faster/slower.
2. Stop a tron player from running into itself.
3. Allow the tron player to go around the edge of the screen.
4. How would you create a computer player?

Exercise one is quite simple – can you find the value which controls the speed?

Another simple change is to change the background colour and add a title to the turtle window:

setup(420, 420, 370, 0)
title("Tron")
bgcolor("black")

Exercise 4 is quite interesting, and takes us into the realm of AI or Artificial intelligence. This could get quite complex quite quickly, but one very simple idea is to get one of the players (the computer) to change direction randomly with a certain probability.

Have a go to see if you can implement that functionality for yourself.

And that’s it for today’s look into the Free Python Games Project. I hope you found it fun and interesting. Any questions or comments, please let me know below.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *