Laby­rinth-Spiel TFT Fernbedienung

#include "IRremote.hpp"
int EmpfaengerPin = 11;

#include "Adafruit_GFX.h"
#include "Adafruit_ST7735.h"

/*
  Arduino
  SCK  13
  RST   9
  DC    8
  CS   10
  COPI 11
*/
#define TFT_CS        10
#define TFT_RST        9
#define TFT_DC         8

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

/*
  Farben als hexadezimal definiert
  alternativ:
  int SCHWARZ = 0;
  int FARBE = 15;
  . . .
*/
// Farben
#define SCHWARZ 0x0000     // dezimal 0
#define BLAU 0x000F        // dezimal 15
#define ROT 0xF800         // dezimal 63488
#define GRUEN 0x0E81       // dezimal 3713
#define CYAN 0x07FF        // dezimal 2047
#define MAGENTA 0xF81F     // dezimal 63519
#define GELB 0xAFE5        // dezimal 65504
#define WEISS 0xFFFF       // dezimal 65535
#define BRAUN 0xFC00       // dezimal 64512
#define GRAU 0xF7F0        // dezimal 63472
#define GRUENGELB 0xAFE5   // dezimal 45029
#define DUNKELCYAN 0x03EF  // dezimal 1007
#define ORANGE 0xFD20      // dezimal 64800
#define PINK 0xFC18        // dezimal 64536

// Farbe der Blöcke
#define FARBE GRUEN

// Farbe des Kreises
#define KREISFARBE ROT

// Farbe der Schrift
#define SCHRIFTFARBE WEISS

// Spiel starten wenn * gedrückt wurde
bool SpielStart = false;

const int Radius = 10;
const int Abstand = Radius * 2;

// je höher, dest langsamer
const int Geschwindigkeit = 100;

// Bewegung des Kreises in Pixeln
const int Bewegung = 5;

// Startposition des Kreises
int CursorX = Radius;
int CursorY = tft.height() / 2 - Abstand;

// Variable für die Zeitmessung
long Start;

void setup() 
{
  // Startbildschirm
  // schwarzes Farbschema horizontale Ausrichtung
  // Cursor setzen, Schriftgröße und -farbe definieren
  tft.initR(INITR_BLACKTAB);
  tft.setRotation(0);
  tft.fillScreen(SCHWARZ);
  tft.setTextSize(2);
  tft.setCursor(1, 10);
  tft.setTextColor(SCHRIFTFARBE);

  tft.println("Start:");
  tft.print("-> *");
  Serial.begin(9600);
  IrReceiver.begin(EmpfaengerPin);
}

void loop() 
{
  // nur ausführen, wenn SpielStart true
  if (IrReceiver.decode() && !SpielStart) 
  {
    delay(200);

    // nächsten Wert lesen
    IrReceiver.resume();

    // Start mit *
    if (IrReceiver.decodedIRData.command == 66) 
    {
      // Spiel wird gestartet
      SpielStart = true;

      // Parcours bauen
      ParcoursBauen();

      // Zeitmessung starten
      Start = millis();
    }
  }

  // wenn die Taste * gedrückt wurde
  if (SpielStart) 
  {
    if (IrReceiver.decode()) 
    {
      // nächsten Wert lesen
      IrReceiver.resume();

      // OK -> Bewegung anhalten
      if (IrReceiver.decodedIRData.command == 64) 
      {
        // Kreis an der aktuellen Position anhalten
        tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);
      }

      // nach oben
      if (IrReceiver.decodedIRData.command == 0x46) 
      {
        // Kreis an der aktuellen Position "löschen"
        tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ);

        // wenn der Bildschirmrand noch nicht erreicht wurde
        // rückwärts (Richtung x = 1) bewegen
        if (CursorY > Radius) CursorY -= Bewegung;
        tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);
        delay(Geschwindigkeit);
      }

      // nach unten
      if (IrReceiver.decodedIRData.command == 21) 
      {
        tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ);
        if (CursorY < tft.height() - Radius) CursorY += Bewegung;
        tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);
        delay(Geschwindigkeit);
      }

      // nach links
      if (IrReceiver.decodedIRData.command == 68) 
      {
        tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ);
        if (CursorX > Radius) CursorX -= Bewegung;
        tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);
        delay(Geschwindigkeit);
      }

      // nach rechts
      if (IrReceiver.decodedIRData.command == 67) 
      {
        tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ);
        CursorX += Bewegung;
        tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);
        delay(Geschwindigkeit);
      }
    }

    // rechter Bildschirmrand erreicht -> Spielende
    if (CursorX > tft.height() - Radius * 2) 
    {
      ErgebnisZeigen();
    }
  }
}

void ErgebnisZeigen() {
  // Zeit berechnen
  int Sekunden;
  long VerstricheneZeit = millis() - Start;
  Sekunden = int(VerstricheneZeit / 1000);

  // Zeit anzeigen
  tft.fillScreen(SCHWARZ);
  tft.setTextSize(2);
  tft.setCursor(1, 10);
  tft.println("Zeit:");
  tft.println(String(Sekunden) + " s");

  tft.setCursor(1, 40);
  tft.setTextColor(ROT);
  tft.println();
  tft.println("Neustart:");
  tft.println("-> *");
  SpielStart = false;
}

void ParcoursBauen() 
{
  CursorX = Radius;
  CursorY = tft.height() / 2 - Abstand;
  tft.fillScreen(SCHWARZ);

  // Kreis anzeigen
  tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE);

  // Parcours "bauen"
  tft.fillRect(65, 35, 5, 45, FARBE);
  tft.fillRect(1, 1, 35, 35, FARBE);
  tft.fillRect(1, 80, 70, 80, FARBE);
  tft.fillRect(110, 1, 70, 95, FARBE);
  tft.fillRect(110, 130, 140, 160, FARBE);
}

Letzte Aktualisierung: Jan. 30, 2025 @ 8:50