Arduino ile Tetris Oyunu ama Butonsuz Kontrol

Bu projede basit bir tetris oyununu daha eğlenceli hale getiren bir yenilik sağlamaktadır. Ben bu projeyi Arduino Nano ile yaptım. APDS-9960 sensörü ile birlikte tetrisin kontrolü ile temassız bir şekilde kontrol edilmektedir. Küçük bir delikli tahta üzerine monte ederek daha kolay kullanım kolaylığı sağladım. APDS-9960 sensörü maalesef ilk güç verildiğinde belli bir süre tepki almamaktadır. Ancak ilk tepkiyi aldıktan sonra herhangi bir gecikme olmadan kontrol edebilirsiniz. Açılış ekranı geldiğinde herhangi bir yöne hareket ederek oyunun başlatabilirsiniz.

Malzemeler:

Arduino Uno veya Nano

ST7735 1.8” TFT LCD Ekran

Breadboard

Jumper Kablo

Bağlantı Planı:

ARDUİNO TFT Ekran
13 SCK**
11 SDA** (MOSI)
8 RES**
9 DC**
7 CS**
3.3V VCC
GND GND
3.3V BLK (araya 100 Ohm direnç ekleyin)

**Araya 1000-220 ohm aralığında mutlaka Direnç ekleyin

ARDUİNO APDS-9960
3.3V 3.3V
GND GND
A4 SDA
A5 SCL

Kütüphaneler (Drive): https://drive.google.com/file/d/1Dlqlj1oHn2hxEVgkSHqfZmm-9EgQbEt2/view?usp=drive_link

Kodlar:

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_APDS9960.h>
#include <SPI.h>
#include <Wire.h>

#define TFT_CS 7
#define TFT_DC 9
#define TFT_RST 8

Adafruit_ST7735 lcd = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

Adafruit_APDS9960 apds;

byte yon = 5;

byte mod = 4;

int tetPuan;

bool tetrisZor = 0;

byte tetPuanX;
byte tetPuanY;
byte tetPuanSira;
bool tetPuanDurum;

byte tetNesYE;

byte tetNesX = 6;
byte tetNesY = 1;

byte tetrisXKonum[4] = {};
byte tetrisYKonum[4] = {};

byte tetrisYon = 0;
byte tetrisTip;

byte tetrisPanelKonumX;
byte tetrisPanelKonumY;
byte tetBoy = 8;
#define tetrisPanelXBoyutu 12
#define tetrisPanelYBoyutu 20
bool tetrisPanel[tetrisPanelXBoyutu][tetrisPanelYBoyutu] = {};

byte tetrisTick;

byte tetrisSira;

byte tetrisDur = 0;

byte a;
byte b;

byte tetSiraAdet;

bool tetAIDurum = 0;

byte tetTabanAdet;

int seed;

#define blokRenk 0xcc6a
#define yerlesikRenk 0x8b55  //RGB565 Renk Kodlaması. Buradan Renkleri değiştirebilirsiniz.
#define arkaRenk 0x4a6a

