Some of the most basic and repetitive games can be the most fun. Consider Flappy Bird, noughts and crosses, or even catch. With this in mind, a simple drop-and-catch game makes excellent use of the Sense HAT. Code a yellow LED to drop each second, and a basket on the bottom row of LEDs. Use code to record the accelerometer reading and utilise this so that when you tilt your Sense HAT left or right, you can move the basket to catch the egg. Catch it and you play again, but if you miss one, then it breaks and it’s game over.
The full article can be found in The MagPi 47 and was written by Dan Aldred
Getting set up
Ensuring that your Raspberry Pi is turned off, attach your Sense HAT to the GPIO pins. Use the supports and screws to secure the HAT into position. Plug in the power and boot up your Pi. The Sense HAT Python library is included with the current OS image and requires no installation. Load your preferred Python editor and download the code to your Raspberry Pi, saving it into your home folder. To run the code, press F5; after a short introduction screen and a countdown, the game will begin. If you open the code in a text editor, you can customise some features of the program such as the messages, the colours used, and the splash screen.
To edit or use your own message, find the code line which begins sense.show_message – for example, on line 32. Change the text within the speech marks and add your own message. Editing the colour of the message is possible and achieved by changing the red, green, and blue (RGB) values. The maximum value for each is 255, which is the full amount of colour. These can be combined to create a wide range of colours. For example, the colour blue is [0, 0, 255].
Like the text, you can also change the colour of the egg or basket by editing the RGB values within the square brackets. These are set using the code sense.set_pixel, found on line 46. You could change the values to [0, 255, 0] to create a pea drop game!
Editing and using images
When the game begins, an image of a chicken is displayed. This is loaded from a file called chick.png, which is stored in the same folder as your egg drop program file. You can create your own images to display throughout the game and use the code sense.load_image("chick.png") on line 34, replacing the file name ‘chick.png’ with the name of your file. An effective tool to create an image is RPi 8x8 Grid Draw, a superb on-screen program that enables you to manipulate the LEDs in real time. You can change the colours, rotate them, and then export the image as code or as an 8×8 PNG file. First, we need to install the ‘Python PNG library’ – open a terminal window and type: sudo pip3 install pypng. After this has finished installing, type:
git clone https://github.com/jrobinson-uk/RPi_8x8GridDraw
Once the installation has completed, move to its folder by typing cd RPi8x8GridDraw, then type python3 sensegrid.py to run the application.
The Grid Editor enables you to select from a range of colours displayed down the right-hand side of the window. Simply choose a colour, then click the location of the LED on the grid and select ‘Play on LEDs’ to display the colour on the Sense HAT LED matrix. Clear the LEDs using the ‘Clear Grid’ button then start over. Finally, when exporting the image, you can either save as a PNG file or you can export the layout as code and import that into your program.
Remember that the image size is limited to 8×8 pixels. If you’re looking for inspiration, check out Johan Viet who has some excellent examples of 8×8-pixel art.
Dropping the egg
Both the basket and egg mechanics are similar, except that the basket only moves left and right along the x axis. This means that the y axis value stays static.
To simulate the egg dropping, the first LED is set in position and coloured yellow (line 46). The eggx is the value the LED’s horizontal position at the top of the matrix; remember, this is selected at random at the start of each drop. The eggy value refers to the LED’s vertical position. The LED is then coloured black, giving the impression that it’s no longer there (line 63). The egg_y value is increased by one, which moves to the LED down one place (line 64). Then that LED is coloured yellow, giving the appearance that the egg has dropped down one space (line 65).
The basket is set by turning on one LED (line 47); this creates a nice brownish colour. Since the basket doesn’t move up, the y value position is set to 7. The orientation of the Sense HAT is taken using pitch = sense.getorientation()['pitch'] on line 55. This value is fed into a function called basketmove (line 18), which calculates the new position of the LED in relation to the amount of tilt. Again, the LED is set to black so it appears that the basket has left its current position (line 62). Lastly, the new position is set using sense.setpixel(basketx, 7, [139, 69, 19]), where basket_x is the new value of the position of the LED along the bottom of the matrix.