This article discusses how to develop a simple Snake game and how to interface a Raspberry Pi board with the game pad hardware and touches upon the techniques of interfacing a real time environment with the animated game running in Raspberry Pi. A Snake game is created using Scratch software and its hardware control capability is used in the game pad interface. You can use many softwares to program in Raspberry Pi, but Scratch is used since it has the capability of introducing many concepts without any complex syntaxes and it is especially good for the creation of graphical programs such as games.
The Snake game running on the software forms the front end of the entire system and the game pad is used to control the snake. The game has a simple odjective: to grow up by steering the snake towards the food and obtain points; and the rules and constraints are not complicated either, for instance, a snake may not touch the screen’s borders.
In this project a Raspberry Pi board is booted with Ubuntu OS and is connected to the Ethernet port of a Windows7 PC. The board is connected to the internet as explained in the project how to connect the Raspberry Pi to the internet. The IP address of the Raspberrypi board has been obtained to remote login in the TUI using PUTTY and is remotely accessed using VNC.
There is no need to install any software since Scratch comes already inbuilt with the Raspbian OS. But we should configure Scratch to have control over the GPIO’s of Raspberry Pi. This is done by the installation of add-on package to the scratch software.
Open the Scratch GPIO from the desktop and start creating the game. The main window consists of three sections left consists of blocks that you can use for your programs, middle is the place where you can make your programs and right section is where the program runs.
In the top-left hand corner, you can see eight sections (Motion, Looks, Sound, Pen, Control, Sensing, Operators and Variables). Each of these is a category that contains pieces that you can drag and drop into the scripts area to build programs.
Variables and messages:
Variables, the basic need of a program, store changes in values of data, which could be in the form of a number or text or a bit of data so that you can use it for the manipulation of the program. These are smaller sections of the memory that we can use to save our data which is to be changed and remembered. By taking lead from the following steps you can create some variables for better understanding. Once you have created a variable, you can then use it in a few ways.
Firstly, you have to set a variable to a particular value, then you can use condition loops to increase are decrease its value.
Messages:
Since our program includes a number of scripts for different patterns in our game, you may want to communicate between them. Messages are the best way to communicate between them. When one script sends a message to all by the means of broadcast all other scripts with “when I receive” block will be triggered. Like variables, messages have names, so they have to be created first, and for a script to trigger it has to be linked to the same message as the broadcast.
Step by step:
1. Create the costumes
Fig.1: Creating Snake Game Costumes Using Raspberry Pi
Create four snake costumes for the four directions and food costume which are the basic components of this game and name it in the convenient way. Create background and start screens as your creativity.
2. Set keys
Fig. 2: Setting Keys To Control Snake Game
Click on Scripts, and create a if loop with conditions for reading input pin from Raspberry Pi’s GPIO and change costume and direction using conditions, i.e. snake travelling right side should only turn up and down not the left.
3. Create and name variable
Fig. 3: Creating And Naming Variables In Snake Game Development
Click on Variables in the top-left (see above for more details on what they are). Click on Make A Variable and enter the variable name for score, food, time delay and directions.
4. Add Broadcast for start and dead conditions
Fig. 4: Adding Broadcast For Raspberry Pi In Snake Game
Create a start button on the first screen which will broadcast start message when it is clicked and
add the block Broadcast… to the bottom of the if condition block which haves conditions for the death of snake. We’ll use this to let the other sprite know that the game has ended.
5. Resize your food
Fig. 5: Resizeing Food In Snake Game Using Raspberry Pi
Select the food costume and shrink to the nominal size in the loop of the food sprite add set size to block and make it random between 60% to 140%. Make the points according to the size of the food by simple mathematical relation.
6. Play your game!
Fig. 6: Playing Snake Game In Python With Raspberry Pi
Click Start button on the first screen. You can use the direction keys of the game pad to move the mouse around. You can make it easier or harder by changing the size of the snake and the amount which it moves each loop. Good luck and happy gaming!
You can download the game file HERE.
Extract to get *.sb file and transfer it to Raspberry Pi using File server and open with SCRATCH.
Project Source Code
###
//Program to
###
Project Source Code
###
//Program to
###
Circuit Diagrams
Project Video
Filed Under: Raspberry pi
Filed Under: Raspberry pi
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.