MAKE GAMES WITH RASPBERRY PI CODE PYTHON GAMES WITH PYGAME MAKE GREAT GAMES WITH APPGAMEKIT MAKE ADVENTURE GAMES WITH REN’PY

Did you know that Raspberry Pi is a game-creation machine? There are many ways to write games and Mark Vanstone will show you a few to get you started

The UK computer games industry has grown and grown since its origin in the eighties; grown so much that it’s on a par with the UK film industry. This trend is true in many other areas of the world. If you are learning how to write games, then Raspberry Pi is a great way to get your teeth into the subject. There are many ways to write games and get started quickly. If you are just starting out and not ready for text- based coding, don’t worry. There are block-based systems like Scratch where you can lay out your game graphics on the screen. You can code games using blocks that you drag and drop into place to create your program. Once you have mastered that, you may want to move on to text-based coding like Python and Pygame. If you aim to start a career in the games industry, you will find that these days game designers use both methods: visual block editing and text-based programming. One of the most popular game engines, Unreal Engine uses a block editor called Blueprint that is underpinned by libraries of C++ code. Game programming is a great way to learn a wide range of techniques that are useful for other areas of programming. You don’t need to start with advanced scripting but can easily get quick results with the tools in this article. Let’s make some games!

MAKE GAMES WITH SCRATCH

Scratch is an ideal place to start making games, with a great online community of creators who share their games so that you can see how they were made

There are several versions of Scratch that are compatible with all versions of Raspberry Pi although the latest version, Scratch 3 is recommended for Raspberry Pi 3 and Raspberry Pi 4.

Get Raspberry Pi ready

It’s always a good idea to keep your system files up-to-date. You can either download a fresh install of Raspberry Pi OS to your system card using the instructions at **magpi.cc/imager** or from your Terminal window use the commands `sudo apt update` and then `sudo apt upgrade`. It is wise to always go through this procedure before installing anything new on your Raspberry Pi to make sure you have the latest version of all the system files. Of course, for any installs or updates, you will need a connection to the internet.

Install Scratch

There are three versions of Scratch and an online editor. You can install Scratch 3 by clicking on Menu > Preferences > Recommended Software. In the Programming section, you will see Scratch 3. Place a tick in the Install checkbox to the right and click Apply.

If you have not used Scratch before, you probably want to jump straight in and make something happen. With Scratch, you can do just that. You’ll find Scratch in Menu > Programming > Scratch 3. You will see a cartoon cat on the right-hand side and a set of blue boxes on the left. Drag the **turn 15 degrees** block into the Script area in the middle (this is where you assemble your program). Click the **turn 15 degrees** block and the cat will rotate.

Our rotation block is good, but what if you want something more? We can build a program of blocks by joining them together. Click Control in the sidebar and then drag and drop the **repeat 10** block into the Script area. Then move your **turn 15 degrees** block so that it’s inside the **repeat** block. Then click Events in the Blocks palette and drag the **when (green flag) clicked** block to sit on top of the **repeat** block. Now if you click the green flag at the top of the window, the program will run.

In Scratch, a sprite is a graphic on the screen that you are controlling. You can change what it looks like by loading in your graphics or editing the image. Select the Costumes tab at the top of the screen. From there you can use the painting tools to make a new image or alter the one that is already there. Try drawing some lines or filling in some shapes to see how it works. You can also write text. If you select items with the arrow tool, you can also change their colour.

You will probably want to find out lots more about how to use Scratch, and there are lots of tutorials if you select the Tutorials section in the top menu bar of the desktop or online version. You can also get lots more help from the main Scratch web page at **scratch.mit.edu/ideas**. If you look at the Explore section on the website, you will be able to find lots of projects that other people have made and if you find one you like, you can see how they did it by selecting the ‘See inside’ button at the top-right corner.

PICO-8 is a fantasy console for making, sharing, and playing tiny games and other computer programs. It feels like a regular console and runs on a variety of platforms. It has a suite of cartridge creation tools and an online cartridge browser called SPLORE. The programs are distributed in the form of a PNG file and each program has a memory limit of 32kB, so it is like programming a retro-style, 8-bit computer. The PICO-8 development system costs £11 / $15 and can be downloaded from **magpi.cc/pico8**.

Loading and saving

You can load/save Scratch projects to your Raspberry Pi. The online version can save projects to the Scratch server if you log in.

Go to the projects section of Raspberry Pi’s website, **magpi.cc/projects**. In the ‘Find a Project’ section, select Games from the Topic drop-down, and Scratch from the Software drop-down. You will be shown a selection of game projects for Scratch. Each of these projects is laid out as a step-by-step tutorial to help you build the game. There are lots of different game projects available, so you shouldn’t run out!

Raspberry Pi can be used to make some super games and Pygame gives you a great head start

