Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Snake game- (Part 6/12)

By Prabakaran P.M February 15, 2016

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

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

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!


RSS EDABOARD.com Discussions.

  • What is the frequency changing when I change the W?
  • PDKs setup for Virtuoso
  • Why do expensive synchronous rectifier controllers exist?
  • improving in delivering a signal from source to load
  • rechargeable battery and simple alkaline battery in one single product

RSS Electro-Tech-Online.com Discussions

  • Manually actuate fuel tank selector solenoid
  • JLCPBC are using a different shipping company = less $$$$$$$$
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • need two ICs
  • MLT-8530 Buzzer on ESP32: Why Is the Sound Output Lower Than Expected?

Featured – Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
  • A filter design example
  • Types of filter responses
  • What are the two types of hardware filters?
  • What are hardware filters and their types?

Recent Articles

  • GigaDevices introduces 32-bit MCUs with integrated DSP and FPU support
  • Grinn introduces 8-core SBC supporting AI-ready embedded development
  • EPC’s 100 kHz BLDC inverter supports high-efficiency motion control
  • Melexis announces 5 W smart driver to supports sensorless FOC operation
  • STMicroelectronics’ motion sensor simplifies industrial IoT system design

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 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

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe