GB
  
You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
14 MIN READ TIME
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

>> OBJECT ORIENTATED PROGRAMMING

QUICK TIP

>> PYTHON MATHEMATICS LIBRARY

QUICK TIP

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.

Object orientated programming techniques have been used in this project. The worldmanager.pyscript file contains a collection of classes used to create and construct the view of the 3D world. These classes include the WorldManager and Camera class. The WorldManager class consists of a class constructor and a method called draw() which is used to render the 3D world.

Instead of scrolling down lines of text looking for a variable or function name, use the IDE’s search facility to go directly to what you’re looking for.

Python has a powerful mathematics library that has a range of uses. In regards to the project featured in this tutorial, the trigonometric mathematical functions have been used such math.cos and math.sin and also math.abs. The Python math library can be included by writing this code in a Python script: import math

What’s a radian then? One radian is the angle create when the radius distance is wrapped to a circle. It’s roughly 57 degrees aka 180 / Pi.To go from radians to degrees: multiply by 180, divide by Pi. To go from degrees to radians: multiply by Pi, divide by 180.

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.

Each class in Object Orientated Programming employs a constructor that’s used to initialise values and perform initial operations when and instance of that class is created or called. For example, the constructor in the WorldManager class is used to load images both for the objects that can be shown when the program is running and also the texture images for the wall. The constructor of the Camera class is used to just initialise values for later use in the program. Both of the constructors used in the script file take in arguments to the constructors, so that values can be passed in from the main.pyscript file.

The functions from the maths library that have been used in this project are: math.cos(x) : the cosine of x radians math.sin(x) : the sine of x radians math.abs(n) : the absolute value of the specified number

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.

Look at the source code from main.py: wm = worldManager.

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
March 2022
VIEW IN STORE

Other Articles in this Issue


