Code your own pinball game | Wireframe #53
Get flappers flapping and balls bouncing off bumpers. Mark Vanstone has the code in the new issue of Wireframe magazine, available now.
There are so many pinball video games that it’s become a genre in its own right. For the few of you who haven’t encountered pinball for some reason, it originated as an analogue arcade machine where a metal ball would be fired onto a sloping play area and bounce between obstacles. The player operates a pair of flippers by pressing buttons on each side of the machine, which will in turn ping the ball back up the play area to hit obstacles and earn points. The game ends when the ball falls through the exit at the bottom of the play area.
Recreating pinball machines for video games
Video game developers soon started trying to recreate pinball, first with fairly rudimentary graphics and physics, but with increasingly greater realism over time – if you look at Nintendo’s Pinball from 1984, then, say, Devil’s Crush on the Sega Mega Drive in 1990, and then 1992’s Pinball Dreams on PC, you can see how radically the genre evolved in just a few years. In this month’s Source Code, we’re going to put together a very simple rendition of pinball in Pygame Zero. We’re not going to use any complicated maths or physics systems, just a little algebra and trigonometry.
Let’s start with our background. We need an image which has barriers around the outside for the ball to bounce off, and a gap at the bottom for the ball to fall through. We also want some obstacles in the play area and an entrance at the side for the ball to enter when it’s first fired. In this case, we’re going to use our background as a collision map, too, so we need to design it so that all the areas that the ball can move in are black.
Next, we need some flippers. These are defined as Actors with a pivot anchor position set near the larger end, and are positioned near the bottom of the play area. We detect left and right key presses and rotate the angle of the flippers by 20 degrees within a range of -30 to +30 degrees. If no key is pressed, then the flipper drops back down. With these elements in place, we have our play area and an ability for the player to defend the exit.
All we need now is a ball to go bouncing around the obstacles we’ve made. Defining the ball as an Actor, we can add a direction and a speed parameter to it. With these values set, the ball can be moved using a bit of trigonometry. Our new x-coordinate will move by the sin of the ball direction multiplied by the speed, and the new y-coordinate will move by the cos of the ball direction multiplied by speed. We need to detect collisions with objects and obstacles, so we sample four pixels around the ball to see if it’s hit anything solid. If it has, we need to make the ball bounce.
Get the code
If you wanted more realistic physics, you’d calculate the reflection angle from the surface which has been hit, but in this case, we’re going to use a shortcut which will produce a rough approximation. We work out what direction the ball is travelling in and then rotate either left or right by a quarter of a turn until the ball no longer collides with a wall. We could finesse this calculation further to create a more accurate effect, but we’ll keep it simple for this sample. Finally, we need to add some gravity. As the play area is tilted downwards, we need to increase the ball speed as it travels down and decrease it as it travels up.
All of this should give you the bare bones of a pinball game. There’s lots more you could add to increase the realism, but we’ll leave you to discover the joys of normal vectors and dot products…
Get your copy of Wireframe issue 53
You can read more features like this one in Wireframe issue 53, available directly from Raspberry Pi Press — we deliver worldwide.
And if you’d like a handy digital version of the magazine, you can also download issue 53 for free in PDF format.
Or join this opensource pincab project !
Check videos emilia pinball running on Pi4:
I am not sure, whether I will compile this, but this reminds me of typing VB code from computer magazines in the late 80s until early 90s. If the typing didn’t test your perseverance, finding the error surely did. I was never able to copy the texts without making some errors, like missing an apostrophe or mixing it with commas and such. After fixing all the bugs, it was such a big achievement to have programmed your personal tool. At least, my parents were very proud of me, even though they thought, that I was some kind of hacker computer genius, while all I did, was copying text, haha.
We discussed type-in listings a LOT here when we started the Raspberry Pi project; a lot of us are 80s kids, and grew up on BASIC listings. (I remember with particular frustration an octopus text adventure game I typed from a library book that would. Not. Work. Never found the error.)
It was, for me at least, a great way to learn. Although you say you were “just copying text”, I defy anybody not to learn SOMETHING from the hours of copying code across. The BBC Micro User Guide was a wonderful thing as well, and Micro User magazine was a fount of knowledge every month too. I was lucky to have these – for me, IT lessons at my all-girls’ school meant typing classes!
Exactly. The detours discovered with “what does this do” is always worth it.
Is that “creating a database adventure game” book, with colored handcuffs? I think that they’re better off showing the database itself, as opposed to just straight code.
Nowadays, we have ScottKit, which uses Ruby. I had to do a Ruby full install to get it running, though. The default Raspberry OS setup is missing so many Ruby libraries.
I don’t think so; I *think* there was an octopus on the cover. Peter Kilworth’s “How to write adventure games for the BBC Micro” was a wonderful thing, too; we still have Eben’s copy on the bookshelf in our study.
Steve rodgers and Marcus Milton are the authors of the only text adventure book (that I know of) with octopus on the cover, which is that database adventure book I mentioned. I read Peter’s book,too. It never did click for me until I see ScottKit, which is why I mention it.
I think that writing adventure games is something beginning coders should take on, but I’m not too happy with existing implementations being too complicated. Hence ScottKit. Unfortunately, it’s not powerful so I’m in the process of expanding it. Is this something of interest to you?
I grew up with personal computers, the first computer I remember had a 3.5″ and 5 1/4″ disk drive with no HDD.
My parents got a code your own games book. I’m not sure I ever actually learned anything useful from copying that down, but it did give me an interest in computers and technology in general.
Now, I work in a Library and mostly copy others’ work when creating records for our books. Maybe it was more relevant than I thought.
I vaguely remember a MAD magazine with 2 pages of code for a Commodore 64? And we lost power as I was finishing it. Ah the memories!
Udya Math Sahoo
Well, Its the TOI Proiffi
Ah, this takes me back to the days when all the print magazines would include programs that you’d have to type in. But nowadays, we have the advantage of copy/paste.
Oooh! Lots of opportunities for customization. Thanks, Mark and Ryan!
Comments are closed