The Snake game is a classic video game concept that originated in the late 1970s and gained widespread popularity with its inclusion on early mobile phones and computers. It is a simple, yet addictive game that involves navigating a growing snake to collect "food" while avoiding collisions with walls or itself.
Objective:
The goal is to control a snake that moves around a grid-like game area, collect food, and grow longer with each item consumed.
The longer the snake grows, the higher the score.
Game Mechanics:
The player controls the direction of the snake (up, down, left, or right).
When the snake eats food, its length increases, and a new piece of food appears randomly on the screen.
If the snake collides with the boundaries of the game area or its own body, the game ends.
Challenges:
As the snake grows longer, navigating the game area without colliding with itself becomes increasingly difficult.
The game may increase in speed or complexity as the score rises.
The primary objective of this project is to design, implement, and play a Snake game that is controlled through a joystick, displayed on an LCD screen, and includes sound feedback via a buzzer. This project combines software and hardware development to create an interactive gaming experience. Below is a detailed breakdown of the objectives:
Game Functionality
Snake Gameplay Mechanics:
Implement core Snake game rules, such as snake movement, food collection, collision detection, and scorekeeping.
Use the joystick as an intuitive control device for directing the snake (up, down, left, right).
Display real-time gameplay on the LCD screen, showing the snake, food, and other necessary visuals.
Audio Feedback:
Use the buzzer to provide sound effects, such as:
A beep when the snake eats food.
A distinct sound when the game ends (collision with walls or itself).
Arduino UNO
It acts as the central microcontroller that processes inputs, controls the game logic, and updates the outputs.
LCD Screen (16x2 or 20x4)
It displays the game environment, including the snake, food, and possibly the score.
Joystick Module
It provides user input to control the snake’s direction (up, down, left, right).
The player uses the joystick to move the snake in the desired direction.
The Arduino reads the analog values from the joystick and translates them into direction changes.
Buzzer
It provides audio feedback to enhance the gaming experience.
Breadboard
It is used for temporary circuit connections during prototyping.
Wires and Connectors
It establishs electrical connections between components and the Arduino.
Connect VCC to 5V on Arduino.
Connect GND to GND on Arduino.
Connect X-axis to A0 and Y-axis to A1 on Arduino.
Connect the joystick button to a digital pin (e.g., D2).
Connect SDA to A4, SCL to A5, VCC to 5V, and GND to GND.
Connect the positive pin to a digital output pin (e.g., D3) on Arduino.
Connect the negative pin to GND.
LiquidCrystal_I2C (for I2C).
Initialize Game
Initialize the display
Define the grid size and snake's initial position.
Place the first "food" at initial position.
1 2 3 4 5 6 7 8 9 10 11 12 |
void setup() { lcd.init(); // Initialize the display // Coordinate of Snake x=0; y=0; // Coordinate of Food x1=4; y1=0; snake=">"; food="o"; } |
3- Joystick Input:
Read X and Y analog values from the joystick.
Map these values to determine the snake's direction (up, down, left, right).
1 2 3 4 5 6 7 8 9 10 11 |
X_joystick = analogRead(VRX_PIN); Y_joystick = analogRead(VRY_PIN); if ((X_joystick < 10) && (sens!="down")) sens="up"; if ((X_joystick > 500) && (sens!="up")) sens="down"; if ((Y_joystick < 10) && (sens!="left")) sens="right"; if ((Y_joystick > 500)&& (sens!="right")) sens="left"; |
4- Snake Movement:
Update the snake's position on the grid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if ((sens== "right" ) && (x<15) ){ snake=">"; x=x+1; } if ((sens== "left" ) && (x>0) ){ snake="<"; x=x-1; } if ((sens== "up" ) && (y>0) ){ snake="^"; y=y-1; } if ((sens== "down" ) && (y<1) ){ snake="v"; y=y+1; } |
5- Collision Detection:
Update the position of food when the snake eats food .
1 2 3 4 |
if ((x==x1)&&(y==y1)) { x1=random(0,15); y1=random(0,1); } |
6- Game Display:
Use the LCD to display the snake and food.
1 2 3 4 5 |
lcd.setCursor(x1,y1); lcd.print(food); lcd.setCursor(x,y); lcd.print(snake); |
The full program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick */ #define VRX_PIN A2 // Arduino pin connected to VRX pin #define VRY_PIN A3 // Arduino pin connected to VRY pin #define swPin 2 int xValue = 0; // To store value of the X axis int yValue = 0; // To store value of the Y axis #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 20, 4); int x,y; // Coordinate of Snake int x1,y1; //Coordinate of food String snake,food; String sens="right"; int X_joystick,Y_joystick; void setup() { lcd.init(); // Initialize the display x=0; y=0; x1=4; y1=0; snake=">"; food="o"; } void loop() { X_joystick = analogRead(VRX_PIN); Y_joystick = analogRead(VRY_PIN); lcd.backlight(); lcd.clear(); if ((X_joystick < 10) && (sens!="down")) sens="up"; if ((X_joystick > 500) && (sens!="up")) sens="down"; if ((Y_joystick < 10) && (sens!="left")) sens="right"; if ((Y_joystick > 500)&& (sens!="right")) sens="left"; if ((sens== "right" ) && (x<15) ){ snake=">"; x=x+1; } if ((sens== "left" ) && (x>0) ){ snake="<"; x=x-1; } if ((sens== "up" ) && (y>0) ){ snake="^"; y=y-1; } if ((sens== "down" ) && (y<1) ){ snake="v"; y=y+1; } if ((x==x1)&&(y==y1)) { x1=random(0,15); y1=random(0,1); } lcd.setCursor(x1,y1); lcd.print(food); lcd.setCursor(x,y); lcd.print(snake); delay(1000); lcd.clear(); } |
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info