Vous consultez actuellement le France version du site.
Voulez-vous passer à votre site local ?
13 TEMPS DE LECTURE MIN

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.

OUR EXPERT

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

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?

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.

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

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 .

Finally, grab a copy of the Dwarf Game Project from the author’s GitHub repository: 

mkdir lxf278dwarfproj

 cd lxf278dwarfproj

 git clone https://github.com/asmith1979/dwarfgame

The source code and project can also be retrieved from the Linux Format archive. Once cloned,you’ll notice the following folder structure: design, images, sfx and src. The src folder contains the Python source code and is also where the source code is executed. 

cd src

 python3 ./dwarfgame.py

If all goes well, you should obtain the following output of the Dwarf Game Project (see screenshot, above right):

Before continuing any further, feel free to get to know the game by moving the game character around the game world that’s been created. The cursor keys are used for movement and the Spacebar key is used for attack. The Escape key ends the game program. When you have become used to the gaming environment, press the Escape key to exit the game and continue with this tutorial.

PYGAME: THE MAIN LOOP

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.

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

while not programEnd:

for event in pygame.event.get(): 

if event.type == pygame.QUIT: 

programEnd = True

if event.type == pygame.KEYDOWN

and event.key == pygame.K_ESCAPE:

programEnd = True

... As can be seen from the code, the main while loop ends on a boolean condition to identify when the program has finished completely. A variable called programEnd is used to identify this, which initially is set to a False boolean value.

In this particular main while loop, you’ll notice as you look through the code in the while loop that first the input to control the game is dealt with via the keys on the keyboard. The Escape key is used to exit the program, the cursor keys enable directional movement of the player and the Space bar invokes the attack move of the player.

Continuing on from this is the processing that’s carried out for both human and computer players, as well as screen rendering operations to process graphics for the game itself and everything in it.

Finally after doing all this we come to the final part of the loop, as shown below:

clock.tick(100)

 pygame.display.flip()

From the above code we determine our framerate, which is set to 100, and then update the contents of the entire display.

Débloquez cet article et bien plus encore avec
Vous pouvez en profiter :
Découvrez l'intégralité de cette édition
Accès instantané à plus de 600 titres
Des milliers d'anciens numéros
Pas de contrat ni d'engagement
Essayer pour €1.09
S'ABONNER
30 jours d'accès, puis seulement €11,99 / mois. Résiliation à tout moment. Nouveaux abonnés uniquement.


En savoir plus
Pocketmags Plus
Pocketmags Plus

Cet article est tiré de...


View Issues
Linux Format
July 2021
VOIR EN MAGASIN

Autres articles dans ce numéro


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
Support Pocketmags