This is an old revision of the document!
The purpose of this project is to be able to play a snake game with a joystick. The game is displayed on the JDMO.96A-1 display and can be played using a joystick to set the direction in which the snake will move. If the snake eats himself a “GAME OVER” text will be displayed, otherwise, the snake grows after each food that it ate.
I used the following components for this project:
Pinout scheme of the ESP32 board
Function used on this project
Setup function
This function was used to start the display and clear it, and after that to initialize the snake and set its color to white.
Loop function
Here I update the snake position then I draw it. After the snake is on the screen, I spawn a pixel random on the screen that can be eaten by the snake.
Update function On this function I update the entire snake positions (each of the pixels that make the body of the snake). Then I move the head on the direction of the pixel, I get this direction from a function called: checkJoystick. After that, I check if the snake hit the boundaries of the screen to teleport on the other side of the screen.
DrawSnake function
Used to draw the snake on the screen.
checkJoystick function
I used this function to check the position of the joystick so I can give the snake the direction that I want it to move to. In this code I also made impossible for the snake to go in the opposite direction of the current one
Spawnpixel function
I spawn the food of the snake as a pixel random on the screen.
Endgame function
Print on the screen the GAME OVER message if the snake hit himself.
Rest of the code:
Here I declared the screen and the pins that I used. I also declared 2 THRESHOLDS because when the joystick was on the middle, I constantly get values between 1850 and 1900 and I don’t want that those values to influent the movement. On the end I declared snake characteristics.
The library that I used are:
And mostly I used the for the LCD.
Conclusion As a conclusion for the project, I realized a snake game that can be controlled using a joystick. The purpose of the game is to eat as many food as possible and to not touch the rest of the body.
Bibliography: https://www.optimusdigital.ro/ro/senzori-senzori-de-atingere/742-modul-joystick-ps2-biaxial-negru-cu-5-pini.html?search_query=joystick&results=42 https://cleste.ro/ecra-oled-0-96-inch.html Placă dezvoltare NodeMCU WIFI + Bluetooth ESP32 | Cleste.ro ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials https://esp32io.com/tutorials/esp32-joystick