Sunday, January 17, 2016

Raspberry Pi SenseHat Game - sensecave

For Christmas I received a lovely Sense HAT from my in-laws. I gave my wife a list of items I would like to have and the Sense HAT was one of them.

The Sense HAT is a great little accessory for the Raspberry Pi.  It includes an 8x8 RGB LED matrix, a joystick and a great range of sensors
Gyroscope
Accelerometer
Magnetometer
Temperature
Barometric pressure
Humidity

With the Sense HAT I gave myself the goal of creating a game and also learning a bit about git/GitHub.  Up until now I have never used git for my projects and just pasted code into the blog posts (if I remembered) otherwise the code is gone forever.

To that here is the link the GitHub page: https://github.com/winkleink/sensecave.git

Thinking on the game I wanted it to be a bit more than an single 8x8 and be a decent challenge.
I was reminded of the old school text cave exploring games where you went from one room to another and decided I liked this idea.

The game is sensecave.
64 rooms in an 8x8 arrangement.  Not because the screen is 8x8, just because it looks like a decent size to play. To make things more interesting instead of all rooms being connected up to 10 entrance/exits are blocked off at random each time you play giving different paths through the maze.

With the cave and the rooms sorted there has to be a quest.
The quest is to find the 7 emeralds hidden in the cave and then get out of the cave.
Just like the walls the 7 emeralds are randomly scattered around the cave making the game different every time.

We now have a location, a challenge and a quest.
Finally we needed a control method.  For the Sense Hat there are two options. Either the small joystick that emulates the cursor keys, with the push button being RETURN or the gyroscope. Realistically there was only one choice it had to be the gyroscope.
Meaning you navigate from room to room and within a room by tilting you Raspberry Pi/Sense Hat.

I love this way of navigating as once you pick up the Pi and the players dot moves the control method is completely intuitive.

In the image below you can see one of the rooms in the cave.
Red dots are the wall with, in this instance exits on all sides.
Green dot is one of the emerald
Blue dot is you.



Each time you find an emerald the screen changes to tell you how many you have found.
After all 7 have been found you're directed to go the exit.  The exit is a white dot.

Once you have exited there is a nice congratulations message and a new maze if created to start all over again.

Some of the fun challenges in programming this game were.
The list for the individual cells/rooms is y,x while the co-ordinates for the players position on the is x,y (as it should be).  This required some mental gymnastics to remember which type I was modifying to get the right result.  A few events were less than productive because of this.

I wanted to store the details for each room in a single number, so I used bitwise data

For the rooms if a side is blocked off then the number included:

top = 128
right = 64
bottom = 32
left = 16

Meaning a room with all exists blocked would equal 240.
If the top and bottom exists were blocked the room would equal 192
Then using Boolean logic the code figured out where the wall were to go.

A diamond = 1, again a room with a diamond had 1 added to it's value.
Diamonds are always drawn in the same place in a room, so that was straightforward.

Finally the exist is always in the bottom right corner and if that room is the current one a white dot is place in the room.

Finally as it can even with 8x8 get a bit disorientating a dot is added to the corners of the corner rooms.  So, you'll know when you've hit a corner and not look for the route to get to the rooms beyond.

One of the goals for this game was to run it headless and the code I found before the Egham Jam on the 17th Jan 2016 used Pygame to access the joystick.  Unfortunately, I could not get Pygame to read the joystick (which is really a keyboard) if no display was attached.  Pygame used SDL and from my reading this expects a display to enable inputs.  So, for now the joystick is not used.  I did see an alternative method for reading the joystick not using Pygame and this will be the next stage...
I really want to add difficulty levels, variable maze sizes and also variable number of emeralds.
This could be a good way for someone the extend the code.

Here's a short video.  Not the best as the lighting was too bright.  It shows the player being controlled by tilting and going from room to room.  When I work out how to do a better video I will update.



Special thanks to the Foundation and specifically Dan Fisher for this game/tutorial that I used to understand how to read the gyroscope and also how to get pixles onto the Sense HAT https://www.raspberrypi.org/learning/sense-hat-marble-maze/




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.