#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TM1637Display.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define ENGELLER_SAYISI 8
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_ADDR);
TM1637Display sevenSegmentDisplay(11, 10);
const int paletGenislik = 20;
const int paletYukseklik = 4;
const int topCap = 3;
const int ledPins[] = {2, 3, 4};
const int maxLives = sizeof(ledPins) / sizeof(ledPins[0]);
int remainingLives = maxLives;
int topX = SCREEN_WIDTH / 2;
int topY = SCREEN_HEIGHT / 2;
int engeller[ENGELLER_SAYISI][4];
int topHizX = 1;
int topHizY = -1;
int paletX;
const int potPin = A0;
int potValue = 0;
int paletPozisyon = 0;
const int ekranGenislik = 128;
int skor = 0;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 ekran başlatılamadı!"));
while (true);
}
sevenSegmentDisplay.setBrightness(0x0f);
for (int i = 0; i < maxLives; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], HIGH);
}
engeller[0][0] = 10;
engeller[0][1] = 10;
engeller[0][2] = 20;
engeller[0][3] = 10;
engeller[1][0] = 31;
engeller[1][1] = 10;
engeller[1][2] = 20;
engeller[1][3] = 10;
engeller[2][0] = 52;
engeller[2][1] = 10;
engeller[2][2] = 20;
engeller[2][3] = 10;
engeller[3][0] = 73;
engeller[3][1] = 10;
engeller[3][2] = 20;
engeller[3][3] = 10;
engeller[4][0] = 10;
engeller[4][1] = 21;
engeller[4][2] = 20;
engeller[4][3] = 10;
engeller[5][0] = 31;
engeller[5][1] = 21;
engeller[5][2] = 20;
engeller[5][3] = 10;
engeller[6][0] = 52;
engeller[6][1] = 21;
engeller[6][2] = 20;
engeller[6][3] = 10;
engeller[7][0] = 73;
engeller[7][1] = 21;
engeller[7][2] = 20;
engeller[7][3] = 10;
display.clearDisplay();
paletX = (SCREEN_WIDTH - paletGenislik) / 2;
}
void loop() {
display.clearDisplay();
potValue = analogRead(potPin);
paletX = map(potValue, 0, 1023, 0, ekranGenislik - paletGenislik);
checkCollisionWithWalls();
hareketEtTop();
drawPalet();
drawTop();
drawWalls();
sevenSegmentDisplay.showNumberDec(skor, false);
displayLives();
display.display();
delay(5);
}
void loseLife() {
remainingLives--;
if (remainingLives >= 0) {
digitalWrite(ledPins[remainingLives], LOW);
}
if (remainingLives == 0) {
gameOver();
}
}
void resetGame() {
remainingLives = maxLives;
for (int i = 0; i < maxLives; i++) {
digitalWrite(ledPins[i], LOW);
}
}
void gameOver() {
}
void displayLives() {
for (int i = 0; i <= maxLives; i++) {
digitalWrite(ledPins[i], LOW);
}
for (int i = 0; i < remainingLives; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
void(* resetFunc) (void) = 0;
void checkCollisionWithWalls() {
if (topY + topCap >= SCREEN_HEIGHT) {
remainingLives--;
if (remainingLives <= 0) {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 20);
display.println("Oyun Bitti!");
display.display();
delay(2000);
resetFunc();
} else {
topX = SCREEN_WIDTH / 2;
topY = SCREEN_HEIGHT / 2;
}
}
if (topY + topCap >= SCREEN_HEIGHT - paletYukseklik && topX >= paletX && topX <= paletX + paletGenislik) {
topHizY *= -1;
}
for (int i = 0; i < ENGELLER_SAYISI; i++) {
int engelX = engeller[i][0];
int engelY = engeller[i][1];
int engelWidth = engeller[i][2];
int engelHeight = engeller[i][3];
int topLeftX = topX - topCap;
int topRightX = topX + topCap;
int topBottomY = topY + topCap;
int topTopY = topY - topCap;
int engelLeftX = engelX;
int engelRightX = engelX + engelWidth;
int engelBottomY = engelY + engelHeight;
int engelTopY = engelY;
if (topRightX >= engelLeftX && topLeftX <= engelRightX && topBottomY >= engelTopY && topTopY <= engelBottomY) {
engeller[i][0] = -1;
engeller[i][1] = -1;
engeller[i][2] = 0;
engeller[i][3] = 0;
topHizX *= -1;
topHizY *= -1;
skor++;
}
}
}
void drawWalls() {
display.fillRect(0, 0, 2, SCREEN_HEIGHT, SSD1306_WHITE);
display.fillRect(SCREEN_WIDTH - 2, 0, 2, SCREEN_HEIGHT, SSD1306_WHITE);
display.fillRect(0, 0, SCREEN_WIDTH, 2, SSD1306_WHITE);
display.fillRect(0, SCREEN_HEIGHT - 2, SCREEN_WIDTH, 2, SSD1306_WHITE);
for (int i = 0; i < ENGELLER_SAYISI; i++) {
int x = engeller[i][0];
int y = engeller[i][1];
int genislik = engeller[i][2];
int yukseklik = engeller[i][3];
display.fillRect(x, y, genislik, yukseklik, SSD1306_WHITE);
}
}
void drawPalet() {
display.fillRect(paletX, SCREEN_HEIGHT - paletYukseklik - 1, paletGenislik, paletYukseklik, SSD1306_WHITE);
}
void drawTop() {
display.drawCircle(topX, topY, topCap, SSD1306_WHITE);
}
void hareketEtTop() {
topX += topHizX;
topY += topHizY;
if (topX < 0 || topX > SCREEN_WIDTH - 1) {
topHizX *= -1;
}
if (topY < 0 || topY > SCREEN_HEIGHT - 1) {
topHizY *= -1;
}
}