One of the best ways to get started with text-based programming on your Raspberry Pi is to jump straight into Pygame or Pygame Zero. These are both available with the Python programming language and all three are already installed by default with Raspberry Pi OS. If you are not familiar with Python, you can get it running from the Programming menu by selecting the Thonny Python IDE. This will open up an editor to use Python 3. Python is easy -to learn and read, and this article will show you how to use it with Pygame and Pygame Zero.

Mix and Match

Even if you start your program with Pygame Zero, if you need a function from Pygame you can include parts of the Pygame module too!

Pygame Zero was designed to require as little code as possible to get a game running. If you launch the Thonny Editor (IDE) and type **import pgzrun** to load the Pygame Zero module and then after that, write **pgzrun.go()** to start the game, you can then save the file and run it (with the green play button). If you have typed the code correctly, you will see a black window appear titled ‘Pygame Zero Game’. You have written your first Pygame Zero game! It’s not a great game yet but that’s all you need to get the game engine running.

Let’s get a graphic moving on the screen. You will need to find a suitable image to use, perhaps a spaceship or little green man. Have a look at the ‘Graphics Resources’ section near the end of this feature about where to find graphics. A PNG is best; you can find our rocket at **magpi.cc/rocketart**. Now make a subdirectory in the same place as you saved your Python file and call it **images** and put your graphic file inside that directory. Now load that graphic into an Actor object in your code. Name your graphic file **rocket.png** (you must keep to lower-case letters) and load it by typing **myrocket = Actor('rocket', center=(400, 500))**.

Now to get our rocket to display on the screen, we need to add some code to draw it. We do this with a **draw()** function, so type **def draw():** and press RETURN, then type **myrocket.draw()**. Then, to make the rocket move up the screen, we need to add an **update()** function by typing **def update():** and underneath type **myrocket.y -=1**. If we save and run this program, we should see the rocket moving up the screen. If you don’t, check the **rocket.py** code to see what you have done differently. It may have some drawing left behind, so add **screen.clear()** at the beginning of the **draw()** function. If all is well, you have the start of your Pygame Zero game.

Pygame Zero makes it very quick and easy to get games working on your Raspberry Pi, but if you want more flexibility you may find that Pygame is what you require. You will need to write a bit more code, but you will be able to access some functions like using game controllers. To start a Pygame program, you will need to import the pygame module using **import pygame** and then after that, make a call to **pygame.init()**. This starts the game engine off, but we won’t see anything happen if we run it.

When you save your code, don’t call it ‘pygame’ or Python will think that you are referring to the pygame module.

Did you know that The MagPi’s sister magazine, Wireframe, features a section called Source Code every month with Pygame Zero game examples? **wfmag.cc**

With AppGameKit you can develop professional-looking games, not just for Raspberry Pi but also for desktop and even mobile devices

AppGameKit provides a cross-platform development system that was originally for PC desktops, but recently it has become available to download free for Raspberry Pi. You can use the same system on other platforms too, and develop on one system to run on a different one. You can even publish your games and earn money without paying any royalties. The engine has many tools to help you build your game, like 2D sprites, 3D, physics, sound, and even virtual reality. This guide will get you started with AppGameKit so that you can explore all the features.

GPIO pins

If you are feeling adventurous, you could try out the AGK features that allow you to read and write to and from the GPIO pins.

Until recently, AppGameKit was compatible with all Raspberry Pi computers, but at the time of writing, it is difficult to get running on Raspberry Pi 4. Some system updates are needed for other Raspberry Pi computers, even with the latest version of Raspberry Pi OS. Make sure you have backed up any data from your Raspberry Pi microSD card before you start.

First, we need to get the AppGameKit files. You’ll need to go to the website **appgamekit.com** and sign up for an account. When that’s done, go to the ‘AppGameKit For Raspberry Pi’ section in the ‘Classic’ menu item and download the editor files (they are free). Double-click the gzip file to open it and extract the files to somewhere suitable like your home directory. When it’s unpacked, you will see a directory called **AGKPi**. Inside that, you will find the AGK launcher. Double-click to open the editor. If you want to see an error log when it’s running, select ‘Execute in Terminal’ when prompted.

If you try to run any of the samples provided with AGK, you may find that you get some errors. This may only be an issue at the time of writing as there are regular updates available.

From a Terminal window, enter **sudo apt update**, followed by **sudo apt upgrade**, just to make sure we have everything up to date. Then, if you are getting errors about libgles2 (graphics library), type **sudo apt install libgles2-mesa libsdl2-dev**, which will install the necessary libraries. Then enter **sudo rpi-update** – this is a firmware update, so a bit more extreme than the usual updates, and the reason why you should make a backup of your memory card before issuing this command. Now reboot your Raspberry Pi.

A good sample to start with, to make sure everything is working, is the Space Shooter game. Select the Open icon on the toolbar of the editor and then navigate to the **SpaceShooter** directory, which is found in the **Games** folder inside **Projects**. Open the **.agk** file and you will see several files open in the editor. AGK uses a language very much like BASIC, so if you have used BASIC before you should be right at home. If you haven’t learnt BASIC, then it’s quite easy as it was designed for beginners.

