#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h> // Biblioteka do zapisu najlepszego wyniku
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Definicje pinów akcesoriów:
#define BUZZER_PIN 8
#define LED_PIN 2 // Definicja pinu dla diody LED
// Przypisanie pinów dla 4 przycisków (wewnętrzny PULLUP -> stan LOW po wciśnięciu):
const int pinLeft = 4; // Przycisk w lewo
const int pinRight = 5; // Przycisk w prawo
const int pinDown = 6; // Przycisk w dół (przyspieszenie)
const int pinRotate = 7; // Przycisk obrotu
// NOWY PRZYCISK (pauza)
const int pinPauza = 9;
// ZMIENNE GLOBALNE PAUZY I KONCA GRY:
bool czyPauza = false;
bool poprzedniStanPauzy = HIGH;
bool czyKoniecGry = false; // FLAGA: Czy gra się skończyła?
#define MARGIN_LEFT 40
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 20
#define BLOCK_SIZE 3
bool plansza[BOARD_WIDTH][BOARD_HEIGHT] = {false};
const byte klocki[7][4][4] = {
{{1,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}}, // I
{{1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0}}, // L
{{1,1,1,0}, {0,0,1,0}, {0,0,0,0}, {0,0,0,0}}, // J
{{1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // O
{{0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // S
{{1,1,1,0}, {0,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // T
{{1,1,0,0}, {0,1,1,0}, {0,0,0,0}, {0,0,0,0}} // Z
};
// ==========================================
// <<< TUTAJ 1: Lista z nutami do dżinglu startowego
// ==========================================
const int intro_tetris[][2] = {
{659, 250}, // E5
{494, 125}, // B4
{523, 125}, // C5
{587, 250}, // D5
{523, 125}, // C5
{494, 125}, // B4
{440, 250} // A4
};
int aktualnyKlocek, aktualnyX, aktualnyY, rotacja;
int nastepnyKlocek;
int punkty = 0;
int najlepszyWynik = 0;
int linie = 0;
int poziom = 1;
unsigned long czasOpadania = 0;
unsigned long interwal = 500;
unsigned long ostatniRuch = 0;
bool przyciskObrotuPuszczony = true;
bool pobierzKlocek(int k, int r, int x, int y) {
int nx = x, ny = y;
if (r == 1) { nx = y; ny = 3 - x; }
else if (r == 2) { nx = 3 - x; ny = 3 - y; }
else if (r == 3) { nx = 3 - y; ny = x; }
return klocki[k][ny][nx];
}
void nowyKlocek() {
aktualnyKlocek = nastepnyKlocek;
nastepnyKlocek = random(0, 7);
aktualnyX = BOARD_WIDTH / 2 - 2;
aktualnyY = 0;
rotacja = 0;
if (kolizja(aktualnyX, aktualnyY, rotacja)) {
if (punkty > najlepszyWynik) {
najlepszyWynik = punkty;
EEPROM.put(0, najlepszyWynik);
}
czyKoniecGry = true;
// <--- SEKWENCJA GAME OVER --->
tone(BUZZER_PIN, 800, 150);
delay(160);
tone(BUZZER_PIN, 600, 150);
delay(160);
tone(BUZZER_PIN, 400, 300);
}
}
void resetujGre() {
punkty = 0;
linie = 0;
poziom = 1;
interwal = 500;
memset(plansza, 0, sizeof(plansza));
czyKoniecGry = false;
aktualnyKlocek = random(0, 7);
nastepnyKlocek = random(0, 7);
aktualnyX = BOARD_WIDTH / 2 - 2;
aktualnyY = 0;
}
bool kolizja(int nx, int ny, int nr) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (pobierzKlocek(aktualnyKlocek, nr, x, y)) {
int px = nx + x;
int py = ny + y;
if (px < 0 || px >= BOARD_WIDTH || py >= BOARD_HEIGHT) return true;
if (py >= 0 && plansza[px][py]) return true;
}
}
}
return false;
}
void zamrozKlocek() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (pobierzKlocek(aktualnyKlocek, rotacja, x, y)) {
if (aktualnyY + y >= 0) {
plansza[aktualnyX + x][aktualnyY + y] = true;
}
}
}
}
sprawdzLinie();
if (!czyKoniecGry) {
nowyKlocek();
}
}
void sprawdzLinie() {
for (int y = BOARD_HEIGHT - 1; y >= 0; y--) {
bool pelna = true;
for (int x = 0; x < BOARD_WIDTH; x++) {
if (!plansza[x][y]) { pelna = false; break; }
}
if (pelna) {
punkty += 10;
linie++;
if (linie >= poziom * 10) {
poziom++;
if (interwal > 100) interwal -= 40;
}
// Radosna sekwencja
tone(BUZZER_PIN, 1000, 80);
delay(90);
tone(BUZZER_PIN, 1300, 80);
delay(90);
tone(BUZZER_PIN, 1800, 150);
digitalWrite(LED_PIN, HIGH);
delay(150);
digitalWrite(LED_PIN, LOW);
for (int ty = y; ty > 0; ty--) {
for (int tx = 0; tx < BOARD_WIDTH; tx++) {
plansza[tx][ty] = plansza[tx][ty-1];
}
}
for (int tx = 0; tx < BOARD_WIDTH; tx++) plansza[tx][0] = false;
y++;
}
}
}
// ==========================================
// <<< TUTAJ 2: Funkcja odtwarzająca melodię
// ==========================================
void play_intro() {
for (int i = 0; i < 7; i++) {
tone(BUZZER_PIN, intro_tetris[i][0], intro_tetris[i][1]);
delay(intro_tetris[i][1] + 50); // Czas trwania nuty + krótki odstęp
}
noTone(BUZZER_PIN);
}
void setup() {
randomSeed(analogRead(A3));
pinMode(pinLeft, INPUT_PULLUP);
pinMode(pinRight, INPUT_PULLUP);
pinMode(pinDown, INPUT_PULLUP);
pinMode(pinRotate, INPUT_PULLUP);
pinMode(pinPauza, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while(1); }
EEPROM.get(0, najlepszyWynik);
if (najlepszyWynik < 0 || najlepszyWynik > 30000) {
najlepszyWynik = 0;
}
display.clearDisplay();
aktualnyKlocek = random(0, 7);
nastepnyKlocek = random(0, 7);
aktualnyX = BOARD_WIDTH / 2 - 2;
aktualnyY = 0;
// ==========================================
// <<< TUTAJ 3: Wywołanie dżingla na sam koniec setupu
// ==========================================
play_intro();
}
void loop() {
// === OBSŁUGA EKRANU KONCA GRY (GAME OVER) ===
if (czyKoniecGry) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(35, 15);
display.print("GAME OVER");
display.setCursor(20, 30);
display.print("Wynik: ");
display.print(punkty);
if ((millis() / 500) % 2 == 0) {
display.setCursor(18, 48);
display.print("PRESS TO START");
}
display.display();
if (digitalRead(pinRotate) == LOW) {
resetujGre();
delay(300);
}
return;
}
// <--- OBSŁUGA PAUZY --->
bool aktualnyStanPauzy = digitalRead(pinPauza);
if (aktualnyStanPauzy == LOW && poprzedniStanPauzy == HIGH) {
czyPauza = !czyPauza;
delay(200);
}
poprzedniStanPauzy = aktualnyStanPauzy;
if (!czyPauza) {
bool stanLeft = digitalRead(pinLeft);
bool stanRight = digitalRead(pinRight);
bool stanDown = digitalRead(pinDown);
bool stanRotate = digitalRead(pinRotate);
if (millis() - ostatniRuch > 120) {
if (stanLeft == LOW) {
if (!kolizja(aktualnyX - 1, aktualnyY, rotacja)) aktualnyX--;
tone(BUZZER_PIN, 1000, 50);
ostatniRuch = millis();
}
else if (stanRight == LOW) {
if (!kolizja(aktualnyX + 1, aktualnyY, rotacja)) aktualnyX++;
tone(BUZZER_PIN, 1000, 50);
ostatniRuch = millis();
}
}
unsigned long aktualnyInterwal = interwal;
if (stanDown == LOW) {
aktualnyInterwal = 50;
}
if (stanRotate == LOW) {
if (przyciskObrotuPuszczony) {
int nastepnaRotacja = (rotacja + 1) % 4;
if (!kolizja(aktualnyX, aktualnyY, nastepnaRotacja)) {
rotacja = nastepnaRotacja;
}
przyciskObrotuPuszczony = false;
}
} else {
przyciskObrotuPuszczony = true;
}
if (millis() - czasOpadania > aktualnyInterwal) {
if (!kolizja(aktualnyX, aktualnyY + 1, rotacja)) {
aktualnyY++;
} else {
zamrozKlocek();
}
czasOpadania = millis();
}
}
// RENDEROWANIE GRAFIKI
display.clearDisplay();
display.drawRect(MARGIN_LEFT - 1, 0, BOARD_WIDTH * BLOCK_SIZE + 2, BOARD_HEIGHT * BLOCK_SIZE + 2, SSD1306_WHITE);
for (int x = 0; x < BOARD_WIDTH; x++) {
for (int y = 0; y < BOARD_HEIGHT; y++) {
if (plansza[x][y]) {
display.fillRect(MARGIN_LEFT + x * BLOCK_SIZE, y * BLOCK_SIZE + 1, BLOCK_SIZE - 1, BLOCK_SIZE - 1, SSD1306_WHITE);
}
}
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (pobierzKlocek(aktualnyKlocek, rotacja, x, y)) {
display.fillRect(MARGIN_LEFT + (aktualnyX + x) * BLOCK_SIZE, (aktualnyY + y) * BLOCK_SIZE + 1, BLOCK_SIZE - 1, BLOCK_SIZE - 1, SSD1306_WHITE);
}
}
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("PTS:");
display.setCursor(0, 10);
display.print(punkty);
display.setCursor(0, 20);
display.print("HI:");
display.setCursor(0, 30);
display.print(najlepszyWynik);
display.setCursor(0, 40);
display.print("NEXT:");
display.setCursor(0, 52);
display.print("L:");
display.print(poziom);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (klocki[nastepnyKlocek][y][x]) {
display.fillRect(28 + x * 2, 50 + y * 2, 2, 2, SSD1306_WHITE);
}
}
}
if (czyPauza) {
display.setCursor(50, 28);
display.print("PAUSE");
}
display.display();
delay(20);
}Loading
ssd1306
ssd1306