#include // Include the SPI library for SPI communication #include // Include the Wire library for I2C communication #include // Include the Adafruit GFX library for graphics functions #include // Include the Adafruit SSD1306 library for OLED display control #define SCREEN_WIDTH 128 // Define the width of the OLED display #define SCREEN_HEIGHT 64 // Define the height of the OLED display #define OLED_RESET -1 // Define the reset pin for the OLED display #define SCREEN_ADDRESS 0x3C // Define the I2C address of the OLED display Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Create an instance of the Adafruit_SSD1306 class for OLED display control #define BUTTON_PIN_PLAY D0 // Define the pin for the play button #define BUTTON_PIN_JUMP D1 // Define the pin for the jump button #define BIRD_SIZE 4 // Define the size of the bird sprite #define BIRD_START_X 10 // Define the initial X position of the bird #define BIRD_START_Y 20 // Define the initial Y position of the bird #define GRAVITY 0.5 // Define the gravity force affecting the bird #define JUMP_STRENGTH -3 // Define the jump strength of the bird #define PIPE_WIDTH 10 // Define the width of the pipes #define PIPE_GAP 20 // Define the gap between pipes #define PIPE_SPEED 1 // Define the speed of the pipes int birdX, birdY; // Variables to store the position of the bird float birdYSpeed; // Variable to store the vertical speed of the bird bool gameover; // Flag to indicate if the game is over int pipeX; // Variable to store the X position of the pipes int pipeGapPosition; // Variable to store the Y position of the gap between pipes int score; // Variable to store the player's score void setup() { pinMode(BUTTON_PIN_PLAY, INPUT_PULLUP); // Set the play button pin as input with pull-up resistor pinMode(BUTTON_PIN_JUMP, INPUT_PULLUP); // Set the jump button pin as input with pull-up resistor Serial.begin(9600); // Initialize serial communication if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Initialize the OLED display Serial.println(F("SSD1306 allocation failed")); // Print an error message if display initialization fails for(;;); // Loop indefinitely } display.display(); // Initialize the display delay(2000); // Delay for 2 seconds display.clearDisplay(); // Clear the display // Display the custom message display.setTextSize(1); // Set text size to 1 display.setTextColor(SSD1306_WHITE); // Set text color to white display.setCursor(10, 20); // Set cursor position for the first line of text display.println("Fab Lab Puebla."); // Print the first line of text display.setCursor(10, 30); // Set cursor position for the second line of text display.println("By: Victor"); // Print the second line of text display.display(); // Display the text on the OLED display delay(4000); // Delay for 4 seconds startGame(); // Start the game } //============================= void startGame() { // Initialize game parameters birdX = BIRD_START_X; // Set the initial X position of the bird birdY = BIRD_START_Y; // Set the initial Y position of the bird birdYSpeed = 0; // Reset the vertical speed of the bird gameover = false; // Reset the gameover flag pipeX = SCREEN_WIDTH; // Set the initial X position of the pipes pipeGapPosition = random(10, SCREEN_HEIGHT - PIPE_GAP - 10); // Set the initial Y position of the gap between pipes score = 0; // Reset the score display.clearDisplay(); // Clear the display } void loop() { // Start a new game if play button is pressed and game is over if (digitalRead(BUTTON_PIN_PLAY) == HIGH && gameover) { startGame(); } if (!gameover) { // Game loop handleInput(); // Handle player input updateBird(); // Update bird position updatePipes(); // Update pipes position checkCollisions(); // Check for collisions draw(); // Draw game elements delay(50); // Delay for smoother gameplay } } void handleInput() { // Make the bird jump if jump button is pressed and bird is above the ground if (digitalRead(BUTTON_PIN_JUMP) == HIGH && birdY > 0) { birdYSpeed = JUMP_STRENGTH; // Set the vertical speed of the bird for jump } } void updateBird() { birdYSpeed += GRAVITY; // Apply gravity to the bird birdY += birdYSpeed; // Update bird's vertical position // Keep the bird within the screen bounds if (birdY > SCREEN_HEIGHT - BIRD_SIZE) { birdY = SCREEN_HEIGHT - BIRD_SIZE; birdYSpeed = 0; // Stop the bird from falling through the ground } if (birdY < 0) { birdY = 0; birdYSpeed = 0; // Stop the bird from going above the screen } } void updatePipes() { pipeX -= PIPE_SPEED; // Move the pipes to the left // Reset pipe position if it goes off-screen if (pipeX < -PIPE_WIDTH) { pipeX = SCREEN_WIDTH; pipeGapPosition = random(10, SCREEN_HEIGHT - PIPE_GAP - 10); // Randomize the position of the gap between pipes score++; // Increase the score when a new pipe appears } } void checkCollisions() { // Check for collisions between bird and pipes if ((birdX + BIRD_SIZE > pipeX && birdX < pipeX + PIPE_WIDTH) && (birdY < pipeGapPosition || birdY + BIRD_SIZE > pipeGapPosition + PIPE_GAP)) { gameOver(); // If collision detected, end the game } } void gameOver() { gameover = true; // Set gameover flag to true display.setTextSize(1); // Set text size to 1 display.setTextColor(SSD1306_WHITE); // Set text color to white display.setCursor(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4); // Set cursor position for "GAME OVER" message display.println("GAME OVER"); // Display "GAME OVER" message display.setCursor(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4 + 20); // Set cursor position for display.print("Score: "); display.println(score); // Display the final score display.display(); // Display the content on the OLED screen delay(2000); // Delay for 2 seconds } void draw() { display.clearDisplay(); // Clear the display buffer display.fillRect(birdX, birdY, BIRD_SIZE, BIRD_SIZE, SSD1306_WHITE); // Draw the bird // Draw the upper part of the pipe display.fillRect(pipeX, 0, PIPE_WIDTH, pipeGapPosition, SSD1306_WHITE); // Draw the lower part of the pipe display.fillRect(pipeX, pipeGapPosition + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT - pipeGapPosition - PIPE_GAP, SSD1306_WHITE); display.display(); // Display the content on the OLED screen }