Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Manufacturing Technology >> Herstellungsprozess

LCD Hill Run v2 – Interaktives Arduino-Laufspiel mit 16×2 LCD & Summer

Komponenten und Zubehör

Bauteil Menge Hinweise
Arduino Mega 2560 1 Alternativ Arduino UNO: Im Code DUCK_PIN auf 3 ändern und entsprechend umverdrahten
Steckbrücken (generic) ca. 20
SparkFun Taster 12 mm 2
Widerstand 10 kΩ 2 Als Pull-Down für die Taster
Adafruit Standard LCD 16×2 (weiß auf blau) 1
Drehpotentiometer 10 kΩ 1 Für LCD-Kontrast
Summer (passiv bevorzugt) 1 Nicht zwingend erforderlich, nur für Soundeffekte
Steckbrett (Full-Size empfohlen) 1

Software und Online-Dienste

Projektbeschreibung

Ich bin noch jung und größtenteils Arduino-Anfänger. Nach dem Studium einiger Project-Hub-Beiträge und dem Erlernen der Arduino-Grundlagen wurde ich inspiriert, dieses unterhaltsame Spiel zu entwickeln. Der Debugging-Aufwand hielt sich in Grenzen, und das Ergebnis ist richtig cool. Der Code ist nach meinen Maßstäben recht sauber. Viel Spaß damit! Bitte respektvoll kommentieren.

Wichtig: Die im Programm enthaltenen Bibliotheken herunterladen und in den Bibliotheksordner legen. Falls eine Fehlermeldung auftaucht, dass die Bibliothek nicht gefunden wird, einfach über Sketch → Bibliothek einbinden hinzufügen. Bei Problemen gerne kommentieren.

Bibliotheken: GitHub-Repository

Schaltplan

Der Fritzing-Schaltplan (hillrun2_zp0SI4wns3.fzz) zeigt die Verdrahtung. Für Arduino UNO: Im Code const int DUCK_PIN = 18 auf 3 ändern und den Duck-Taster von Pin 18 auf Digitalpin 3 umstecken.

Code (Arduino)

Der Code nutzt Interrupts für reaktionsschnelle Steuerung. Hinweis: DUCK_PIN ist standardmäßig 18 (Mega), für UNO auf 3 ändern.

/* Hill Run v2 – Verbesserte Version mit Interrupts
 * Copyright (c) 2020 by Miles C.
 * Spielprinzip: Über Hügel springen (Jump-Taster), unter Krähen ducken (Duck-Taster).
 * Verkabelung siehe Kommentare im Code.
 */
#include <LiquidCrystal.h>
#include "pitches.h"

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int JUMP_PIN = 2;
const int BUZZER_PIN = 5;
const int DUCK_PIN = 18; // Für UNO auf 3 ändern
const int JUMP_PITCH = 2700;
const int JUMP_PITCH_DURATION = 50;
const int DUCK_PITCH = 1350;
const int DUCK_PITCH_DURATION = 50;
const int DIE_PITCH = 200;
const int DIE_PITCH_DURATION = 500;
const int TICKSPEED = 90; // ms pro Gametick
const int JUMP_LENGTH = 3; // übersprungene Zeichen

// Benutzerdefinierte Zeichen (Stickman, Hügel, Krähen)
const byte stickStep1[8] = { B01110, B01110, B00101, B11111, B10100, B00110, B11001, B00001 };
const byte stickStep2[8] = { B01110, B01110, B00101, B11111, B10100, B00110, B01011, B01000 };
const byte stickJump[8]  = { B01110, B01110, B00100, B11111, B00100, B11111, B10001, B00000 };
const byte stickDuck[8]  = { B00000, B00000, B00000, B01110, B01110, B11111, B00100, B11111 };
const byte hill[8]       = { B00000, B00100, B01010, B01110, B11101, B10101, B11001, B11111 };
const byte crow1[8]      = { B00111, B00100, B00110, B01111, B11111, B01111, B00110, B00111 };
const byte crow2[8]      = { B00111, B00110, B01111, B11111, B01111, B00110, B00110, B00111 };

volatile int jumpPhase = JUMP_LENGTH + 1;
int gameTick = 0;
int crowX = 40;
int hillX = 25;
bool playerY = 0;
volatile bool ducking = LOW;
bool loopBreaker = 1;
bool crowGo = 0;
int score = 0;

