Programming has always been fun and it just got a lot better with development platforms like Arduino. Almost every programmer out here would have tried to develop some kind of game using the language that they learning/practising. This helps them to tune their programming skills in a fun yet productive way. I have been a big fan of Arduino ever since I got introduced to it and always wanted to try something cool with it, when I found out how cool it can get with a Graphical LCD like Nokia 5110 along with Arduino my idea of developing a game kicked in. It was an interesting way to tune up few programming skills and have fun at the same time, so though you guys might be interested in developing your game as well. Hence In this tutorial we will learn how we can create Decent Game using Arduino and Graphical LCDs.
We have already built that old fashioned Snake game using Arduino so this time we are trying something new and we named this game as Space Race Game where you need to keep your ship safe from enemy ships using joystick.
Before we start up, it is very important to plan out how you game would actually work. I went with the Nokia5110 Graphical LCD and Joystick for my hardware selection. I assume in this tutorial that you have also selected the same. Since the Nokia 5110 does not come with a lot of space we have plan our entire game inside the 84*48 pixel resolution of our display. We have already made tutorials on how to interface Nokia 5110 LCD with Arduino and Joystick with Arduino.
Within this space we have to tightly fit in the gaming area and the score board area which will display things like score and stuff. It is very important to know the pixel location of where you place stuff to keep track of the pixel locations and update them on the screen.
Once the game screen appearance is decided we have to decide the characters in our game. For my game we have only two, the player character which is a space ship and an enemy character which should kind of look like an alien spaceship. The Nokia LCD can display bitmap images, so I decided to use that option to display my space ship and the enemies.
So we will have a space ship that is racing through the aliens spaceships, this spaceship will have three lanes to changes in order to avoid a hit with the aliens. At all time the aliens can occupy only two track and the player should be able to drive through the free track. Once these ideas are concluded we can proceed with the Hardware and then the programming.
The circuit for this Arduino game is very simple; we just have to interface the Nokia 5110 LCD module and the Joystick with Arduino. The complete circuit diagram is shown below
The Nokia 5110 LCD works with the 3.3V and the Joystick module work son 5V, so make sure you connect the LCD with 3.3V only, because 5V may damage it permanently. The LCD communicates with Arduino through SPI protocol and the Joystick only read ADC to read the change in voltage. The connection set-up will look something like this below
Before we dive into the programming part it is important for you people to be convenient with the Display module and the Joystick, so you can use the following tutorials to know more about them and then get back here to make sure things are working the way we need it to!
Programming Arduino for Space Race Game:
The complete program for the game can be found at the end of this page; you can use it directly on your Arduino IDE and upload it to you Board. But if you want to know what actually happens inside the code then read further.
As always we start our program with adding our library header files, we need three libraries for this project out of which the SPI library is added to your IDE by default. The other two library has to be downloaded from the Adafruit Github page. Follow the LCD interfacing tutorial mentioned in the pre-requisites section if you are not sure how to add the library.