#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <FastLED.h>
// DEBUG AYARLARI
#define DEBUG_SERIAL true
#define DEBUG_COIN false
// Pin Tanımlamaları
#define PLAYER1_START_BTN 13
#define PLAYER1_BURN_BTN 12
#define PLAYER1_GAME_LED 14
#define PLAYER2_START_BTN 27
#define PLAYER2_BURN_BTN 26
#define PLAYER2_GAME_LED 35
#define COIN_SENSOR 15
#define GAME_STATUS_LED 25
// SES PİNİ - PAM8400 için
#define BUZZER_PIN 33
// FastLED Ayarları
#define PIXEL_PIN 32
#define NUM_LEDS 40
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define BRIGHTNESS 100
// TFT Ekran Pinleri
#define TFT1_CS 23
#define TFT1_RST 22
#define TFT1_DC 1
#define TFT1_MOSI 3
#define TFT1_SCK 21
#define TFT2_CS 19
#define TFT2_RST 18
#define TFT2_DC 5
#define TFT2_MOSI 17
#define TFT2_SCK 16
// EEPROM
#define EEPROM_SIZE 64
#define EEPROM_TOTAL_GAMES_ADDR 0
#define EEPROM_MAGIC_NUMBER 123
// FastLED dizileri
CRGB leds[NUM_LEDS];
// Ses değişkenleri
bool isPlayingSound = false;
unsigned long soundStartTime = 0;
// Oyun değişkenleri
bool player1BonusActive = false;
bool player2BonusActive = false;
unsigned long player1BonusStartTime = 0;
unsigned long player2BonusStartTime = 0;
const unsigned long BONUS_DURATION = 6000; // 6 saniye
// Havai fişek efekti
struct Firework {
int x, y;
int radius;
uint16_t color;
bool active;
unsigned long startTime;
};
Firework player1Fireworks[10];
Firework player2Fireworks[10];
const int MAX_FIREWORKS = 10;
// Game Over efekti
bool player1GameOverBlink = false;
bool player2GameOverBlink = false;
unsigned long player1LastBlinkTime = 0;
unsigned long player2LastBlinkTime = 0;
bool player1BlinkState = false;
bool player2BlinkState = false;
SPIClass hspi(HSPI);
SPIClass vspi(VSPI);
Adafruit_ILI9341 tft1(&vspi, TFT1_DC, TFT1_CS, TFT1_RST);
Adafruit_ILI9341 tft2(&hspi, TFT2_DC, TFT2_CS, TFT2_RST);
LiquidCrystal_I2C lcd1(0x27, 16, 2);
enum PlayerState { PLAYER_MENU, PLAYER_PLAYING, PLAYER_GAME_OVER };
struct Player {
PlayerState state = PLAYER_MENU;
int score = 2000;
int previousScore = 2000;
bool invincible = false, lastStartBtnState = HIGH, lastBurnBtnState = HIGH;
bool blinkState = false, isBurning = false, hasCredit = false;
unsigned long lastUpdateTime = 0, gameStartTime = 0, gameEndTime = 0;
unsigned long lastBlinkTime = 0, burnStartTime = 0;
unsigned long burnEffectEndTime = 0;
unsigned long errorDisplayEndTime = 0;
Adafruit_ILI9341* tft;
int ledStartIndex;
Firework* fireworks;
bool* bonusActive;
unsigned long* bonusStartTime;
int startBtnPin, burnBtnPin, gameLedPin;
int lastDigitPositions[5];
bool* gameOverBlink;
bool* blinkStatePtr;
unsigned long* lastBlinkTimePtr;
// Debounce için yeni değişkenler
unsigned long lastStartDebounceTime = 0;
unsigned long lastBurnDebounceTime = 0;
};
Player player1, player2;
unsigned long totalGames = 0;
bool showingTotalGames = false, menuBlinkState = true;
unsigned long totalGamesDisplayStartTime = 0, menuLastBlink = 0, lastLcdUpdate = 0;
unsigned long lastAnimationUpdate = 0, lastSaveTime = 0;
uint8_t animationOffset = 0;
bool player1StartPressed = false, player1BurnPressed = false;
bool player2StartPressed = false, player2BurnPressed = false;
unsigned long allButtonsPressedTime = 0;
bool allButtonsActive = false;
// Tüm butonların debounce zamanları
unsigned long lastP1StartDebounce = 0, lastP1BurnDebounce = 0;
unsigned long lastP2StartDebounce = 0, lastP2BurnDebounce = 0;
bool lastP1StartState = HIGH, lastP1BurnState = HIGH;
bool lastP2StartState = HIGH, lastP2BurnState = HIGH;
bool gameStatusLedActive = false;
unsigned long gameStatusLedStartTime = 0;
const unsigned long GAME_STATUS_LED_DURATION = 2000;
unsigned long lastDebugPrint = 0;
const unsigned long DEBUG_INTERVAL = 1000;
bool ledsInitialized = false;
// PALETLER
DEFINE_GRADIENT_PALETTE( fire_gp ) { 0, 0, 0, 0, 128, 0, 255, 0, 224, 0,255, 0, 255, 0,255,0 };
DEFINE_GRADIENT_PALETTE( ocean_gp ) { 0, 0, 0, 0, 64, 0, 0,255, 128, 0,255,255, 192,255,255,255, 255, 0, 0,255};
DEFINE_GRADIENT_PALETTE( gameover_gp ) { 0, 255, 0, 0, 64, 128, 0, 0, 128, 255, 255, 0, 192, 128, 128, 128, 255, 0, 0, 0 };
CRGBPalette16 firePalette = fire_gp;
CRGBPalette16 oceanPalette = ocean_gp;
CRGBPalette16 gameOverPalette = gameover_gp;
// ⭐⭐⭐ TAMAMEN FARKLI MÜZİK FONKSİYONLARI ⭐⭐⭐
void playStartSound() {
Serial.println("🔊 STARTING MÜZİĞİ ÇALINIYOR - TAMAMEN FARKLI");
noTone(BUZZER_PIN);
delay(50);
// ⭐ TAMAMEN FARKLI STARTING MÜZİĞİ - VICTORY'DEN BAĞIMSIZ ⭐
tone(BUZZER_PIN, 400, 250); // 1. nota - Düşük frekans
delay(280);
tone(BUZZER_PIN, 600, 200); // 2. nota - Orta frekans
delay(230);
tone(BUZZER_PIN, 500, 180); // 3. nota - Tekrar düşük
delay(210);
tone(BUZZER_PIN, 800, 350); // 4. nota - Yüksek frekans
delay(400);
noTone(BUZZER_PIN);
Serial.println("✅ STARTING müziği tamamlandı");
}
void playVictorySound() {
Serial.println("🎉 VICTORY MÜZİĞİ ÇALINIYOR - TAM VERSİYON");
noTone(BUZZER_PIN);
delay(50);
// VICTORY MÜZİĞİ - TAM 6 NOTA
tone(BUZZER_PIN, 659, 200); // Mi - 1. nota
delay(220);
tone(BUZZER_PIN, 784, 180); // Sol - 2. nota
delay(200);
tone(BUZZER_PIN, 880, 160); // La - 3. nota
delay(180);
tone(BUZZER_PIN, 1047, 350); // Do - 4. nota
delay(370);
tone(BUZZER_PIN, 880, 180); // La - 5. nota
delay(200);
tone(BUZZER_PIN, 1047, 450); // Do - 6. nota
delay(470);
noTone(BUZZER_PIN);
Serial.println("✅ VICTORY müziği tamamlandı");
}
void playGameOverSound() {
Serial.println("💀 GAME OVER sesi çalınıyor");
noTone(BUZZER_PIN);
delay(50);
tone(BUZZER_PIN, 392, 350); // Sol
delay(370);
tone(BUZZER_PIN, 349, 350); // Fa
delay(370);
tone(BUZZER_PIN, 330, 350); // Mi
delay(370);
tone(BUZZER_PIN, 294, 600); // Re
delay(620);
noTone(BUZZER_PIN);
}
void playAlarmSound() {
Serial.println("🚨 ALARM sesi çalınıyor");
noTone(BUZZER_PIN);
delay(50);
tone(BUZZER_PIN, 880, 2000); // 2 saniye alarm
}
void stopSound() {
noTone(BUZZER_PIN);
}
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("====================================");
Serial.println("🎮 OYUN SİSTEMİ - BEDAVA MOD!");
Serial.println("====================================");
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
initializeLEDs();
initializeAllOutputs();
EEPROM.begin(EEPROM_SIZE);
const int inputPins[] = {PLAYER1_START_BTN, PLAYER1_BURN_BTN, PLAYER2_START_BTN, PLAYER2_BURN_BTN};
for(int i = 0; i < 4; i++) pinMode(inputPins[i], INPUT_PULLUP);
Wire.begin(4, 2);
delay(1000);
lcd1.begin(16, 2);
lcd1.backlight();
lcd1.print("SISTEM HAZIR");
if(!readTotalGames()) {
totalGames = 0;
saveTotalGames();
}
vspi.begin(TFT1_SCK, -1, TFT1_MOSI, TFT1_CS);
hspi.begin(TFT2_SCK, -1, TFT2_MOSI, TFT2_CS);
tft1.begin(); tft1.setRotation(1); tft1.fillScreen(ILI9341_BLACK);
tft2.begin(); tft2.setRotation(1); tft2.fillScreen(ILI9341_BLACK);
initializePlayer(&player1, &tft1, 0, player1Fireworks, &player1BonusActive, &player1BonusStartTime, &player1GameOverBlink, &player1BlinkState, &player1LastBlinkTime, PLAYER1_START_BTN, PLAYER1_BURN_BTN, PLAYER1_GAME_LED);
initializePlayer(&player2, &tft2, 20, player2Fireworks, &player2BonusActive, &player2BonusStartTime, &player2GameOverBlink, &player2BlinkState, &player2LastBlinkTime, PLAYER2_START_BTN, PLAYER2_BURN_BTN, PLAYER2_GAME_LED);
Serial.println("✅ TUM SISTEMLER HAZIR!");
updateLCD();
showStartScreen();
ledsInitialized = true;
digitalWrite(GAME_STATUS_LED, HIGH);
}
void loop() {
unsigned long currentTime = millis();
if (DEBUG_SERIAL && currentTime - lastDebugPrint >= DEBUG_INTERVAL) {
lastDebugPrint = currentTime;
Serial.print("Sistem calisiyor - ToplamOyun:");
Serial.println(totalGames);
}
checkAllButtons(currentTime);
if(showingTotalGames) {
if(currentTime - totalGamesDisplayStartTime >= 7000) {
showingTotalGames = false;
updateLCD();
}
} else if(currentTime - lastLcdUpdate >= 1000) {
lastLcdUpdate = currentTime;
updateLCD();
}
updateGameStatusLed(currentTime);
updatePixelAnimations(currentTime);
handlePlayer(&player1, currentTime);
handlePlayer(&player2, currentTime);
if(currentTime - menuLastBlink >= 500) {
menuLastBlink = currentTime;
menuBlinkState = !menuBlinkState;
updateStartDisplay();
}
if(currentTime - lastSaveTime >= 30000) {
saveTotalGames();
lastSaveTime = currentTime;
}
if (player1.state == PLAYER_MENU && player2.state == PLAYER_MENU && !gameStatusLedActive) {
digitalWrite(GAME_STATUS_LED, HIGH);
}
}
// DİĞER FONKSİYONLAR
void initializeLEDs() {
Serial.println("🔴 FastLED başlatılıyor...");
FastLED.addLeds<LED_TYPE, PIXEL_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
testLEDs();
Serial.println("✅ FastLED başlatıldı");
}
void testLEDs() {
Serial.println("🎮 LED TESTİ...");
fill_solid(leds, 20, CRGB::Red);
fill_solid(leds + 20, 20, CRGB::Green);
FastLED.show(); delay(1000);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
Serial.println("✅ LED testi tamamlandı");
}
void updatePixelAnimations(unsigned long currentTime) {
if(currentTime - lastAnimationUpdate >= 15) {
lastAnimationUpdate = currentTime;
animationOffset++;
updatePlayerLEDs(&player1, animationOffset);
updatePlayerLEDs(&player2, animationOffset + 64);
FastLED.show();
}
}
void updatePlayerLEDs(Player* player, uint8_t offset) {
int startIndex = player->ledStartIndex;
int numLeds = 20;
if (player->state == PLAYER_MENU) {
fill_rainbow(leds + startIndex, numLeds, offset * 3, 10);
} else if (player->state == PLAYER_PLAYING) {
if (player->isBurning) {
for(int i = 0; i < numLeds; i++) {
int heat = inoise8(i * 30 + offset * 8, millis() / 10);
leds[startIndex + i] = ColorFromPalette(firePalette, heat);
}
} else {
fill_solid(leds + startIndex, numLeds, CHSV(0, 255, triwave8(offset * 6)));
}
} else if (player->state == PLAYER_GAME_OVER) {
bool blinkOn = (millis() % 300) < 150;
if (blinkOn) {
for(int i = 0; i < numLeds; i++) {
int intensity = sin8((i * 20 + offset * 10) % 255);
leds[startIndex + i] = ColorFromPalette(gameOverPalette, intensity);
}
} else {
fill_solid(leds + startIndex, numLeds, CRGB::Black);
}
}
}
void initializeAllOutputs() {
const int outputPins[] = {PLAYER1_GAME_LED, PLAYER2_GAME_LED, GAME_STATUS_LED,
TFT1_CS, TFT2_CS, TFT1_RST, TFT1_DC, TFT2_RST, TFT2_DC};
for(int i = 0; i < 9; i++) {
pinMode(outputPins[i], OUTPUT);
digitalWrite(outputPins[i], LOW);
}
}
void initializePlayer(Player* player, Adafruit_ILI9341* tft, int ledStartIndex, Firework* fireworks, bool* bonusActive, unsigned long* bonusStartTime, bool* gameOverBlink, bool* blinkStatePtr, unsigned long* lastBlinkTimePtr, int startBtn, int burnBtn, int gameLedPin) {
player->tft = tft;
player->ledStartIndex = ledStartIndex;
player->fireworks = fireworks;
player->bonusActive = bonusActive;
player->bonusStartTime = bonusStartTime;
player->gameOverBlink = gameOverBlink;
player->blinkStatePtr = blinkStatePtr;
player->lastBlinkTimePtr = lastBlinkTimePtr;
player->startBtnPin = startBtn;
player->burnBtnPin = burnBtn;
player->gameLedPin = gameLedPin;
player->previousScore = player->score;
for(int i = 0; i < 5; i++) player->lastDigitPositions[i] = 0;
for(int i = 0; i < MAX_FIREWORKS; i++) {
player->fireworks[i].active = false;
}
*(player->gameOverBlink) = false;
*(player->blinkStatePtr) = false;
*(player->lastBlinkTimePtr) = 0;
// Debounce değişkenlerini sıfırla
player->lastStartDebounceTime = 0;
player->lastBurnDebounceTime = 0;
}
bool readTotalGames() {
if(EEPROM.readInt(EEPROM_TOTAL_GAMES_ADDR) == EEPROM_MAGIC_NUMBER) {
totalGames = EEPROM.readULong(EEPROM_TOTAL_GAMES_ADDR + 4);
return true;
} return false;
}
void saveTotalGames() {
EEPROM.writeInt(EEPROM_TOTAL_GAMES_ADDR, EEPROM_MAGIC_NUMBER);
EEPROM.writeULong(EEPROM_TOTAL_GAMES_ADDR + 4, totalGames);
EEPROM.commit();
}
void updateLCD() {
lcd1.clear();
lcd1.print("BEDAVA OYUN");
lcd1.setCursor(0,1);
lcd1.print("BASLATMAK ICIN");
}
void showStartScreen() {
updateStartDisplay();
}
void updateStartDisplay() {
updateTFTDisplay(&player1, &tft1);
updateTFTDisplay(&player2, &tft2);
}
void updateTFTDisplay(Player* player, Adafruit_ILI9341* tft) {
if(player->state != PLAYER_MENU) return;
// KIRMIZI ARKA PLAN
tft->fillScreen(ILI9341_RED);
// SARI RENK - 2 KAT BÜYÜK YAZI
tft->setTextColor(ILI9341_YELLOW);
tft->setTextSize(6); // 2 kat büyük (önceki 3'tü, şimdi 6)
// ÜST SATIR: "PRESS" - ORTALANMIŞ
String pressText = "PRESS";
int pressWidth = pressText.length() * 36; // 6x6 = 36 pixel genişlik
int pressX = (320 - pressWidth) / 2;
tft->setCursor(pressX, 60);
tft->print(pressText);
// ALT SATIR: "BUTTON" - ORTALANMIŞ
String buttonText = "BUTTON";
int buttonWidth = buttonText.length() * 36; // 6x6 = 36 pixel genişlik
int buttonX = (320 - buttonWidth) / 2;
tft->setCursor(buttonX, 140);
tft->print(buttonText);
}
// Debounce sabiti (ms)
const unsigned long DEBOUNCE_DELAY = 50;
void checkAllButtons(unsigned long currentTime) {
// Her buton için debounce kontrollü okuma
bool rawP1Start = digitalRead(PLAYER1_START_BTN) == LOW;
if (rawP1Start != lastP1StartState) {
lastP1StartDebounce = currentTime;
}
if ((currentTime - lastP1StartDebounce) > DEBOUNCE_DELAY && rawP1Start != lastP1StartState) {
lastP1StartState = rawP1Start;
}
bool p1Start = lastP1StartState;
bool rawP1Burn = digitalRead(PLAYER1_BURN_BTN) == LOW;
if (rawP1Burn != lastP1BurnState) {
lastP1BurnDebounce = currentTime;
}
if ((currentTime - lastP1BurnDebounce) > DEBOUNCE_DELAY && rawP1Burn != lastP1BurnState) {
lastP1BurnState = rawP1Burn;
}
bool p1Burn = lastP1BurnState;
bool rawP2Start = digitalRead(PLAYER2_START_BTN) == LOW;
if (rawP2Start != lastP2StartState) {
lastP2StartDebounce = currentTime;
}
if ((currentTime - lastP2StartDebounce) > DEBOUNCE_DELAY && rawP2Start != lastP2StartState) {
lastP2StartState = rawP2Start;
}
bool p2Start = lastP2StartState;
bool rawP2Burn = digitalRead(PLAYER2_BURN_BTN) == LOW;
if (rawP2Burn != lastP2BurnState) {
lastP2BurnDebounce = currentTime;
}
if ((currentTime - lastP2BurnDebounce) > DEBOUNCE_DELAY && rawP2Burn != lastP2BurnState) {
lastP2BurnState = rawP2Burn;
}
bool p2Burn = lastP2BurnState;
if (p1Start && p1Burn && p2Start && p2Burn) {
if (!allButtonsActive) {
allButtonsActive = true;
allButtonsPressedTime = currentTime;
}
else if (currentTime - allButtonsPressedTime >= 1000 && !showingTotalGames) {
showingTotalGames = true;
totalGamesDisplayStartTime = currentTime;
lcd1.clear();
lcd1.print("TOPLAM OYUN");
lcd1.setCursor(0,1);
lcd1.print("SAYISI:");
lcd1.print(totalGames);
}
} else {
allButtonsActive = false;
}
}
void updateGameStatusLed(unsigned long currentTime) {
if (gameStatusLedActive && currentTime - gameStatusLedStartTime >= GAME_STATUS_LED_DURATION) {
digitalWrite(GAME_STATUS_LED, HIGH);
gameStatusLedActive = false;
}
}
void activateGameStatusLed(unsigned long currentTime) {
digitalWrite(GAME_STATUS_LED, LOW);
gameStatusLedActive = true;
gameStatusLedStartTime = currentTime;
}
void handlePlayer(Player* player, unsigned long currentTime) {
switch(player->state) {
case PLAYER_MENU: handlePlayerMenu(player, currentTime); break;
case PLAYER_PLAYING: handlePlayerPlaying(player, currentTime); break;
case PLAYER_GAME_OVER: handlePlayerGameOver(player, currentTime); break;
}
}
void handlePlayerMenu(Player* player, unsigned long currentTime) {
// Debounce kontrollü start butonu okuma
bool rawStartState = digitalRead(player->startBtnPin);
if (rawStartState != player->lastStartBtnState) {
player->lastStartDebounceTime = currentTime;
}
if ((currentTime - player->lastStartDebounceTime) > DEBOUNCE_DELAY) {
if (rawStartState != player->lastStartBtnState) {
player->lastStartBtnState = rawStartState;
}
}
bool currentStartBtnState = player->lastStartBtnState;
if(!currentStartBtnState && currentTime - player->gameStartTime > 500) {
Serial.println("🔄 START BUTONUNA BASILDI - Starting müziği çalacak");
startPlayerGame(player);
updateLCD();
}
}
void handlePlayerPlaying(Player* player, unsigned long currentTime) {
if (player->isBurning) {
if (currentTime >= player->burnEffectEndTime) {
player->isBurning = false;
player->invincible = false;
drawPlayerScore(player);
}
return;
}
if (*(player->bonusActive)) {
updateBonusCelebration(player, currentTime);
return;
}
if(!player->invincible && currentTime - player->lastUpdateTime >= 80) {
player->lastUpdateTime = currentTime;
player->score--;
if(player->score != player->previousScore) {
String currentStr = String(player->score);
String previousStr = String(player->previousScore);
if(currentStr.length() != previousStr.length()) {
drawPlayerScore(player);
} else {
for(int i = 0; i < currentStr.length(); i++) {
if(currentStr[i] != previousStr[i]) {
updateScoreDigit(player, i, currentStr[i]);
}
}
}
player->previousScore = player->score;
}
if(player->score <= 0) {
endPlayerGame(player, "GAME OVER");
activateGameStatusLed(currentTime);
}
}
// Debounce kontrollü start butonu
bool rawStartState = digitalRead(player->startBtnPin);
if (rawStartState != player->lastStartBtnState) {
player->lastStartDebounceTime = currentTime;
}
if ((currentTime - player->lastStartDebounceTime) > DEBOUNCE_DELAY) {
if (rawStartState != player->lastStartBtnState) {
player->lastStartBtnState = rawStartState;
}
}
bool currentStartBtnState = player->lastStartBtnState;
if(!currentStartBtnState && currentTime - player->gameStartTime > 2000) {
if(player->score >= 1000) {
Serial.print("🎯 MANUEL BİTİRME - BONUS KAZANILDI! Skor = ");
Serial.println(player->score);
startBonusCelebration(player);
} else {
endPlayerGame(player, "FINISHED!");
}
}
// Debounce kontrollü burn butonu
bool rawBurnState = digitalRead(player->burnBtnPin);
if (rawBurnState != player->lastBurnBtnState) {
player->lastBurnDebounceTime = currentTime;
}
if ((currentTime - player->lastBurnDebounceTime) > DEBOUNCE_DELAY) {
if (rawBurnState != player->lastBurnBtnState) {
player->lastBurnBtnState = rawBurnState;
}
}
bool currentBurnBtnState = player->lastBurnBtnState;
if(!currentBurnBtnState && !player->invincible && !player->isBurning) {
burnAction(player, currentTime);
}
}
void handlePlayerGameOver(Player* player, unsigned long currentTime) {
if (*(player->gameOverBlink)) {
drawGameOverEffect(player, currentTime);
}
if(currentTime - player->gameEndTime >= 10000) {
resetPlayer(player);
updateStartDisplay();
updateLCD();
}
}
// ⭐⭐⭐ DÜZELTİLMİŞ START PLAYER GAME - KESİN STARTING MÜZİĞİ ⭐⭐⭐
void startPlayerGame(Player* player) {
Serial.println("🎬 OYUN BAŞLIYOR - STARTING MÜZİĞİ ÇALACAK");
player->state = PLAYER_PLAYING;
player->score = 2000;
player->previousScore = 2000;
player->lastUpdateTime = player->gameStartTime = millis();
digitalWrite(player->gameLedPin, LOW);
player->tft->fillScreen(ILI9341_RED);
drawInitialScore(player);
// Bonus durumunu sıfırla
*(player->bonusActive) = false;
// ⭐⭐⭐ KESİNLİKLE STARTING MÜZİĞİ ÇAL ⭐⭐⭐
playStartSound();
Serial.println("✅ STARTING müziği çalındı");
}
void endPlayerGame(Player* player, const char* message) {
stopSound();
updateTotalGames();
player->state = PLAYER_GAME_OVER;
player->gameEndTime = millis();
digitalWrite(player->gameLedPin, LOW);
activateGameStatusLed(millis());
if(strcmp(message, "YOU WIN!") == 0 || player->score >= 1000) {
player->tft->fillScreen(ILI9341_BLACK);
drawYouWin(player->tft, player->score);
Serial.println("🎉 YOU WIN! Victory ekranı gösteriliyor");
}
else {
playGameOverSound();
*(player->gameOverBlink) = true;
*(player->blinkStatePtr) = true;
*(player->lastBlinkTimePtr) = millis();
drawGameOverContent(player->tft, player->score);
if(strcmp(message, "GAME OVER") == 0) {
Serial.println("💀 GAME OVER! GameOver sesi çalınıyor");
} else {
Serial.println("🎮 FINISHED! GameOver sesi çalınıyor");
}
}
}
void updateTotalGames() {
totalGames++;
saveTotalGames();
}
void resetPlayer(Player* player) {
player->state = PLAYER_MENU;
player->score = 2000;
player->previousScore = 2000;
player->invincible = false;
player->isBurning = false;
player->hasCredit = false;
player->lastUpdateTime = 0;
player->gameStartTime = 0;
player->gameEndTime = 0;
player->lastBlinkTime = 0;
player->burnStartTime = 0;
player->burnEffectEndTime = 0;
player->errorDisplayEndTime = 0;
*(player->bonusActive) = false;
*(player->gameOverBlink) = false;
// Debounce değişkenlerini sıfırla
player->lastStartDebounceTime = 0;
player->lastBurnDebounceTime = 0;
player->lastStartBtnState = HIGH;
player->lastBurnBtnState = HIGH;
}
void burnAction(Player* player, unsigned long currentTime) {
player->score = max(0, player->score - 200);
player->invincible = true;
player->isBurning = true;
player->burnStartTime = currentTime;
player->burnEffectEndTime = currentTime + 2000;
player->tft->fillScreen(ILI9341_RED);
activateGameStatusLed(currentTime);
playAlarmSound();
Serial.println("🔥 Yanma eylemi - Alarm sesi çalınıyor");
}
void drawInitialScore(Player* player) {
if(player->state != PLAYER_PLAYING) return;
player->tft->fillScreen(ILI9341_GREEN);
player->tft->setTextColor(ILI9341_BLACK);
String scoreStr = String(player->score);
int textSize = calculateTextSize(scoreStr) * 2;
calculateDigitPositions(player, scoreStr, textSize);
player->tft->setTextSize(textSize);
for(int i = 0; i < scoreStr.length(); i++) {
player->tft->setCursor(player->lastDigitPositions[i], calculateCenteredY(textSize, player->tft));
player->tft->print(scoreStr[i]);
}
}
void updateScoreDigit(Player* player, int digitIndex, char newDigit) {
if(player->state != PLAYER_PLAYING) return;
String scoreStr = String(player->score);
int textSize = calculateTextSize(scoreStr) * 2;
int charWidth = 6 * textSize;
int charHeight = 8 * textSize;
int y = calculateCenteredY(textSize, player->tft);
int x = player->lastDigitPositions[digitIndex];
player->tft->fillRect(x, y, charWidth, charHeight, ILI9341_GREEN);
player->tft->setTextColor(ILI9341_BLACK);
player->tft->setTextSize(textSize);
player->tft->setCursor(x, y);
player->tft->print(newDigit);
}
void drawPlayerScore(Player* player) {
if(player->state != PLAYER_PLAYING) return;
drawInitialScore(player);
}
int calculateTextSize(String text) {
int len = text.length();
if(len <= 3) return 6;
if(len <= 4) return 5;
if(len <= 5) return 4;
return 3;
}
int calculateCenteredX(String text, int textSize, Adafruit_ILI9341* tft) {
int charWidth = 6 * textSize;
int textWidth = text.length() * charWidth;
return (320 - textWidth) / 2;
}
int calculateCenteredY(int textSize, Adafruit_ILI9341* tft) {
int charHeight = 8 * textSize;
return (240 - charHeight) / 2;
}
void calculateDigitPositions(Player* player, String scoreStr, int textSize) {
int charWidth = 6 * textSize;
int totalWidth = scoreStr.length() * charWidth;
int startX = (320 - totalWidth) / 2;
for(int i = 0; i < scoreStr.length(); i++)
player->lastDigitPositions[i] = startX + (i * charWidth);
}
// ⭐⭐⭐ DÜZELTİLMİŞ KAZANMA SİSTEMİ ⭐⭐⭐
void startBonusCelebration(Player* player) {
Serial.print("🎉🎉🎉 BONUS KAZANILDI! Skor: ");
Serial.print(player->score);
Serial.println(" 🎉🎉🎉");
*(player->bonusActive) = true;
*(player->bonusStartTime) = millis();
for(int i = 0; i < MAX_FIREWORKS; i++)
player->fireworks[i].active = false;
// YOU WIN EKRANI
player->tft->fillScreen(ILI9341_BLACK);
drawYouWin(player->tft, player->score);
// ⭐⭐⭐ VICTORY MÜZİĞİ ÇAL ⭐⭐⭐
playVictorySound();
Serial.println("🎵 VICTORY müziği çalınıyor - YOU WIN anında!");
}
void updateBonusCelebration(Player* player, unsigned long currentTime) {
drawYouWin(player->tft, player->score);
if(random(100) < 40) {
createFirework(player->tft, player->fireworks);
}
drawFireworks(player->tft, player->fireworks);
if(currentTime - *(player->bonusStartTime) >= BONUS_DURATION) {
*(player->bonusActive) = false;
stopSound();
endPlayerGame(player, "YOU WIN!");
Serial.println("🎊 Bonus kutlaması tamamlandı");
}
}
void createFirework(Adafruit_ILI9341* tft, Firework* fireworks) {
for(int i = 0; i < MAX_FIREWORKS; i++) {
if(!fireworks[i].active) {
fireworks[i].x = random(50, 270);
fireworks[i].y = random(50, 190);
fireworks[i].radius = 5;
fireworks[i].color = tft->color565(random(255), random(255), random(255));
fireworks[i].active = true;
fireworks[i].startTime = millis();
break;
}
}
}
void drawFireworks(Adafruit_ILI9341* tft, Firework* fireworks) {
unsigned long currentTime = millis();
for(int i = 0; i < MAX_FIREWORKS; i++) {
if(fireworks[i].active) {
int growth = (currentTime - fireworks[i].startTime) / 50;
int currentRadius = fireworks[i].radius + growth;
if(currentRadius > 30)
fireworks[i].active = false;
else {
tft->fillCircle(fireworks[i].x, fireworks[i].y, currentRadius, fireworks[i].color);
for(int j = 0; j < 8; j++) {
float angle = j * 45 * 3.14159 / 180;
int x2 = fireworks[i].x + cos(angle) * (currentRadius + 10);
int y2 = fireworks[i].y + sin(angle) * (currentRadius + 10);
tft->drawLine(fireworks[i].x, fireworks[i].y, x2, y2, fireworks[i].color);
}
}
}
}
}
void drawGameOverEffect(Player* player, unsigned long currentTime) {
if (currentTime - *(player->lastBlinkTimePtr) >= 500) {
*(player->blinkStatePtr) = !*(player->blinkStatePtr);
*(player->lastBlinkTimePtr) = currentTime;
if (*(player->blinkStatePtr)) {
player->tft->fillScreen(ILI9341_RED);
} else {
player->tft->fillScreen(ILI9341_BLACK);
}
drawGameOverContent(player->tft, player->score);
}
}
void drawGameOverContent(Adafruit_ILI9341* tft, int score) {
tft->setTextColor(ILI9341_WHITE);
tft->setTextSize(4);
String scoreText = "SKOR: " + String(score);
int scoreWidth = scoreText.length() * 24;
int scoreX = (320 - scoreWidth) / 2;
tft->setCursor(scoreX, 80);
tft->print(scoreText);
tft->setTextColor(ILI9341_YELLOW);
tft->setTextSize(3);
String messageText = "TEKRAR DENEYINIZ";
int messageWidth = messageText.length() * 18;
int messageX = (320 - messageWidth) / 2;
tft->setCursor(messageX, 160);
tft->print(messageText);
}
void drawYouWin(Adafruit_ILI9341* tft, int score) {
tft->fillScreen(ILI9341_BLACK);
tft->setTextColor(ILI9341_YELLOW);
tft->setTextSize(5);
String winText = "YOU WIN!";
int winTextWidth = winText.length() * 30;
int winX = (320 - winTextWidth) / 2;
int winY = 80;
tft->setCursor(winX, winY);
tft->print(winText);
tft->setTextColor(ILI9341_GREEN);
tft->setTextSize(4);
String scoreText = "SKOR: " + String(score);
int scoreTextWidth = scoreText.length() * 24;
int scoreX = (320 - scoreTextWidth) / 2;
int scoreY = 160;
tft->setCursor(scoreX, scoreY);
tft->print(scoreText);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4