void setup() {
  lcd.initR(INITR_BLACKTAB);
  lcd.setRotation(2);
  lcd.setTextSize(2);
  lcd.setTextColor(0x5555, ST77XX_BLACK);
  lcd.setTextWrap(true);
  if (!apds.begin()) {
    lcd.fillScreen(0x04b3);
    lcd.setCursor(20, 10);
    lcd.print("Sorun Var");
    delay(2000);
  } else {
  }
  apds.enableProximity(true);
  apds.enableGesture(true);
  seed = analogRead(A0);
  randomSeed(seed);
  delay(100);
  tetrisTip = random(1, 6);
  tetrisSira = random(0, 7);
  lcd.fillScreen(0x04b3);
}
void loop() {
  uint8_t gesture = apds.readGesture();
  if (gesture == APDS9960_UP) {
    yon = 1;
  }
  if (gesture == APDS9960_RIGHT) {
    yon = 2;
  }
  if (gesture == APDS9960_DOWN) {
    yon = 3;
  }
  if (gesture == APDS9960_LEFT) {
    yon = 4;
  }
  if (mod == 0) {
    if (yon != 5) {
      lcd.fillScreen(ST77XX_BLACK);
      mod = 1;
      yon = 5;
    }
  } else if (mod == 1) {
    tetrisOyunu();
  } else if (mod == 2) {
    lcd.fillScreen(0xe202);
    delay(100);
    lcd.setTextSize(2);
    lcd.setTextColor(0xc006, 0xe202);
    lcd.setCursor(20, 40);
    lcd.print("YANDINIZ");
    lcd.setTextColor(0xffff, 0xe202);
    lcd.setCursor(60, 70);
    lcd.print(tetPuan);
    lcd.setTextSize(2);
    mod = 3;
  } else if (mod == 3) {
    if (yon != 5) {
      lcd.fillScreen(0x04b3);
      tetPuan = 0;
      mod = 4;
      yon = 5;
    }
  } else if (mod == 4) {
    lcd.setTextSize(2);
    lcd.setTextColor(0xFFFF, 0x04b3);
    lcd.setCursor(0, 10);
    lcd.print("  TETRIS      ama     Arduino     ile");
    lcd.setTextColor(0x5555, ST77XX_BLACK);
    mod = 0;
  }
}
void tetrisOyunu() {
  if (tetrisDur == 0) {  //tetrisin asagi hareketi
    tetrisTick++;
  }
  if (tetrisTick > 6) {
    tetrisTick = 0;
    tetNesY++;
    panelRenk();
  }
  for (a = 0; a <= tetrisPanelYBoyutu; a++) {  //bloklarin ekrana yazilmasi
    for (b = 1; b < tetrisPanelXBoyutu - 1; b++) {
      if (tetrisPanel[b][a] == 1) {
        lcd.fillRect(b * tetBoy, a * tetBoy, tetBoy, tetBoy, yerlesikRenk);
      } else {
      }
    }
  }
  if (tetrisPanel[tetrisXKonum[0]][tetrisYKonum[0] + 1] == 1 || tetrisPanel[tetrisXKonum[1]][tetrisYKonum[1] + 1] == 1 || tetrisPanel[tetrisXKonum[2]][tetrisYKonum[2] + 1] == 1 || tetrisPanel[tetrisXKonum[3]][tetrisYKonum[3] + 1] == 1) {
    tetrisYerlestirme();
  } else if (tetrisYKonum[0] >= tetrisPanelYBoyutu - 1 || tetrisYKonum[1] >= tetrisPanelYBoyutu - 1 || tetrisYKonum[2] >= tetrisPanelYBoyutu - 1 || tetrisYKonum[3] >= tetrisPanelYBoyutu - 1) {
    tetrisYerlestirme();
  }
  tetrisNesne(tetrisPanelXBoyutu + 1, 7, 0, tetrisSira, 0);
  tetrisNesne(tetNesX, tetNesY, tetrisYon, tetrisTip, 1);
  lcd.setCursor(100, 0);
  lcd.print(tetPuan);
  lcd.setCursor(100, 20);
  lcd.print(yon);
  delay(15);
  if (tetrisTip == 0 || tetrisTip == 1 || tetrisTip == 2) {  //bloklarin yatay hareketi ve disari cikmamasini saglayan kisim| baslangic
    if (tetrisYon == 0 || tetrisYon == 2) {
      if (yon == 4 && tetNesX - 2 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 2 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    } else if (tetrisYon == 1) {
      if (yon == 4 && tetNesX - 1 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 2 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    } else if (tetrisYon == 3) {
      if (yon == 4 && tetNesX - 2 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 1 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    }
  } else if (tetrisTip == 3 || tetrisTip == 4) {
    if (tetrisYon == 0) {
      if (yon == 4 && tetNesX - 2 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 2 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    } else if (tetrisYon == 1) {
      if (yon == 4 && tetNesX - 2 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 1 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    }
  } else if (tetrisTip == 5) {
    if (tetrisYon == 0) {
      if (yon == 4 && tetNesX - 2 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 3 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    } else if (tetrisYon == 1) {
      if (yon == 4 && tetNesX - 1 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 1 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    }
  } else if (tetrisTip == 6) {
    if (tetrisYon == 0) {
      if (yon == 4 && tetNesX - 1 != 0) {
        tetNesX -= 1;
        delay(50);
        yon = 5;
      }
      if (yon == 2 && tetNesX + 2 != tetrisPanelXBoyutu - 1) {
        tetNesX += 1;
        delay(50);
        yon = 5;
      }
    }
  }
  if (yon == 1 || yon == 3) {  //blok dondurme islemi
    tetrisYon++;
    delay(100);
    yon = 5;
  }
  if (yon != 5) {
    panelRenk();
  }
  if (tetrisTip == 0 || tetrisTip == 1 || tetrisTip == 2) {  //blok yonu degeri sinirlandirma islemi
    if (tetrisYon > 3) {
      tetrisYon = 0;
    }
  } else if (tetrisTip == 3 || tetrisTip == 4 || tetrisTip == 5) {
    if (tetrisYon > 1) {
      tetrisYon = 0;
    }
  } else if (tetrisTip == 6) {
    if (tetrisYon > 0) {
      tetrisYon = 0;
    }
  }
  if (tetrisTip > 6) {
    tetrisTip = 0;
  }
}
void panelRenk() {
  lcd.fillRect(tetBoy, 0, (tetrisPanelXBoyutu * tetBoy) - (tetBoy * 2), tetrisPanelYBoyutu * tetBoy, arkaRenk);
  //lcd.fillRect((tetNesX - 2) * tetBoy, (tetNesY - 2) * tetBoy, 6 * tetBoy, 6 * tetBoy, 0x5550);
}

void tetrisTemizle(byte o) {  // bloklari temizleme
  for (byte m = 0; m < o; m++) {
    for (byte n = 0; n < tetrisPanelXBoyutu; n++) {
      tetrisPanel[n][m] = 0;
    }
  }
}
void tetrisYerlestirme() {  // bir blogu degiskendeki kisma yerlestirme ve yeni blogu olusturma
  //lcd.fillRect(tetBoy, 0, (tetrisPanelXBoyutu * tetBoy) - (tetBoy * 2), tetrisPanelYBoyutu * tetBoy, 0x5550);
  lcd.fillRect((tetrisPanelXBoyutu)*tetBoy, 6 * tetBoy, 4 * tetBoy, 3 * tetBoy, ST77XX_BLACK);
  if (tetNesY > 2) {
    tetrisPanel[tetrisXKonum[0]][tetrisYKonum[0]] = 1;
    tetrisPanel[tetrisXKonum[1]][tetrisYKonum[1]] = 1;
    tetrisPanel[tetrisXKonum[2]][tetrisYKonum[2]] = 1;
    tetrisPanel[tetrisXKonum[3]][tetrisYKonum[3]] = 1;
    tetrisTip = tetrisSira;
    delay(10);
    tetrisSira = random(0, 7);
    tetNesX = 6;
    tetNesY = 2;
    tetrisYon = 0;
    for (byte k = 0; k < tetrisPanelYBoyutu; k++) {
      tetPuanSira = 0;
      for (byte l = 1; l < tetrisPanelXBoyutu - 1; l++) {
        if (tetrisPanel[l][k] == 1) {
          tetPuanSira++;
        }
        if (tetPuanSira == tetrisPanelXBoyutu - 2) {
          tetPuanSira = 0;
          tetPuan++;
          tetPuanDurum = 1;
          tetPuanY = k;
          k = 1;
        }
        if (tetPuanDurum == 1) {
          for (byte y = tetPuanY; y > 2; y--) {
            for (byte x = 1; x < tetrisPanelXBoyutu - 1; x++) {
              tetrisPanel[x][y] = tetrisPanel[x][y - 1];
            }
          }
          tetPuanDurum = 0;
        }
      }
    }
  } else {
    delay(4000);
    mod = 2;
    tetrisTemizle(tetrisPanelYBoyutu);
  }
}
byte tetrisNesne(byte funcX, byte funcY, byte tetrisNesneYon, byte tetrisBlok, bool tetrisSinir) {  // tum bloklar burda
  tetrisXKonum[0] = funcX;
  tetrisYKonum[0] = funcY;
  if (tetrisBlok == 0) {  // T Blok
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 3;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0] + 1;
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 1;
    }
    if (tetrisNesneYon == 2) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0] + 1;
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0];
      tetTabanAdet = 1;
    }
    if (tetrisNesneYon == 3) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] - 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 1;
    }
  }
  if (tetrisBlok == 1) {  //L blok deger 1
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] + 1;
      tetrisXKonum[2] = tetrisXKonum[0] - 1;
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 1;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0] + 1;
      tetrisYKonum[1] = tetrisYKonum[0] - 1;
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 2;
    }
    if (tetrisNesneYon == 2) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0] + 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 3;
    }
    if (tetrisNesneYon == 3) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0] + 1;
      tetrisYKonum[2] = tetrisYKonum[0] - 1;
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 1;
    }
  }
  if (tetrisBlok == 2) {  // L blok deger 2
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] + 1;
      tetrisXKonum[2] = tetrisXKonum[0] - 1;
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 3;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0] + 1;
      tetrisYKonum[1] = tetrisYKonum[0] + 1;
      tetrisYKonum[2] = tetrisYKonum[0] - 1;
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 1;
    }
    if (tetrisNesneYon == 2) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0] + 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 1;
    }
    if (tetrisNesneYon == 3) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0] - 1;
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 2;
    }
  }
  if (tetrisBlok == 3) {  // S blok deger 1
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] + 1;
      tetrisXKonum[2] = tetrisXKonum[0] - 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 2;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0] - 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] - 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 1;
    }
  }
  if (tetrisBlok == 4) {  // S blok deger 2
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0] + 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 2;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0] - 1;
      tetrisXKonum[2] = tetrisXKonum[0] - 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] - 1;
      tetTabanAdet = 1;
    }
  }
  if (tetrisBlok == 5) {  // Uzun Blok
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] + 1;
      tetrisXKonum[2] = tetrisXKonum[0] + 2;
      tetrisXKonum[3] = tetrisXKonum[0] - 1;
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0];
      tetrisYKonum[3] = tetrisYKonum[0];
      tetTabanAdet = 4;
    }
    if (tetrisNesneYon == 1) {
      tetrisXKonum[1] = tetrisXKonum[0];
      tetrisXKonum[2] = tetrisXKonum[0];
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0] + 1;
      tetrisYKonum[2] = tetrisYKonum[0] - 1;
      tetrisYKonum[3] = tetrisYKonum[0] - 2;
      tetTabanAdet = 1;
    }
  }
  if (tetrisBlok == 6) {  //Kare Blog
    if (tetrisNesneYon == 0) {
      tetrisXKonum[1] = tetrisXKonum[0] + 1;
      tetrisXKonum[2] = tetrisXKonum[0] + 1;
      tetrisXKonum[3] = tetrisXKonum[0];
      tetrisYKonum[1] = tetrisYKonum[0];
      tetrisYKonum[2] = tetrisYKonum[0] + 1;
      tetrisYKonum[3] = tetrisYKonum[0] + 1;
      tetTabanAdet = 2;
    }
  }
  lcd.fillRect(tetrisXKonum[0] * tetBoy, tetrisYKonum[0] * tetBoy, tetBoy, tetBoy, blokRenk);
  lcd.fillRect(tetrisXKonum[1] * tetBoy, tetrisYKonum[1] * tetBoy, tetBoy, tetBoy, blokRenk);
  lcd.fillRect(tetrisXKonum[2] * tetBoy, tetrisYKonum[2] * tetBoy, tetBoy, tetBoy, blokRenk);
  lcd.fillRect(tetrisXKonum[3] * tetBoy, tetrisYKonum[3] * tetBoy, tetBoy, tetBoy, blokRenk);
}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir