Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

Snake game- (Part 6/12)

By Prabakaran P.M

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

Creating Snake Game Costumes Using Raspberry Pi

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

Setting Keys to Control Snake Game

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

 Creating and Naming Variables in Snake Game Development

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

Adding Broadcast for Raspberry Pi in Snake Game

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

Resizeing Food in Snake Game using Raspberry Pi

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!

Playing Snake Game in Python with Raspberry pi

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

Circuit-Diagram-Snake-game

Project Video


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!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What is a loop calibrator? 
  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • What was before microcontrollers ?
  • Measure AC current accurateley (100mA to 10A)
  • 74HC595 creating Fake output
  • NEED HELP FOR OP-AMP IN BGR
  • Check undesired substrate mode...

RSS Electro-Tech-Online.com Discussions

  • Control Bare LCD With ATmega328p
  • Need a ducted soldering fan for solder smoke extraction
  • Sla ir li ion
  • Question about ultrasonic mist maker
  • Best way to reduce voltage in higher wattage system?
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering