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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | /* GND (1) — GND VCC (2) — 5V RESET (3) — D9 D/C (4) — D8 CARD-CS (5) - TFT-CS (6) — D10 MOSI (7) — D11 SCK (8) — D13 MISO (9) - LITE (10) — 5V */ #include “Adafruit_GFX.h” #include “Adafruit_ST7735.h” #include “SPI.h” #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 BLAU = 15; … */ #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 GELB // Farbe der Schrift #define SCHRIFTFARBE WEISS // Joystick analoge Pins int XAchse = A0; int YAchse = A1; // Button/Knopf int JoystickButton = 7; // Zustand des Buttons int ButtonLesen; // Spiel starten wenn * gedrückt wurde bool SpielStart = false; // Radius des kreises const int Radius = 10; // Abstand zu den Rändern 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; // Variablen für die Auswertung der Bewegung des Joysticks int PositionX; int PositionY; // Variable für die Zeitmessung long Start; void setup() { // Startbildschirm // schwarzes Farbschema vertikale Ausrichtung (nicht drehen) // Cursor setzen, Schriftgröße und ‑farbe definieren tft.initR(INITR_BLACKTAB); tft.setRotation(0); tft.fillScreen(SCHWARZ); // Startbildschirm tft.setTextSize(2); tft.setCursor(1, 10); tft.setTextColor(SCHRIFTFARBE); tft.println(“Start:”); tft.print(“-> Button”); pinMode(JoystickButton, INPUT_PULLUP); } void loop() { // Button/Knopf auswerten ButtonLesen = digitalRead(JoystickButton); if (ButtonLesen == LOW) { // Spiel wird gestartet SpielStart = true; // Parcours bauen ParcoursBauen(); // Zeitmessung starten Start = millis(); } // wenn der Button gedrückt wurde -> SpielStart -> true if (SpielStart) { // Bewegung der X‑Achse lesen PositionX = analogRead(XAchse); // Bewegung X‑Achse nach oben if (PositionX > 600) { // Kreis an der aktuellen Position “löschen” tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ); // wenn der Bildschirmrand oben noch nicht erreicht wurde // rückwärts -> Richtung x = 1 bewegen if (CursorY > Radius) CursorY -= Bewegung; // Kreis an der neuen Position zeichnen tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE); delay(Geschwindigkeit); } // Bewegung X‑Achse nach unten if (PositionX < 300) { // Kreis an der aktuellen Position “löschen” tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ); // wenn der Bildschirmrand rechts noch nicht erreicht wurde // vorwärts -> Richtung tft.height() bewegen if (CursorY < tft.height() - Radius) CursorY += Bewegung; // Kreis an der neuen Position zeichnen tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE); delay(Geschwindigkeit); } // Bewegung Y‑Achse nach links if (PositionY < 300) { // Kreis an der aktuellen Position “löschen” tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ); // wenn der Bildschirmrand links noch nicht erreicht wurde // rückwärts -> Richtung linken Bildschirmrand bewegen if (CursorX > Radius) CursorX -= Bewegung; // Kreis an der neuen Position zeichnen tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE); delay(Geschwindigkeit); } // Bewegung Y‑Achse nach rechts PositionY = analogRead(YAchse); if (PositionY > 600) { // Kreis an der aktuellen Position “löschen” tft.fillCircle(CursorX, CursorY, Radius, SCHWARZ); // Abfrage, ob der rechte Rand erreicht wurde, nicht nötig // wird in der nächsten Bedingung abgefragt CursorX += Bewegung; // Kreis an der neuen Position zeichnen tft.fillCircle(CursorX, CursorY, Radius, KREISFARBE); delay(Geschwindigkeit); } // rechter Bildschirmrand erreicht -> Spielende if (CursorX > tft.height() - Radius) { ErgebnisZeigen(); } } } void ErgebnisZeigen() { // Zeit berechnen int Sekunden; long VerstricheneZeit = millis() - Start; Sekunden = int(VerstricheneZeit / 1000); // Zeit anzeigen tft.fillScreen(SCHWARZ); tft.setTextColor(SCHRIFTFARBE); tft.setTextSize(2); tft.setCursor(1, 10); tft.println(“Zeit:”); tft.println(String(Sekunden) + ” s”); tft.setCursor(1, 40); tft.println(); tft.println(“Neustart:”); tft.println(“-> Taste”); 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: