GB
  
You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
13 MIN READ TIME
DWARF PROJECT

Managing game objects with Python

Tired of being called small and hairy, Andrew Smith grabs his double-headed axe and sets out to create a world full of Dwarfs.

PYGAME: THE MAIN LOOP

QUICK TIP

OBJECT ORIENTATED PROGRAMMING

In every PyGame program there needs to be a main loop that renders the graphics used in the program and that also controls input for the game via keyboard and/or mouse depending on the PyGame application being developed.

If you want to jump to a specific function in the code, instead of scrolling through lines and lines of code, just use the IDE’s search function (if it has one). It’s usually invoked by pressing (Ctrl+F). Type in your search term and then click the Search Next button.

Object orientated programming (OOP) is a paradigm based on the concept of classes, objects and associations that can contain both data and code, which determines how the objects behaves in the software system being created. Before an object is created, a template is first created to then instantiate an object. A template of an object is called a Class which, by using a concept called inheritance, can inherit other data and operations so that they don’t have to be re-written each time.

OUR EXPERT

Let’s take a look at the main loop implemented in this Dwarf Project. 

Object Orientated Programming has been implemented in the Dwarf Game Project to implement the game characters for the game. There is a base class called Player that includes much of the properties and operations which are applicable for both the human player and computer-based players. Notice that in the base class there’s a method called processWalk_Anim() , which is used to process the walk animation of dwarf, whether it be the player’s dwarf or an enemy dwarf. After the base class Player, there are two derived classes called DwarfPlayer and DwarfAIPlayer, which both inherit the functionality and properties of the Player class. Both of the derived classes DwarfPlayer (used for the human player) and DwarfAIPlayer used for the computer player continue to define properties and operations unique to their application in the Dwarf Game Project

Andrew Smith is a software developer at NHS Digital, has a degree in software engineering and an MSc in computer networks (mobile and distributed).

while not programEnd:

T his issue we’re going to add to an already developed game platform, fully created in the Python scripting language using the PyGame library module. The theme of the project was inspired by an isometric video game in the early 1990s called HeroQuest (http://bit.ly/lxf277dwarf). This project first started off as an experiment in video game construction over one weekend when the question was asked: What if the game world moved around the main game character instead of moving the main game character around the game world?

for event in pygame.event.get(): 

The Dwarf Game Project that we’ll be modifying as part of this month’s tutorial consists of a constructed game map made up of interconnected rooms, game objects and enemy game characters (hostile dwarfs). The player of the game will control the main game character (a green dwarf), around the map disposing of all the enemy players to complete the game or until the main game character is killed by any of the enemy dwarf characters.

if event.type == pygame.QUIT: 

To get started, we’ll need a few things: Python, PyGame and the Dwarf Game Project.

programEnd = True

To install Python, open a terminal window (Ctr-Alt-T) and type sudo apt-get python3 followed by sudo aptget install pip3 . Then install the PyGame module by typing pip3 install PyGame .

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
July 2021
VIEW IN STORE

Other Articles in this Issue


WELCOME
LINUX FORMET
MEET THE TEAM
We need upgrades!
Technology never stops advancing – that’s a blessing
REGULARS AT A GLANCE
University banned for ‘hypocrite commits’
LINUX KERNEL
Google’s FLoC gets frosty reception
FLoC is Google’s replacement for cookies, but many companies and individuals have their concerns
Linux kernel hits one million git commits
Major milestone reached ahead of 30th anniversary
FILESYSTEM ERRORS
Gabriel Krisman Bertazi is a senior software engineer
STRONG SUPPORT
Keith Edmunds is MD of Tiger Computing Ltd,
Linux on Apple’s M1 makes headway
Ongoing work on the M1’s GPU brings Linux closer to Apple’s SoC
Spectre is back
Researchers find new line of attack that beats defences
Lucas Chess
One of the strongest ways to play – and learn – chess can now run on Linux
DISTRO WATCH
What’s behind the free software sofa?
PULLING TOGETHER
Matt Yonkovit is Percona’s Head of Open Source
TESTING TIMES
Jon Masters has been involved with Linux for
KERNEL WATCH
Jon Masters summarises the latest happenings in the Linux kernel, so that you don’t have 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
Subscriptions
www.magazinesdirect.com/lin/stm21
Loop Hero
We often find Management going around in circles, so when Evan Lahti introduced this latest “micro-RPG” they were left all in a spin!
Shortwave
Version: 2.0 Web: https://gitlab . gnome.org/World/Shortwave
Cosmic
Version: GIT Web: https://github.com/ pop-os/cosmic O ne
Haruna
Version: 0.6.1 Web: https://github.com/g-fb/haruna H aruna is
Vizex
Version: 1.9.9 Web: https://github .com/bexxmodd/vizex F ancy
eBPFSnitch
Version: 0.3.0 Web: https://github .com/harporoeder/ebpfsnitch B ack
Evilpixie
Version: 0.2.1-GIT Web: http://evilpixie.scumways.com B oth Gimp
Nethogs-Qt
Version: 0.0.8 Web: http://slist.lilotux .net/linux/nethogs-qt/ M any
Kitchen Tales
Version: GIT Web: https://github.com/pigdevstudio/kitchen-tales Here’s another brilliant
zFRAG
Version: GIT Web: https://github.com/ LostTrainDude/zfrag B ack
Optimizt
Version: GIT Web: https://github.com/ funbox/optimizt Optimizt is ideal
SingleFileZ
Version: GIT Web: https://github.com/ gildas-lormeau/SingleFileZ Don’t miss
REVIEWS
Phrozen Sonic Mini 4K
Print with more pixels per inch than an iPhone 12 Pro, says Andrew Sink.
Intel Core i5 11600K
It’s a chip that tickles Dave James’ price/performance sensibilities
Ubuntu 21.04
Not a fan of release names, Mayank Sharma is even more perplexed by the one given to this release, since hippos aren’t known to be hairy
Fedora 34
Red Hat-backed distros have had a falling out with the community off late. Mayank Sharma is hopeful this one will help heal some of those wounds
Escuelas Linux 6.13
Remote learning is the flavour of the season and Mayank Sharma finds a distro that’s just what the, er… headmaster ordered
ROUNDUP
3D modellers
Not everyone has the time to learn a complicated piece of 3D software. Michael Reed has five simple solutions for creating your own 3D assets
Flexibility and potential uses
What can you achieve with it?
Licencing and installation
What hurdles do you have to negotiate to get it up and running?
User interface
A clear interface is vital for graphics work
Documentation and community
Is help and support available when you need it?
Import/export formats
How well does it work and play with other programs and engines?
Rendering
What does the output look like?
Ease of use
How easy is it to learn and use?
The Verdict
3D modellers
GET INTO UBUNTU
The latest version of Ubuntu has landed, and Nick Peers is on hand to show you how to install it and find out what’s new
Install Ubuntu 21.04
Looking to switch to Linux from Windows? Discover how to take Ubuntu for a test drive, then install it alongside your existing operating system
Wayland strikes back
Ubuntu 21.04 attempts to reintroduce Wayland as the default desktop server. Will it succeed second time around?
What’s new in 21.04?
It may not be packed full of eye-catching new features, but Ubuntu 21. 04 features plenty of improvements
IN-DEPTH
EXPLORING PRESTEL
Mike Bedford shines a spotlight on the online data service that predated the World Wide Web by more than a decade…
LXF’s NEW $HOME
Jonni Bidwell has one month to save Linux Format’s web presence. He claims he didn’t sign * up for this, but Fate takes a different view…
Pi USER
NEC’s 4K Pi module exhibits positive signs
The Compute Module 4 is powering a commercial signage solution from NEC at 4K resolutions
Mu code editor
Les Pounder is a life-long learner and always keen to try something new. He goes back to basics and codes from a new coder’s point of view
Blast the evil mutant space bats
Les Pounder has landed on a new planet and the evil mutant space bats are trying to steal his space snacks. Defend those provisions with your life!
Fit a Raspberry Pi 4 inside a custom case
Your dreams of a Raspberry Pi desktop can be fulfilled with the DeskPi Pro case. Christian Cawley’s on hand to provide assembly instructions
TUTORIALS
Vet your USB devices
Security is more than just a strong password. Shashank Sharma is always on the hunt for robust techniques to safeguard his data
Get a next-gen media server up and running
Nick Peers reveals how to install and set up a completely open source – but brilliant – alternative to Plex, the streaming media platform
Exploring Blender’s features and tools
Delving further into Blender, this month, Michael Reed uncovers some impressive-looking features that are surprisingly easy to get started with
Build a fully featured Ubuntu NAS system
Nick Peers takes a generous portion of Ubuntu Server, adds a dash of Cockpit and whips up a tasty network-attached storage setup,
Emulate the classic Dragon 32
Les Pounder dons his suit of armour and gets ready to do battle with the fearsome Dragon!
Make your own Magic Eye images
Discover how Magic Eye images work by learning how to create your own pictures with hidden 3D objects. Mike Bedford’s behind the visual wizardry
CODING ACADEMY
Comparing time series data like a pro
Mihalis Tsoukalos explains how to use the Stumpy library to compare time series, with a little help from Anaconda
On the disc
Discover the highlights from this month’s packed DVD!
BUILD A BETTER LINUX SERVER
CentOS is dead, long live CentOS! We migrate from CentOS to its alternatives. Come with us!
Chat
X
Pocketmags Support