If everything has gone well with the install and updates, when you press the green Run arrow you should see a window open up titled AGK and a Start Game screen with spaceships floating about. If you don’t see that, then check the Terminal window that launched the editor to see if there are any errors. You may see some warnings there anyway as some of the shader modes are not supported on Raspberry Pi, but the game should work fine. Start the game by clicking the screen and move the player ship up and down with cursor keys.

Now you have the editor building a game, why not start your own? Start a new project by clicking the New icon on the toolbar. You will be asked for a name for the new project and a base path. Select the folder icon to the right of the base path input box, and navigate to somewhere suitable inside your home directory. Select Create and you will see a new file called **main.agc** open in the editor. In that file, there will already be a listing of the base code you need to start your game. Run it and you will see a black window open with the title ‘test’.

Have a look through the samples to see the range of what AGK can do. You will find a huge range of tutorials at **magpi.cc/agkyt** and there is a full user guide at **magpi.cc/agkguide**. There is also an active and helpful community forum at **magpi.cc/agkforum** where you will find more hints and tips to help you on your way. If you are having a problem with something, you’ll find someone who has solved it and will tell you how. Don’t be afraid to get stuck in and just start coding: the compiler will give you feedback on anything you get wrong.

Raspberry Pi 4

Feeling brave and want to run AGK on a Raspberry Pi 4? Check out the forum post at: **magpi.cc/agkpi4** for instructions.

■ AppGameKit: **appgamekit.com** ■ Raspberry Pi SDK: **appgamekit.com/agk-pi**

There are many graphics resources that are free to download. You can get images, animations and programs. Here are a few places to visit: * **opengameart.org** has a wide range of artwork for backgrounds and character images to include in your games free of charge. * **spriters-resource.com** specialises in sprites, which are the characters to include in games. They are often available in sprite sheets which have all the frames in one image file. * **free3d.com** has many free (and paid-for) 3D models for you to download and use. There are models for just about any situation, some of them specifically designed for games. * **gimp.org** is a great image manipulation program and should be all you need for creating 2D graphics for your games. It can be installed using **sudo apt install gimp** in a Terminal window. * **blender.org** is best for creating a game with 3D graphics. Install Blender free from your Terminal with **sudo apt install blender**. Discover a range of Blender projects on Raspberry Pi’s website (**magpi.cc/blenderprojects**).

This game engine is for storytelling. Use Raspberry Pi to combine words, images, and sounds to create interactive visual novels and life simulation games

Ren’Py is open-source and free to download and use. You can even share your creations without paying a penny in royalties or licences. Ren’Py includes a simple scripting language to control the flow of your story and add interactivity to the pages. The engine also includes a wide selection of animation and transition effects to bring your games and graphic novels to life without needing to learn complicated animation software and supports the most common graphics and sound formats like JPG, PNG, MP3, and a whole lot more.

Embed Python

Ren’Py scripting is quite similar to Python, but if you need to embed a Python program inside your Ren’Py game, you can do that too.

First, download the install files from the Ren’Py website at **renpy.org/latest.html**. You will need the .bz2 version for Raspberry Pi. When it has downloaded, double-click to open the archive and extract it to a suitable place such as your home directory. You will also need to download and extract Raspberry Pi support files from the Additional Downloads section. Once this is all in place, you will find a file in the directory you have extracted called **renpy.sh**. Double-click this file and select ‘Execute’. After a few seconds, you will see the Ren’Py Launcher open.

Ren’Py includes a getting started tutorial, which is probably the best place to begin. By selecting the Tutorial project from the launcher, you will be introduced to Ren’Py’s features by Eileen. She will show you how to start a new project and the ways to set colours and screen sizes. There are also sections in the tutorial to

cover adding your images, text, and sound to your pages. It then goes on to creating interactions and transitions to make your game engaging for your audience. Have a look at the Choices and Python section to see how scripting is used to ask questions and branch to different options.

If you need further help, select the Documentation link at the bottom left of the Launcher window, or check out the forums at magpi.cc/renpyforum

Let’s make a game

Going back to the Ren’Py Launcher, start a new project with the ‘Create New Project’ link on the left-hand side. You will then be asked where you want to save your project and what it should be named. Next, choose what screen resolution you want your production to use and the colour scheme that you would like. After a short pause for processing, your project will be created and listed with the tutorial in the Projects section in the Launcher.

Let’s get scripting

Start scripting the game by selecting the ‘script.rpy’ option under Edit File in the Launcher. It will ask you to select the editor you want to use and then open the script. From there you can make changes to the default script. When you want to test your changes, select your project and Launch Project, then select ‘Start’ from the list on the left of the window that opens. If you make changes to your script, you can then press SHIFT+R to reload your script and start the game again.