Create Retro Snake Game With Arduino Nano and SSD130 OLED
by Skylar Jones in Circuits > Arduino
13 Views, 0 Favorites, 0 Comments
Create Retro Snake Game With Arduino Nano and SSD130 OLED
The project utilizes an Arduino Nano in conjunction with an SSD1306 OLED display to create a Snake game interface. The SSD1306 OLED display serves as the visual component, showcasing the game's graphics. User interaction is facilitated by four buttons that control the direction of the snake's movement. The control unit for the project is the Arduino Development Board (ATmega328P).
Supplies
Circuit Connections
SSD1306 OLED Display:
- VCC is connected to the Arduino's 5V pin.
- GND is connected to the Arduino's GND pin.
- SCL is connected to the Arduino's A5 pin (SCL).
- SDA is connected to the Arduino's A4 pin (SDA).
- SSD1306 OLED Display: VCC is connected to the Arduino's 5V pin.GND is connected to the Arduino's GND pin.SCL is connected to the Arduino's A5 pin (SCL).SDA is connected to the Arduino's A4 pin (SDA).
Buttons:
- One terminal of each button is connected to specific digital input pins on the Arduino.
- The other terminal is connected to GND.
- To prevent signal bouncing, a 10kΩ pull-up resistor can be added between the button terminal and GND.
- Buttons: One terminal of each button is connected to specific digital input pins on the Arduino. The other terminal is connected to GND. To prevent signal bouncing, a 10kΩ pull-up resistor can be added between the button terminal and GND.
Description
Including library files: At the beginning of the program, three library files are included for I2C communication, graphic display, and control of the OLED display.
Defining constants: Constants are defined for the size of the OLED display, the size of each snake block, and the maximum length of the snake.
OLED display initialization: The width, height, and I2C address of the OLED display are defined, and an Adafruit_SSD1306 object is created.
Initial positions of the snake and food: The initial position and direction of the snake, as well as the initial position variable for the food, are defined.
Button definition: The pin numbers for the buttons that control the snake's movement are defined.
Snake body array: A two-dimensional array is defined to store the position of each part of the snake's body.
setup() function: Initializes serial communication, the OLED display, and sets the button modes to input pull-up.
loop() function: The main loop of the program, is responsible for reading input, moving the snake, checking for collisions, drawing the screen, and controlling the game speed.
readInput() function: Reads button input and updates the snake's movement direction based on the button state.
moveSnake() function: Moves the snake according to the current direction and updates the position of each part of the snake's body.
increaseSnake() function: Increases the length of the snake when it eats food.
checkCollision() function: Checks if the snake has hit a boundary or its own body.
placeFood() function: Randomly places food on the screen, ensuring that it is not placed on the snake's body.
checkFoodCollision() function: Checks if the food collides with the snake's body.
drawScreen() function: Clears the screen and draws the snake and food.
displayGameOver() function: Displays "Game Over" information when the game ends and resets the game state.
Simulate Online
Project details here: https://www.pcbx.com/community-detail/7dd78acbf8c546319eda0409a003f23f
Conclusion
While the 3D simulation feature is still a work in progress, we would love to hear your suggestions and expectations. It's an open-source community; any sharing and feedback is welcome.
Your feedback will help our engineering team enhance the platform and better serve our users.
Join the PCBX Simulation Community and create your own project to win a surprise bag now.