#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
bool tuglaDurumu[5][5];
int tusAltPin = A1;
int tusSagPin = 9;
int tusSolPin = 10;
int potPin = A0;
int yesilLedler[] = {13, 12, 11};
bool menuSecimi = false;
bool menuSecimiYeni = false;
bool secimYapildi = false;
int canSayisi = 3;
int puan = 0;
int toplamPuan = 0;
int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
int paletX, paletY, topX, topY, topDX, topDY;
float baslangicHiziX = 2;
float baslangicHiziY = -2;
void setup() {
pinMode(tusAltPin, INPUT);
pinMode(tusSagPin, INPUT);
pinMode(tusSolPin, INPUT);
pinMode(potPin, INPUT);
for (int i = 0; i < 3; i++) {
pinMode(yesilLedler[i], OUTPUT);
digitalWrite(yesilLedler[i], HIGH);
}
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 OLED başlatma başarısız"));
while (1);
}
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
gosterMenu();
}
void loop() {
if (!secimYapildi) {
menuKontrol();
} else {
if (!menuSecimi && !menuSecimiYeni) {
oyunuBaslat();
} else if (menuSecimi && !menuSecimiYeni) {
cikisYap();
} else if (!menuSecimi && menuSecimiYeni) {
yeniMenuFonksiyonu();
}
}
}
void setDisplayNumber(int number) {
byte numbers[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};
for (int segment = 0; segment < 7; segment++) {
digitalWrite(segmentPins[segment], numbers[number][segment]);
}
}
void gosterMenu() {
puan = 0;
canSayisi = 3;
for (int i = 0; i < 3; i++) {
digitalWrite(yesilLedler[i], HIGH);
}
oled.clearDisplay();
oled.setTextSize(2);
int textWidth = 6 * 7;
int textX = (SCREEN_WIDTH - textWidth) / 2;
int textY = (SCREEN_HEIGHT - 16) / 2;
oled.setCursor(textX, textY);
if (!menuSecimi) {
oled.print(">Baslat");
oled.setCursor(textX, textY + 25);
oled.print(" Cikis");
} else {
oled.print(" Baslat");
oled.setCursor(textX, textY + 25);
oled.print(">Cikis");
}
oled.display();
}
void menuKontrol() {
bool tusSagDurum = digitalRead(tusSagPin);
bool tusSolDurum = digitalRead(tusSolPin);
bool tusAltDurum = digitalRead(tusAltPin);
if (tusSolDurum) {
menuSecimi = false;
menuSecimiYeni = false;
gosterMenu();
while (digitalRead(tusSolPin));
} else if (tusSagDurum) {
menuSecimi = true;
menuSecimiYeni = false;
gosterMenu();
while (digitalRead(tusSagPin));
}
if (tusAltDurum) {
secimYapildi = true;
while (digitalRead(tusAltPin));
}
}
void oyunuBaslat() {
puan = 0;
canSayisi = 3;
int seviye = 1;
topDX = 2;
topDY = -2;
for (int i = 0; i < 3; i++) {
digitalWrite(yesilLedler[i], HIGH);
}
setDisplayNumber(puan);
configureBricks(seviye);
paletX = SCREEN_WIDTH / 2 - 20;
paletY = SCREEN_HEIGHT - 10;
topX = paletX + 20;
topY = paletY - 10;
topDX = 2 + seviye;
topDY = -2 - seviye;
bool objeDurumu = false;
int objeX = 0;
int objeY = 0;
int objeHizi = 1;
float speedIncreaseFactor = 1.0 + 0.20 * (seviye - 1);
int initialSpeedX = 2;
int initialSpeedY = -2;
topDX = initialSpeedX * speedIncreaseFactor;
topDY = initialSpeedY * speedIncreaseFactor;
setDisplayNumber(puan);
configureBricks(seviye);
paletX = SCREEN_WIDTH / 2 - 20;
paletY = SCREEN_HEIGHT - 10;
topX = paletX + 20;
topY = paletY - 10;
while (canSayisi > 0) {
int potValue = analogRead(potPin);
paletX = map(potValue, 0, 1023, 0, SCREEN_WIDTH - 20);
topX += topDX;
topY += topDY;
if (topX <= 2 || topX >= SCREEN_WIDTH - 2) {
topDX = -topDX;
}
if (topY <= 2) {
topDY = -topDY;
}
bool tuglaKirildi = false;
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (tuglaDurumu[row][col] && topX + 2 >= col * 12 && topX - 2 <= col * 12 + 11 && topY + 2 >= row * 8 && topY - 2 <= row * 8 + 7) {
tuglaDurumu[row][col] = false;
topDY = -topDY;
tuglaKirildi = true;
if (random(100) < 10) {
objeDurumu = true;
objeX = col * 12 + 5;
objeY = row * 8 + 3;
}
}
}
}
if (tuglaKirildi) {
puan += 1;
toplamPuan += 1;
setDisplayNumber(puan % 10);
}
if (topY + 2 >= SCREEN_HEIGHT) {
canSayisi--;
digitalWrite(yesilLedler[canSayisi], LOW);
topX = paletX + 20;
topY = paletY - 10;
setDisplayNumber(puan % 10);
}
if (topX + 2 >= paletX && topX - 2 <= paletX + 30 && topY + 2 >= paletY) {
topDY = -topDY;
}
objeY += objeHizi;
if (objeDurumu && objeY >= paletY && objeX >= paletX && objeX <= paletX + 30) {
objeDurumu = false;
canSayisi++;
if (canSayisi > 3) canSayisi = 3;
digitalWrite(yesilLedler[canSayisi - 1], HIGH);
}
oled.clearDisplay();
oled.fillRect(paletX, paletY, 30, 3, WHITE);
oled.fillCircle(topX, topY, 2, WHITE);
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (tuglaDurumu[row][col]) {
oled.fillRect(col * 12, row * 8, 11, 7, WHITE);
}
}
}
if (objeDurumu) {
oled.drawLine(objeX, objeY, objeX - 3, objeY + 5, WHITE);
oled.drawLine(objeX, objeY, objeX + 3, objeY + 5, WHITE);
oled.drawLine(objeX - 3, objeY + 5, objeX + 3, objeY + 5, WHITE);
}
bool tumTuglalarKirildi = true;
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (tuglaDurumu[row][col]) {
tumTuglalarKirildi = false;
break;
}
}
if (!tumTuglalarKirildi) {
break;
}
}
if (tumTuglalarKirildi) {
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Yeni Seviye!");
oled.display();
delay(2000);
configureBricks(++seviye);
if (seviye > 5) seviye = 5;
}
oled.display();
delay(50);
}
if (canSayisi == 0) {
oyunBittiEkran();
}
}
void oyunBittiEkran() {
oled.clearDisplay();
oled.setTextSize(1);
int textWidth = 6 * 11;
int textX = (SCREEN_WIDTH - textWidth) / 2;
int textY = 10;
oled.setCursor(textX, textY);
oled.print("Oyun Bitti!");
textWidth = 6 * 14;
textX = (SCREEN_WIDTH - textWidth) / 2;
textY += 15;
oled.setCursor(textX, textY);
oled.print("Toplam Puan: ");
oled.print(toplamPuan);
oled.display();
delay(3000);
secimYapildi = false;
gosterMenu();
}
void cikisYap() {
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Cikis Yapiliyor");
oled.display();
delay(2000);
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Oyunumuza gösterdiginiz ilgi icin tesekkurler");
oled.display();
delay(2000);
secimYapildi = false;
}
void yeniMenuFonksiyonu() {
}
void configureBricks(int yeniSeviye) {
int brickWidth = 12;
int brickHeight = 8;
int startX = (SCREEN_WIDTH - (5 * brickWidth)) / 2;
int startY = 20;
memset(tuglaDurumu, 0, sizeof(tuglaDurumu));
float speedIncreaseFactor = 1.0 + 0.20 * (yeniSeviye - 1);
topDX = (int)(2 * speedIncreaseFactor) * (topDX > 0 ? 1 : -1);
topDY = (int)(-2 * speedIncreaseFactor) * (topDY > 0 ? 1 : -1);
switch(yeniSeviye) {
case 1:
for (int i = 0; i < 5; i++) {
tuglaDurumu[2][i] = true;
tuglaDurumu[i][2] = true;
}
break;
case 2:
for (int i = 0; i < 5; i++) {
tuglaDurumu[0][i] = true;
tuglaDurumu[4][i] = true;
tuglaDurumu[i][0] = true;
tuglaDurumu[i][4] = true;
}
break;
case 3:
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if ((row + col) % 2 == 0) {
tuglaDurumu[row][col] = true;
}
}
}
break;
case 4:
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if ((row + col) % 2 != 0) {
tuglaDurumu[row][col] = true;
}
}
}
break;
case 5:
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if ((row == 1 || row == 3) && (col > 0 && col < 4)) {
tuglaDurumu[row][col] = true;
}
if ((col == 1 || col == 3) && (row > 0 && row < 4)) {
tuglaDurumu[row][col] = true;
}
}
}
break;
}
}