#include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C // I2C address for the OLED display Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const int button1Pin = D0; const int button2Pin = D10; int barX = SCREEN_WIDTH / 2; // Initial X position of the bar int barY = SCREEN_HEIGHT - 10; // Y position of the bar int barWidth = 20; // Width of the bar int barHeight = 5; // Height of the bar int barSpeed = 4; // Speed of the bar int dotX, dotY; // Position of the dot int dotSpeedX = 2; // Speed of the dot in X direction int dotSpeedY = 2; // Speed of the dot in Y direction void setup() { // Initialize buttons pinMode(button1Pin, INPUT_PULLUP); pinMode(button2Pin, INPUT_PULLUP); // Initialize the display if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); } // Clear the display buffer display.clearDisplay(); display.display(); // Initialize the dot position dotX = random(0, SCREEN_WIDTH); dotY = 0; } void loop() { // Read the state of the buttons int button1State = digitalRead(button1Pin); int button2State = digitalRead(button2Pin); // Move the bar based on button presses if (button1State == LOW) { barX -= barSpeed; // Move left if (barX < 0) barX = 0; } if (button2State == LOW) { barX += barSpeed; // Move right if (barX > SCREEN_WIDTH - barWidth) barX = SCREEN_WIDTH - barWidth; } // Move the dot dotX += dotSpeedX; dotY += dotSpeedY; // Check if the dot hits the walls if (dotX < 0 || dotX > SCREEN_WIDTH) { dotSpeedX = -dotSpeedX; } // Check if the dot hits the top if (dotY < 0) { dotSpeedY = -dotSpeedY; } // Check if the dot hits the bottom (lose condition) if (dotY > SCREEN_HEIGHT) { // Reset the game display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1306_WHITE); display.setCursor(10, 20); display.println("You lost!"); display.display(); delay(2000); // Show "You lost!" for 2 seconds // Reinitialize positions dotX = random(0, SCREEN_WIDTH); dotY = 0; barX = SCREEN_WIDTH / 2; return; } // Check collision with the bar if (dotX >= barX && dotX <= (barX + barWidth) && dotY >= barY && dotY <= (barY + barHeight)) { dotSpeedY = -dotSpeedY; } // Clear the display display.clearDisplay(); // Draw the bar display.fillRect(barX, barY, barWidth, barHeight, SSD1306_WHITE); // Draw the dot display.fillCircle(dotX, dotY, 3, SSD1306_WHITE); // Update the display display.display(); // Small delay for stability delay(50); }