// Libraries used
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Constants and Bitmap Definitions (same as before)...
// Game Variables
int score = 0;
int lives = 3;
// Game State
bool gameState = LOW;
// Joystick pins and display configuration (same as before)...
// Torro_Logo, Torro, Player, and Falling Figures Definitions (same as before)...
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST_PIN);
void setup () {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
delay(1000);
// Game Introduction
display.drawBitmap(xTorro_Logo, yTorro_Logo, bitmap_Torro_Logo, 75, 52, bitmap_Torro_Logo, WHITE);
display.display();
delay(2000);
display.clearDisplay();
// Game Title
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(4, 10);
display.println("El Matador");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 50);
display.println("Game Time");
delay(2000);
display.display ();
// Joystick Input Setup
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT);
digitalWrite(SEL_PIN, LOW);
randomSeed(analogRead(0));
}
void loop () {
display.clearDisplay();
// Check Game Over
if (lives <= 0) {
gameOver();
return;
}
// Call Functions
joystickControl();
fall();
checkCollision();
// Display Score and Lives
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Score: ");
display.print(score);
display.setCursor(0, 10);
display.print("Lives: ");
display.print(lives);
display.display();
}
void joystickControl() {
display.drawBitmap(xTorro, yTorro, bitmap_Torro, 17, 10, bitmap_Torro, WHITE);
int joystickV = map(analogRead(VERT_PIN), 0, 1023, -1, 1);
int joystickH = map(analogRead(HORZ_PIN), 0, 1023, -1, 1);
bool currSelPressed = digitalRead(SEL_PIN);
switch (currSelPressed) {
case HIGH:
if (gameState != currSelPressed) { gameState = HIGH; }
break;
case LOW:
if (gameState == currSelPressed) { gameState = LOW; }
break;
}
// Movement Control
if (joystickV == 1 && yTorro > 50) { yTorro -= 3; } // Up
if (joystickV == -1 && yTorro < 54) { yTorro += 3; } // Down
if (joystickH == 1 && xTorro > 0) { xTorro -= 3; } // Left
if (joystickH == -1 && xTorro < 111) { xTorro += 3; } // Right
}
void fall() {
// Falling Mann
display.drawBitmap(xMann, yMann, bitmap_Mann, 10, 10, bitmap_Mann, WHITE);
yMann += 0.25;
if (yMann >= 54) {
yMann = 0;
xMann = random(0, 111); // Reset position
}
// Falling Mann2
display.drawBitmap(xMann2, yMann2, bitmap_Mann2, 10, 10, bitmap_Mann2, WHITE);
yMann2 += 0.75;
if (yMann2 >= 54) {
yMann2 = 0;
xMann2 = random(0, 111);
}
// Falling Mann3
display.drawBitmap(xMann3, yMann3, bitmap_Mann3, 10, 10, bitmap_Mann3, WHITE);
yMann3++;
if (yMann3 >= 54) {
yMann3 = 0;
xMann3 = random(0, 111);
}
// Falling Herz
display.drawBitmap(xHerz, yHerz, bitmap_Herz, 10, 10, bitmap_Herz, WHITE);
yHerz += 1.5;
if (yHerz >= 54) {
yHerz = 0;
xHerz = random(0, 111);
}
}
void checkCollision() {
// Check collision with Mann
if (rectRect(xTorro, yTorro, wTorro, hTorro, xMann, yMann, wMann, hMann)) {
lives--; // Lose a life
yMann = 0; // Reset Mann position
xMann = random(0, 111);
}
// Check collision with Mann2
if (rectRect(xTorro, yTorro, wTorro, hTorro, xMann2, yMann2, wMann2, hMann2)) {
lives--; // Lose a life
yMann2 = 0; // Reset Mann2 position
xMann2 = random(0, 111);
}
// Check collision with Mann3
if (rectRect(xTorro, yTorro, wTorro, hTorro, xMann3, yMann3, wMann3, hMann3)) {
lives--; // Lose a life
yMann3 = 0; // Reset Mann3 position
xMann3 = random(0, 111);
}
// Check collision with Herz
if (rectRect(xTorro, yTorro, wTorro, hTorro, xHerz, yHerz, wHerz, hHerz)) {
score++; // Gain a point
yHerz = 0; // Reset Herz position
xHerz = random(0, 111);
}
}
// Rectangle Collision Function
boolean rectRect(float r1x, float r1y, int r1w, int r1h, float r2x, float r2y, int r2w, int r2h) {
return (r1x + r1w >= r2x && r1x <= r2x + r2w && r1y + r1h >= r2y && r1y <= r2y + r2h);
}
// Game Over Function
void gameOver() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 20);
display.println("Game Over!");
display.setTextSize(1);
display.setCursor(20, 40);
display.print("Final Score: ");
display.print(score);
display.display();
delay(5000); // Wait before restarting
lives = 3; // Reset lives
score = 0; // Reset score
yMann = yMann2 = yMann3 = yHerz = 0; // Reset positions
}