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 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.Īll of this should give you the bare bones of a pinball game. We could finesse this calculation further to create a more accurate effect, but we’ll keep it simple for this sample. 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. 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.
And to download the full code and assets, head here.
To get it working on your system, you’ll need to install Pygame Zero. If it has, we need to make the ball bounce. 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. 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. With these values set, the ball can be moved using a bit of trigonometry. Defining the ball as an Actor, we can add a direction and a speed parameter to it. With these elements in place, we have our play area and an ability for the player to defend the exit.Īll we need now is a ball to go bouncing around the obstacles we’ve made. If no key is pressed, then the flipper drops back down. 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.
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. Here it is: your own pinball game in less than 100 lines of code.
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. 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. 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’re not going to use any complicated maths or physics systems, just a little algebra and trigonometry. In this month’s Source Code, we’re going to put together a very simple rendition of pinball in Pygame Zero. 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. Recreating pinball machines for video games One of the earliest pinball video games – it’s the imaginatively-named Pinball on the NES.