Python Turtle Graphics Game - Arachnophobia

Here’s a fun game written with Python Turtle Graphics. It’s called Arachnophobia, and is basically a spider version of Whack-a-Mole.

Please note spiders are mostly innocent and do not deserve to die just because you may be afraid of them. No spiders were harmed in the production of this game.

You can play the game online at this repl.it. The goal is to use the mouse to click on spiders as they appear. However, this is where we run into some of the limitations of browser-based Python programming, as the fastest time the online version can handle is 1000 ms between “frames” (You’ll see what I mean if you play it…)

Broweser Based Version of Arachnophobia Python Turtle Graphics Game

Arachnophobia Python Turtle Graphics Game

You will find that running the game on a desktop or laptop will give you a much better experience. For one, you can change the speed by adjusting the SPEED constant (try 500, as in 500 ms between frames). Also, if you install playsound via pip install playsound, there is a fun sound effect every time you click on a spider.

Below is the code listing for the game. As usual, I strongly encourage you to type the code in for yourself, even if there are parts you don’t understand. But if that seems like a drag, just paste it into a new file for now and save and run.

I will explain some of the details after the listing, but please note that even though we are using a module which some people consider to be for beginners at programming, there are some fairly advanced techniques used, and many of the choices I have made are the result of a considerable amount of experience writing these kinds of games.

If you want to learn more about how to write games using Python, why not get in touch using the details on our contact page for further information or to book a free 1/2 hour no-obligation lesson? 

Here’s is the listing:

You will need to download the spider image and save it the same folder as the program, naming it spider.gif (right-click, save image as).

Some observations about the above code:

  • Constants are used to avoid “magic numbers” scattered throughout the program
  • The board is based on the concept of a 2d grid of individual turtle objects.
  • The turtle objects have a click-handler attached, but all click events are handled by one function due to the use of the lambda expression (this is a fairly advanced technique).
  • The board is created using a nested FOR loop. See link for more info.
  • If you are concerned about the use of global variables, please read this article
  • It is a good idea to clear the screen on reset, otherwise there can be an unseen accumulation of stray turtle objects existing in memory which can cause the program to slow down as you play multiple rounds.

That’s it for now. As I said above, writing this kind of game is non-trivial, especially if you want to be able to do it entirely independently eventually. If you have any questions, go ahead and ask either in a comment or by email and I’ll get back to you with an answer.

Happy computing!

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python