WELCOME
Not so private networks
I have no idea what you get up
MEET THE TEAM
This issue we’re exploring VPNs. We asked our experts if they use a specific one, how they hide their online tracks (if at all) and their paranoid online fears!
REGULARS AT A GLANCE
2022: the year of Linux gaming?
Hold on to your gamepads – there’s some great stuff coming for Linux video gaming
TikTok accused of violating GPL licence
TikTok’s new Live Studio desktop streaming application has been accused of using code from OBS Studio
Intel Alder Lake gets Linux boost
Update to Linux drivers will give Intel’s latest processors a hike in performance
MY HAPPY PLACE
Matt Yonkovit Head of open source strategy and
PROBLEMS PROBLEMS
Keith Edmunds is MD of Tiger Computing Ltd,
RIP a.out
Archaic file format looks set to disappear from Linux
New ransomware discovered
SFile ransomware has been ported to Linux
Mozilla and Mint join forces
Linux Mint announces that it’s teaming up with the “champion of Open Source”
Distro watch
What’s down the side of the free software sofa?
WINE ON WAYLAND
Alexandros Frantzis is a senior software engineer at
FOCUS ON FIRMWARE
Jon Masters has been involved with Linux for
KERNEL WATCH
Jon Masters summarises the latest happenings in the Linux kernel, because someone has to
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to lxf.answers@futurenet.com
Mailserver
WRITE TO US Do you have a burning Linux-related
System Monitor Center
Version: 0.3 Web: https://github.com/ hakandundar34coding/system-monitoring-center
Tesseract
Version: 5.0 Web: https://github.com/ tesseract-ocr/tesseract
Libtree
Version: 3.0.1 Web: https://github.com/ haampie/libtree
Video trimmer
Version: 0.7 Web: https://gitlab.gnome.org/ YaLTeR/video-trimmer
Ntfy
Version: 1.6.1 Web: https://github.com/ binwiederhier/ntfy
Rusty Aquarium
Version: 0.13 Web: https://github.com/ ollej/rusty-aquarium
Caravaggio
Version: 3.6.2 Web: https://github . com/ramiel/caravaggio
FishFight
Version: 0.3 Web: https://fishfight.org
AssaultCube
Version: 1.3 Web: https://assault.cubers.net
Kage Studio
Version: 0.4.2 Web: https://kage.sourceforge.io
System G
Version: 3.0.1 Web: https://github.com/jafl/system-g
REVIEWS
Intel NUC 11 Extreme PC
Kizito Katawonga wonders what happened to the NUC of the past?
Kingston XS2000 External SSD
Next-gen external USB 3.2 2x2 storage goes mainstream with Ganesh T S at the testing station
Neptune 7.0
Debian-based KDE distros are a dime a dozen, so Mayank Sharma was keen to see how it stacks up against the competition
Slackel 7.5
Mayank Sharma finds this distribution combines Slackware and USB distros with two use-cases: older computers and portable installations
Siduction 2021.3.0
You probably think Mayank Sharma has a thing for Debian-based KDE distros, but he’ll want you to believe that it’s just coincidence
Pop!_OS 21.10
Keyboard-driven Jonni Bidwell is excited by the prospect of new shortcut keys and swish application launchers. Will his enthusiasm be curbed?
Oxygen Not Included
Management are thrilled with Christopher Livingston’s latest cost-saving suggestion: workers don’t really need to breathe, do they?
ROUNDUP
GUI text editors
Within the world of Linux, so often we need to invoke a text editor to get the heart of the matter. Michael Reed investigates the best of the current crop
General-purpose text editor
How does it perform when working with configuration and log files?
Licence and installation
What does it take to get hold of this software and install it?
Configurability
To what extent can you make your text editor feel like home?
User interface
We want something efficient and pleasant
Documentation and community
Extensive features aren’t much good if you can’t figure out how to use them
Additional plugins
Improve your enjoyment of using an editor and increase productivity too
Suitable for programmers?
Editing and creating source code is a common task for a text editor
The Verdict
GUI text editors
SECURE YOUR VPN
The ever-sceptical Jonni Bidwell is here to cast doubt on the highly profitable VPN business, and show you how you can do better…
The why and wherefore of VPNs
It’s not VPNs that are bad, rather people’s unrealistic expectations of privacy. And, in a pay-per-click age, honesty
Run a WireGuard VPN
WireGuard is the newest, leanest, meanest VPN protocol on the scene. Let’s get those wires guarded!
Tor and other VPN alternatives
Tor has long been regarded as the gold standard for privacy and anonymity. See how easy it is to go dark
Extra measures
See what VPNs don’t and can’t protect against, and bolster your privacy with a layered approach
Pi USER
Celebrating the Raspberry Pi at 10
The Raspberry Pi has achieved so much more than just material success, as Neil Mohr reveals…
Raspberry Pi OS
Les Pounder takes the latest Raspberry Pi OS, otherwise known as Bullseye, for a ride… but does it miss the target?
How to easily create multi-HAT Pi projects
PHAT STACK
LINUX FROM SCRATCH
Aaron Peters examines building a distro from the ground up with Linux From Scratch, because why take the easy way?
TUTORIALS
Managing passwords
Credit: https://github.com/biox/pa
Build your own dynamic timelines
Credit: https://thetimelineproj.sourceforge.net
Emulate the classic MSX system
Credit: https://openmsx.org
Multi-boot USB drives for easy distro hopping
Use the powerful Ventoy to repair operating systems and make distrohopping so much simpler, with a bit of help from Matthew Holder
Understand and deploy security keys
Stuart Burns reveals how you can quickly boost your system’s protection using security keys and Multi Factor Authentication (MFA)
Recipe management done by the book
Credit: https://hay-kot.github
Publishing a hit book using Linux
Have you ever wanted to publish your own book? Michael Reed demonstrates that all of the tools you need exist on Linux
TOP OF THE FOSS
Perl at 34
Jonni Bidwell tries not to cut himself on the “Swiss army chainsaw” of programming languages, Perl
CODING ACADEMY
Use Python to get more from Dropbox
Mihalis Tsoukalos explains how to use the Dropbox REST API with Python to create your own custom tools
Chat
X
Pocketmags Support