Code
Le 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 3#define_ SPRITE' SPRITE // Utilisez le '.' caractère pour la tête#define SPRITE_JUMP_LOWER 4#define SPRITE_TERRAIN_EMPTY ' ' // Utilisez le caractère ' '#define SPRITE_TERRAIN_SOLID 5#define SPRITE_TERRAIN_SOLID_RIGHT 6#define SPRITE_TERRAIN_SOLID_SOLID_LEFT 7#define HEROPOSITION_HORIZW TERRAIN_EMPTY 0#define TERRAIN_LOWER_BLOCK 1#define TERRAIN_UPPER_BLOCK 2#define HERO_POSITION_OFF 0 // Le héros est invisible#define HERO_POSITION_RUN_LOWER_1 1 // Le héros court sur la rangée inférieure (pose 1)#define HERO_POSITION_RUN_LOWER_2 2 // (pose 2) /#JUMP HERO_POSITION 3 / Commencer un saut#define HERO_POSITION_JUMP_2 4 // À mi-hauteur#define HERO_POSITION_JUMP_3 5 // Le saut est sur la ligne supérieure#define HERO_POSITION_JUMP_4 6 // Le saut est sur la ligne supérieure#define HERO_POSITION_JUMP_5 7 // Le saut est sur la ligne supérieure#define HERO_POSITION_JUMP_6 8 // Le saut est sur la rangée supérieure#define HERO_POSITION_JUMP_7 9 // À mi-chemin#define HERO_POSITION_JUMP_8 10 // Sur le point d'atterrir#define HERO_POSITION_RUN_UPPER_1 11 // Hero court sur la rangée supérieure (pose 1)#define HERO_POSITION_RUN_UPPER_2 12 // (pose 2)LiquidCrystal lcd(11, 9, 6, 5, 4, 3);static char terrainUpper[TERRAIN_WIDTH + 1];static char terrainLower [TERRAIN_WIDTH + 1];static bool buttonPushed =false;void initializeGraphics(){ static byte graphics[] ={ // Exécuter la position 1 B01100, B01100, B00000, B01110, B11100, B01100, B11010, B10011, // Exécuter la position 2 B01100, B01100, B00000, B01100, B01100, B01100, B01100, B01110, // Sauter B01100, B01100, B00000, B11110, B01101, B11111, B10000, B00000, // Sauter plus bas B11110, B01101, B11111, B10000, B00000, B00000 , B00000, B00000, // Terre B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111, // Terre droite B00011, B00011, B00011, B00011, B00011, B00011, B00011, B00011, // Terre gauche B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000, } ; int je; // Ignorer l'utilisation du caractère 0, cela permet à lcd.print() d'être utilisé pour // dessiner rapidement plusieurs caractères pour (i =0; i <7; ++i) { lcd.createChar(i + 1, &graphics[i * 8]); } pour (i =0; i 9999) ? 5 :(score> 999) ? 4 :(score> 99) ? 3 :(note> 9) ? 2 :1 ; // Dessine la scène terrainUpper[TERRAIN_WIDTH] ='\0'; terrainLower[TERRAIN_WIDTH] ='\0'; char temp =terrainUpper[16-chiffres] ; terrainUpper[16-chiffres] ='\0'; lcd.setCursor(0,0); lcd.print(terrainSupérieur); terrainUpper[16-chiffres] =temp; lcd.setCursor(0,1) ; lcd.print(terrainLower); lcd.setCursor (16 - chiffres, 0); lcd.print(score); terrainUpper[HERO_HORIZONTAL_POSITION] =upperSave; terrainLower[HERO_HORIZONTAL_POSITION] =lowerSave; return collision;}// Gérer la pression du bouton comme une interruption buttonPush() { buttonPushed =true;}void setup(){ pinMode(PIN_READWRITE, OUTPUT); digitalWrite(PIN_READWRITE, BAS); pinMode(PIN_CONTRASTE, SORTIE); digitalWrite(PIN_CONTRASTE, FAIBLE); pinMode(PIN_BUTTON, INPUT); digitalWrite(PIN_BUTTON, HAUT); pinMode(PIN_AUTOPLAY, SORTIE); digitalWrite(PIN_AUTOPLAY, HAUT); pinMode(PIN_BUZZER,OUTPUT);//initialise la broche du buzzer en tant que sortie digitalWrite(PIN_BUZZER, LOW); // La broche numérique 2 mappe pour interrompre 0 attachInterrupt(0/*PIN_BUTTON*/, buttonPush, FALLING); initializeGraphics(); lcd.begin (16, 2);}boucle vide(){ octet statique heroPos =HERO_POSITION_RUN_LOWER_1; octet statique newTerrainType =TERRAIN_EMPTY; octet statique newTerrainDuration =1 ; lecture booléenne statique =faux ; bool statique clignotement =faux ; distance int non signée statique =0 ; if (!playing) { drawHero((clignote) ? HERO_POSITION_OFF :heroPos, terrainUpper, terrainLower, distance>> 3); if (clignote) { lcd.setCursor(0,0); lcd.print("Appuyez sur Démarrer"); } délai (250); clignoter =!cligner; if (boutonPoussé) { initializeGraphics(); heroPos =HERO_POSITION_RUN_LOWER_1 ; jouer =vrai; boutonPoussé =faux ; distance =0 ; } retourner; } // Décale le terrain vers la gauche advanceTerrain(terrainLower, newTerrainType ==TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); AdvanceTerrain(terrainUpper, newTerrainType ==TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); // Créer un nouveau terrain à saisir à droite if (--newTerrainDuration ==0) { if (newTerrainType ==TERRAIN_EMPTY) { newTerrainType =(random(3) ==0) ? TERRAIN_UPPER_BLOCK :TERRAIN_LOWER_BLOCK ; newTerrainDuration =2 + random(10) ; } else { newTerrainType =TERRAIN_EMPTY; newTerrainDuration =10 + aléatoire(10) ; } } if (buttonPushed) { if (heroPos <=HERO_POSITION_RUN_LOWER_2) heroPos =HERO_POSITION_JUMP_1; boutonPoussé =faux ; digitalWrite(PIN_BUZZER,HIGH); } if (drawHero(heroPos, terrainUpper, terrainLower, distance>> 3)) { play =false; // Le héros est entré en collision avec quelque chose. Dommage. for (int i =0; i <=2; i++) { digitalWrite(PIN_BUZZER, HIGH); retard (100); digitalWrite(PIN_BUZZER, BAS); retard (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; } else if (heroPos ==HERO_POSITION_RUN_UPPER_2) { heroPos =HERO_POSITION_RUN_UPPER_1; } else { ++heroPos; } ++distance; digitalWrite(PIN_AUTOPLAY, terrainLower[HERO_HORIZONTAL_POSITION + 2] ==SPRITE_TERRAIN_EMPTY ? HIGH :LOW); } retard (100); digitalWrite(PIN_BUZZER,LOW);}