void setup() {
  pinMode(JUMP_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  lcd.begin(16, 2);
  lcd.createChar(0, hill);
  lcd.createChar(1, stickStep1);
  lcd.createChar(2, stickStep2);
  lcd.createChar(3, stickJump);
  lcd.createChar(4, stickDuck);
  lcd.createChar(5, crow1);
  lcd.createChar(6, crow2);
  attachInterrupt(digitalPinToInterrupt(JUMP_PIN), seeJumping, RISING);
  attachInterrupt(digitalPinToInterrupt(DUCK_PIN), seeDucking, CHANGE);
}

void loop() {
  playerY = 0;
  if (jumpPhase < JUMP_LENGTH) playerY = 1;
  drawSprites();
  loopBreaker = 1;

  // Hügel-Logik
  if (hillX < 16) {
    if (crowX < hillX) { hillX += 8; loopBreaker = 0; }
    if (loopBreaker) { lcd.setCursor(hillX, 1); lcd.write((byte)0); }
  }
  if (hillX < 1) {
    if (jumpPhase < JUMP_LENGTH) { score++; hillX = 16 + random(8); }
    else endGame();
  }

  // Krähen-Logik
  if (crowX < 16) {
    lcd.setCursor(crowX, 0);
    lcd.write((byte)(gameTick % 8 < 4 ? 5 : 6));
  }
  if (crowX < 1) {
    if (ducking) { score++; crowX = 24 + random(16); }
    else endGame();
  }

  lcd.setCursor(0, playerY); lcd.print(" ");
  jumpPhase++;
  hillX--;
  crowGo = !crowGo;
  crowX -= crowGo;
  gameTick++;
  delay(TICKSPEED);
}

void endGame() {
  lcd.clear();
  lcd.setCursor(0, 0); lcd.print("Score: ");
  lcd.setCursor(7, 0); lcd.print(score);
  tone(BUZZER_PIN, DIE_PITCH, DIE_PITCH_DURATION);
  while (!digitalRead(JUMP_PIN)) {
    lcd.setCursor(0, 1);
    if (millis() % 500 < 250) lcd.print("Jump to Continue");
    else lcd.print("                ");
  }
  lcd.clear();
  score = 0; hillX = 25; crowX = 40;
}

void drawSprites() {
  lcd.setCursor(0, 1 - playerY);
  if (!ducking) {
    if (!playerY) lcd.write((byte)(gameTick % 4 < 2 ? 1 : 2));
    else lcd.write((byte)3);
  } else lcd.write((byte)4);
  lcd.setCursor(1, 1); lcd.print(" ");
  lcd.setCursor(1, 0); lcd.print(" ");
}

void seeJumping() {
  if (jumpPhase > (JUMP_LENGTH + 2) && !ducking) {
    jumpPhase = 0;
    tone(BUZZER_PIN, JUMP_PITCH, JUMP_PITCH_DURATION);
  }
}

void seeDucking() {
  ducking = digitalRead(DUCK_PIN);
  if (ducking) {
    jumpPhase = JUMP_LENGTH;
    tone(BUZZER_PIN, DUCK_PITCH, DUCK_PITCH_DURATION);
  }
}

Herstellungsprozess

  1. Echtzeit-Gyroskop-Spiel mit Arduino Nano & MPU-6050
  2. Klassisches Pong-Spiel auf Arduino UNO mit OLED-Display bauen – Schritt-für-Schritt-Tutorial
  3. DIY Arduino USB Gaming Controller – Bauen Sie Ihren eigenen High-Performance Gamepad
  4. LCD-Animationen & Spiele mit Arduino UNO erstellen
  5. Arduino Touch Breakout-Spiel: Klassisches Arcade auf 2,4″ TFT LCD
  6. Pixel Chaser: Interaktives One-Tap-LED-Spiel mit Arduino Nano
  7. Arduino-gesteuertes automatisiertes Chrome Dino-Spiel bauen
  8. DIY LED-Roulette – Ein-Personen-Arcade-Spiel mit Arduino Nano
  9. Arduino Taschenkonsol mit A-Maze: Kompaktes, flashbares Labyrinth-Spiel
  10. Zuverlässige Arduino-Wetterstation mit DHT-Sensoren bauen