PYTHON
Create 3D gaming worlds with Python
Understanding the basics of rending a 3D world is a great way to understand game code. Lock and load, says Andrew Smith…
OUR EXPERT
Andrew Smith is a software developer for NHS Digital, has a bachelors degree in software engineering and a master’s degree in computer networks.
During the early to mid-90s, some of the most popular video games to play on a PC were video games such as Wolfenstein 3D, Doom, Quake and Unreal Tournament to name a few. In this edition of Linux Format we’re going to look at the construction and mathematics involved in creating a 3D game world, similar to that of Wolfenstein 3D (1992) or Doom (1993). In this first part of the tutorial, we’ll cover how a 3D game world can be created using PyGame and look at some of the mathematics used to navigate around the game world (using functions from the Python mathematics library) as well as some of the collision detection techniques.
This tutorial is based on a project that was created over 10 years ago in 2011 and has recently been adapted to work on the latest Python for the benefit of this tutorial. The original project was written to work on Python 2.7. The original source code and resources that this tutorial is based on can be found at https://github. com/jtmfam/Gh0stenstein.
Python 3.10 has recently been released, so we’ll install and set up Python 3.10 for the benefit of this tutorial. For those that have Python and PyGame already installed, Python 3.8+ should be suitable for use with this tutorial. Type the following to install Python 3.10 and PyGame.
Released back in 1992, Wolfenstein 3D was the first example of a first-person shooter using ray casting.
CREDIT: Wolfenstein 3D 1992, id Software.
Doom, created by id Software, was another example of a first-person shooter that used ray casting and featured 2D sprites.
CREDIT: Doom 1993, id Software.
$ sudo apt-get install python3.10
$ sudo apt-get install python3-pip
$ python3.10 -m pip install pygame
Check both the python and pygame versions. Next, git clone from repository. $ git clone https://github.com/asmith1979/ lxf286_3DWorld/
As an example, the whole project has been put into a folder called PythonProjects, which was created before downloading the project. Alternatively the source code and project can be retrieved from the LXF286 linuxformat.com/archives. This tutorial will focus on the source code located in the folder called Gh0stein\src. If it’s not already in that folder type cd Ghostein to get into the folder and gain access the Python source code.
To edit and view the source code you can either use a default text editor installed on your flavour of Linux or something more specific such as Notepad++, PyCharm or VS Code. In this tutorial, we’ll be using gedit to view and edit the source files. It may be helpful to open up two console windows: one for editing/viewing source files and the other terminal window for executing the PyGame code.