Code
Der CodeArduino
#include #define PIN_BUTTON 2#define PIN_AUTOPLAY 1#define PIN_BUZZER 8#define PIN_READWRITE 10#define PIN_CONTRAST 12#define SPRITE_RUN1 1#define SPRITE_RUN2 2#define SPRITE_JUMP_'JUM'P SPRITE 3#define. // Verwenden Sie das '.' Zeichen für den Kopf#define SPRITE_JUMP_LOWER 4#define SPRITE_TERRAIN_EMPTY ' ' // Benutze das ' ' Zeichen#define SPRITE_TERRAIN_SOLID 5#define SPRITE_TERRAIN_SOLID_RIGHT 6#define SPRITE_TERRAIN_SOLID_LEFT 7#define HERO_WIDine Bildschirm TERRAIN_EMPTY 0#define TERRAIN_LOWER_BLOCK 1#define TERRAIN_UPPER_BLOCK 2#define HERO_POSITION_OFF 0 // Held ist unsichtbar#define HERO_POSITION_RUN_LOWER_1 1 // Held läuft in der unteren Reihe (Pose 1)#define HERO_POSITION_RUN_LOWER_2def_JEROPTY 2 // (Pose_3)#_1 / Einen Sprung starten#define HERO_POSITION_JUMP_2 4 // Auf halber Höhe#define HERO_POSITION_JUMP_3 5 // Sprung ist auf der oberen Reihe#define HERO_POSITION_JUMP_4 6 // Sprung ist auf der oberen Reihe#define HERO_POSITION_JUMP_5 7 // Sprung ist auf der oberen Reihe#define HERO_6_ 8 // Sprung ist in der oberen Reihe#define HERO_POSITION_JUMP_7 9 // Auf halbem Weg nach unten#define HERO_POSITION_JUMP_8 10 // Kurz vor der Landung#define HERO_POSITION_RUN_UPPER_1 11 // Hero läuft in der oberen Reihe (Pose 1)#define HERO_POSITION_RUN_UPPER_2 12 // (Pose 2)LiquidCrystal lcd(11, 9, 6, 5, 4, 3);statisches Zeichen TerrainUpper[TERRAIN_WIDTH + 1];statisches Zeichen TerrainLower [TERRAIN_WIDTH + 1];static bool buttonPushed =false;void initializeGraphics(){ static byte graphics[] ={ // Laufposition 1 B01100, B01100, B00000, B01110, B11100, B01100, B11010, B10011, // Laufposition 2 B01100, B01100, B00000, B01100, B01100, B01100, B01100, B01110, // Sprung B01100, B01100, B00000, B11110, B01101, B11111, B10000, B00000, // Sprung tiefer B11110, B01101, B11111, B10000, B00000, B00000 , B00000, B00000, // Masse B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111, // Masse rechts B00011, B00011, B00011, B00011, B00011, B00011, B00011, B00011, // Masse links B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000, }; int ich; // Überspringe die Verwendung von Zeichen 0, dies ermöglicht die Verwendung von lcd.print() zum // schnellen Zeichnen mehrerer Zeichen für (i =0; i <7; ++i) { lcd.createChar(i + 1, &graphics[i * 8]); } for (i =0; i 9999) ? 5 :(Punktzahl> 999) ? 4:(Punktzahl> 99) ? 3:(Punktzahl> 9) ? 2 :1; // Zeichnen Sie die Szene GeländeUpper[TERRAIN_WIDTH] ='\0'; GeländeLower[TERRAIN_WIDTH] ='\0'; Zeichentemp =GeländeUpper[16-stellig]; GeländeUpper[16-stellig] ='\0'; lcd.setCursor(0,0); lcd.print (terrainUpper); GeländeUpper[16-stellig] =temp; lcd.setCursor(0,1); lcd.print (terrainLower); lcd.setCursor(16 - Ziffern,0); lcd.print (Score); GeländeUpper[HERO_HORIZONTAL_POSITION] =upperSave; GeländeLower[HERO_HORIZONTAL_POSITION] =niedrigerSpeichern; Return Collide;} // Behandeln Sie den Tastendruck als Interruptvoid buttonPush () { buttonPushed =true;}void setup () { pinMode (PIN_READWRITE, OUTPUT); digitalWrite (PIN_READWRITE, LOW); pinMode (PIN_CONTRAST, AUSGANG); digitalWrite (PIN_CONTRAST, NIEDRIG); pinMode (PIN_BUTTON, INPUT); digitalWrite (PIN_BUTTON, HOCH); pinMode (PIN_AUTOPLAY, AUSGANG); digitalWrite (PIN_AUTOPLAY, HOCH); pinMode (PIN_BUZZER, OUTPUT); // den Summer-Pin als Ausgang initialisieren digitalWrite (PIN_BUZZER, LOW); // Digitaler Pin 2 wird auf Interrupt 0 abgebildet attachInterrupt (0/*PIN_BUTTON*/, buttonPush, FALLING); initializeGraphics(); lcd.begin (16, 2);}void loop () { statisches Byte heroPos =HERO_POSITION_RUN_LOWER_1; statisches Byte newTerrainType =TERRAIN_EMPTY; statisches Byte newTerrainDuration =1; statisches Bool-Spielen =false; static bool blink =false; statischer unsigned int Abstand =0; if (!playing) { drawHero((blink) ? HERO_POSITION_OFF :heroPos, TerrainUpper, TerrainLower, Distance>> 3); wenn (blinken) {lcd.setCursor (0,0); lcd.print("Start drücken"); } Verzögerung (250); blink =!blink; if (buttonPushed) { initializeGraphics(); heroPos =HERO_POSITION_RUN_LOWER_1; spielen =wahr; buttonPushed =false; Abstand =0; } Rückkehr; } // Verschiebe das Terrain nach links advanceTerrain(terrainLower, newTerrainType ==TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); AdvanceTerrain(terrainUpper, newTerrainType ==TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); // Neues Terrain erstellen, um rechts einzutreten if (--newTerrainDuration ==0) { if (newTerrainType ==TERRAIN_EMPTY) { newTerrainType =(random(3) ==0) ? TERRAIN_UPPER_BLOCK:TERRAIN_LOWER_BLOCK; newTerrainDuration =2 + zufällig(10); } else { newTerrainType =TERRAIN_EMPTY; newTerrainDuration =10 + random(10); }} if (buttonPushed) { if (heroPos <=HERO_POSITION_RUN_LOWER_2) heroPos =HERO_POSITION_JUMP_1; buttonPushed =false; digitalWrite (PIN_BUZZER, HOCH); } if (drawHero(heroPos, TerrainUpper, TerrainLower, Distance>> 3)) { Playing =false; // Der Held ist mit etwas kollidiert. Schade. für (int i =0; i <=2; i++) { digitalWrite (PIN_BUZZER, HIGH); Verzögerung (100); digitalWrite (PIN_BUZZER, LOW); Verzögerung (100); } } else { if (heroPos ==HERO_POSITION_RUN_LOWER_2 || heroPos ==HERO_POSITION_JUMP_8) { heroPos =HERO_POSITION_RUN_LOWER_1; } else if ((heroPos>=HERO_POSITION_JUMP_3 &&heroPos <=HERO_POSITION_JUMP_5) &&TerrainLower[HERO_HORIZONTAL_POSITION] !=SPRITE_TERRAIN_EMPTY) { heroPos =HERO_POSITION_RUN_UPPER_1; } else if (heroPos>=HERO_POSITION_RUN_UPPER_1 &&TerrainLower[HERO_HORIZONTAL_POSITION] ==SPRITE_TERRAIN_EMPTY) { heroPos =HERO_POSITION_JUMP_5; } sonst if (heroPos ==HERO_POSITION_RUN_UPPER_2) { heroPos =HERO_POSITION_RUN_UPPER_1; } sonst { ++heroPos; } ++Abstand; digitalWrite(PIN_AUTOPLAY, TerrainLower[HERO_HORIZONTAL_POSITION + 2] ==SPRITE_TERRAIN_EMPTY ? HIGH :LOW); } Verzögerung (100); digitalWrite(PIN_BUZZER,LOW);}