1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #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: