#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // Lebar OLED yang kita gunakan
#define SCREEN_HEIGHT 64 // Tinggi OLED yang kita gunakan
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int playerRow = 2; // Initial player position (middle of the top row)
int playerCol = 0; // Player's column (left side)
int obstacleCol = 19; // Column of the first obstacle (right side)
int obstacleCol2 = 19; // Column of the second obstacle (right side)
int obstacleRow; // Row of the first obstacle
int obstacleRow2; // Row of the second obstacle
int score = 0;
boolean gameStarted = false; // Adding a state variable to determine if the game has started
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
randomSeed(analogRead(0)); // Initialize random number generator
pinMode(2, INPUT_PULLUP); // "Up" button
pinMode(3, INPUT_PULLUP); // "Down" button
}
void loop() {
if (!gameStarted) { // Check if the game has not started
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Press Up Button");
display.setCursor(4, 10);
display.print("to start");
display.display();
if (digitalRead(2) == LOW) { // Wait for the "Up" button press to start the game
gameStarted = true;
display.clearDisplay();
}
} else { // Handle player's movement up and down
if (digitalRead(2) == LOW && playerRow > 0) {
playerRow--;
delay(200); // Delay to avoid double movement when holding the button
} else if (digitalRead(3) == LOW && playerRow < 3) {
playerRow++;
delay(200); // Delay to avoid double movement when holding the button
}
display.clearDisplay();
display.setCursor(playerCol, playerRow * 16); // Adjust position for OLED (each row is 16 pixels high)
display.print("X");
display.setCursor(obstacleCol * 6, obstacleRow * 16); // Adjust position for OLED
display.print("O");
display.setCursor(obstacleCol2 * 6, obstacleRow2 * 16); // Adjust position for OLED
display.print("O");
obstacleCol--; // Move obstacles to the left
obstacleCol2--;
if (obstacleCol < 0) { // Check and move obstacles if they have gone off the left edge
obstacleCol = 19;
obstacleRow = random(0, 3);
}
if (obstacleCol2 < 0) {
obstacleCol2 = 19;
obstacleRow2 = random(0, 3);
}
if ((obstacleCol == playerCol && obstacleRow == playerRow) || (obstacleCol2 == playerCol && obstacleRow2 == playerRow)) {
display.clearDisplay();
display.setCursor(4, 0); // Check for collisions with the player and update the game if necessary
display.print("Game Over");
display.setCursor(2, 10);
display.print("Press Up btn to restart");
display.display();
while (digitalRead(2) == HIGH) {
// Wait for the "Up" button to be pressed
}
gameStarted = false; // Reset parameters for a new game
playerRow = 2;
obstacleCol = 19;
obstacleCol2 = 19;
score = 0;
display.clearDisplay();
}
// Display game score and update it
display.setCursor(0, 48);
display.print("Score: ");
display.print(score);
score++;
display.display();
delay(50); // Delay to control game speed
}
}