PYGAME
Build an overhead Python racing game
Calvin Robinson creates a racing video game in Python with PyGame – there’s nothing like a mad dash to the finish line to get the heart pounding!
OUR EXPERT
Calvin Robinson is a former assistant principal and computer science teacher with a degree in computer games design and programming.
Over the past few issues we’ve taken a look at programming classic video games in Python. This issue we’re going to be developing a racing video game.
Racing games are a sub-genre of video games difficult to categories. Depending on the style of game, they could sit anywhere between arcade games with unrealistic physics, sports titles with realistic graphics, or simulations with hyper-realistic mechanics.
We’ll be using Python to develop our racing video game, because it’s incredibly accessible, versatile and free (in both senses of the word). Make sure you’ve got Python3 installed and ready to go. If you’re using a Debian-based distribution run apt-get update and aptget install python3 to be sure. Once confirmed, create a new Python file in a text editor and start coding:
We import the PyGame module to begin with, because this contains a load of handy tools that make games programming in Python a whole lot easier. There’s no point re-inventing the wheel, after all. If there’s a module that contains shortcuts to what it is you’re attempt to do, use it!
Having a start menu takes the game to the next level.
Retro resolution
After importing PyGame we initiated it, set the screen resolution and window title. 800 by 600 is incredibly low resolution, but since we’re designing a retro game it’ll be ideal for our purposes. We also defined black and white as RGB values in a couple of variables for later use.
We’ve also included a game clock to monitor our frames per second (FPS). There’s a common myth that the human eye can only see 30fps, which is untrue. It’s quite easy to tell the difference between 30fps and 120fps – the ideal framerate for a first-person shooter. The latest mobile phones are being released with 90Hz and 120Hz screens, which allow for scrolling and gameplay of 90fps and 120fps, respectively. The average monitor will only support 60fps, unless you’re using a gaming or design monitor. The important thing to note is that if your game falls below 30fps it’ll be noticeable to the player, while anything under 24fps and they’ll start to see stuttering. This isn’t great for a racing game – or any game for that matter.