Components and supplies
Pushbutton Switch, Momentary
Jumper wires (generic)
Resistor 220 ohm
Alphanumeric LCD, 16 x 2
Arduino UNO
Solderless Breadboard Half Size
Apps and platforms
Arduino IDE
Project description
Code
The code
c_cpp
1#include <LiquidCrystal.h> 2 3#define PIN_BUTTON 2 4#define PIN_AUTOPLAY 1 5#define PIN_READWRITE 10 6#define PIN_CONTRAST 12 7 8#define SPRITE_RUN1 1 9#define SPRITE_RUN2 2 10#define SPRITE_JUMP 3 11#define SPRITE_JUMP_UPPER '.' // Use the '.' character for the head 12#define SPRITE_JUMP_LOWER 4 13#define SPRITE_TERRAIN_EMPTY ' ' // User the ' ' character 14#define SPRITE_TERRAIN_SOLID 5 15#define SPRITE_TERRAIN_SOLID_RIGHT 6 16#define SPRITE_TERRAIN_SOLID_LEFT 7 17 18#define HERO_HORIZONTAL_POSITION 1 // Horizontal position of hero on screen 19 20#define TERRAIN_WIDTH 16 21#define TERRAIN_EMPTY 0 22#define TERRAIN_LOWER_BLOCK 1 23#define TERRAIN_UPPER_BLOCK 2 24 25#define HERO_POSITION_OFF 0 // Hero is invisible 26#define HERO_POSITION_RUN_LOWER_1 1 // Hero is running on lower row (pose 1) 27#define HERO_POSITION_RUN_LOWER_2 2 // (pose 2) 28 29#define HERO_POSITION_JUMP_1 3 // Starting a jump 30#define HERO_POSITION_JUMP_2 4 // Half-way up 31#define HERO_POSITION_JUMP_3 5 // Jump is on upper row 32#define HERO_POSITION_JUMP_4 6 // Jump is on upper row 33#define HERO_POSITION_JUMP_5 7 // Jump is on upper row 34#define HERO_POSITION_JUMP_6 8 // Jump is on upper row 35#define HERO_POSITION_JUMP_7 9 // Half-way down 36#define HERO_POSITION_JUMP_8 10 // About to land 37 38#define HERO_POSITION_RUN_UPPER_1 11 // Hero is running on upper row (pose 1) 39#define HERO_POSITION_RUN_UPPER_2 12 // (pose 2) 40 41LiquidCrystal lcd(12,11,5,4,3,6); 42int score = 0; 43static char terrainUpper[TERRAIN_WIDTH + 1]; 44static char terrainLower[TERRAIN_WIDTH + 1]; 45static bool buttonPushed = false; 46 47void initializeGraphics(){ 48 static byte graphics[] = { 49 // Run position 1 50 B01100, 51 B01100, 52 B00000, 53 B01110, 54 B11100, 55 B01100, 56 B11010, 57 B10011, 58 // Run position 2 59 B01100, 60 B01100, 61 B00000, 62 B01100, 63 B01100, 64 B01100, 65 B01100, 66 B01110, 67 // Jump 68 B01100, 69 B01100, 70 B00000, 71 B11110, 72 B01101, 73 B11111, 74 B10000, 75 B00000, 76 // Jump lower 77 B11110, 78 B01101, 79 B11111, 80 B10000, 81 B00000, 82 B00000, 83 B00000, 84 B00000, 85 // Ground 86 B11111, 87 B11111, 88 B11111, 89 B11111, 90 B11111, 91 B11111, 92 B11111, 93 B11111, 94 // Ground right 95 B00011, 96 B00011, 97 B00011, 98 B00011, 99 B00011, 100 B00011, 101 B00011, 102 B00011, 103 // Ground left 104 B11000, 105 B11000, 106 B11000, 107 B11000, 108 B11000, 109 B11000, 110 B11000, 111 B11000, 112 }; 113 int i; 114 // Skip using character 0, this allows lcd.print() to be used to 115 // quickly draw multiple characters 116 for (i = 0; i < 7; ++i) { 117 lcd.createChar(i + 1, &graphics[i * 8]); 118 } 119 for (i = 0; i < TERRAIN_WIDTH; ++i) { 120 terrainUpper[i] = SPRITE_TERRAIN_EMPTY; 121 terrainLower[i] = SPRITE_TERRAIN_EMPTY; 122 } 123} 124 125// Slide the terrain to the left in half-character increments 126// 127void advanceTerrain(char* terrain, byte newTerrain){ 128 for (int i = 0; i < TERRAIN_WIDTH; ++i) { 129 char current = terrain[i]; 130 char next = (i == TERRAIN_WIDTH-1) ? newTerrain : terrain[i+1]; 131 switch (current){ 132 case SPRITE_TERRAIN_EMPTY: 133 terrain[i] = (next == SPRITE_TERRAIN_SOLID) ? SPRITE_TERRAIN_SOLID_RIGHT : SPRITE_TERRAIN_EMPTY; 134 break; 135 case SPRITE_TERRAIN_SOLID: 136 terrain[i] = (next == SPRITE_TERRAIN_EMPTY) ? SPRITE_TERRAIN_SOLID_LEFT : SPRITE_TERRAIN_SOLID; 137 break; 138 case SPRITE_TERRAIN_SOLID_RIGHT: 139 terrain[i] = SPRITE_TERRAIN_SOLID; 140 break; 141 case SPRITE_TERRAIN_SOLID_LEFT: 142 terrain[i] = SPRITE_TERRAIN_EMPTY; 143 break; 144 } 145 } 146} 147 148bool drawHero(byte position, char* terrainUpper, char* terrainLower, unsigned int score) { 149 bool collide = false; 150 char upperSave = terrainUpper[HERO_HORIZONTAL_POSITION]; 151 char lowerSave = terrainLower[HERO_HORIZONTAL_POSITION]; 152 byte upper, lower; 153 switch (position) { 154 case HERO_POSITION_OFF: 155 upper = lower = SPRITE_TERRAIN_EMPTY; 156 break; 157 case HERO_POSITION_RUN_LOWER_1: 158 upper = SPRITE_TERRAIN_EMPTY; 159 lower = SPRITE_RUN1; 160 break; 161 case HERO_POSITION_RUN_LOWER_2: 162 upper = SPRITE_TERRAIN_EMPTY; 163 lower = SPRITE_RUN2; 164 break; 165 case HERO_POSITION_JUMP_1: 166 case HERO_POSITION_JUMP_8: 167 upper = SPRITE_TERRAIN_EMPTY; 168 lower = SPRITE_JUMP; 169 break; 170 case HERO_POSITION_JUMP_2: 171 case HERO_POSITION_JUMP_7: 172 upper = SPRITE_JUMP_UPPER; 173 lower = SPRITE_JUMP_LOWER; 174 break; 175 case HERO_POSITION_JUMP_3: 176 case HERO_POSITION_JUMP_4: 177 case HERO_POSITION_JUMP_5: 178 case HERO_POSITION_JUMP_6: 179 upper = SPRITE_JUMP; 180 lower = SPRITE_TERRAIN_EMPTY; 181 break; 182 case HERO_POSITION_RUN_UPPER_1: 183 upper = SPRITE_RUN1; 184 lower = SPRITE_TERRAIN_EMPTY; 185 break; 186 case HERO_POSITION_RUN_UPPER_2: 187 upper = SPRITE_RUN2; 188 lower = SPRITE_TERRAIN_EMPTY; 189 break; 190 } 191 if (upper != ' ') { 192 terrainUpper[HERO_HORIZONTAL_POSITION] = upper; 193 collide = (upperSave == SPRITE_TERRAIN_EMPTY) ? false : true; 194 } 195 if (lower != ' ') { 196 terrainLower[HERO_HORIZONTAL_POSITION] = lower; 197 collide |= (lowerSave == SPRITE_TERRAIN_EMPTY) ? false : true; 198 } 199 200 byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1; 201 202 // Draw the scene 203 terrainUpper[TERRAIN_WIDTH] = '\\0'; 204 terrainLower[TERRAIN_WIDTH] = '\\0'; 205 char temp = terrainUpper[16-digits]; 206 terrainUpper[16-digits] = '\\0'; 207 lcd.setCursor(0,0); 208 lcd.print(terrainUpper); 209 terrainUpper[16-digits] = temp; 210 lcd.setCursor(0,1); 211 lcd.print(terrainLower); 212 213 lcd.setCursor(16 - digits,0); 214 lcd.print(score); 215 216 terrainUpper[HERO_HORIZONTAL_POSITION] = upperSave; 217 terrainLower[HERO_HORIZONTAL_POSITION] = lowerSave; 218 return collide; 219} 220 221// Handle the button push as an interrupt 222void buttonPush() { 223 buttonPushed = true; 224} 225 226void setup(){ 227 pinMode(PIN_READWRITE, OUTPUT); 228 digitalWrite(PIN_READWRITE, LOW); 229 pinMode(PIN_CONTRAST, OUTPUT); 230 digitalWrite(PIN_CONTRAST, LOW); 231 pinMode(PIN_BUTTON, INPUT); 232 digitalWrite(PIN_BUTTON, HIGH); 233 pinMode(PIN_AUTOPLAY, OUTPUT); 234 digitalWrite(PIN_AUTOPLAY, HIGH); 235 236 // Digital pin 2 maps to interrupt 0 237 attachInterrupt(0/*PIN_BUTTON*/, buttonPush, FALLING); 238 239 initializeGraphics(); 240 241 lcd.begin(16, 2); 242} 243 244void loop(){ 245 static byte heroPos = HERO_POSITION_RUN_LOWER_1; 246 static byte newTerrainType = TERRAIN_EMPTY; 247 static byte newTerrainDuration = 1; 248 static bool playing = false; 249 static bool blink = false; 250 static unsigned int distance = 0; 251 score = millis()/1000; 252 253 if (!playing) { 254 drawHero((blink) ? HERO_POSITION_OFF : heroPos, terrainUpper, terrainLower, distance >> 3); 255 if (blink) { 256 lcd.setCursor(0,0); 257 lcd.print("Press Start"); 258 } 259 delay(250); 260 blink = !blink; 261 if (buttonPushed) { 262 initializeGraphics(); 263 heroPos = HERO_POSITION_RUN_LOWER_1; 264 playing = true; 265 buttonPushed = false; 266 distance = 0; 267 } 268 return; 269 } 270 271 // Shift the terrain to the left 272 advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY); 273 advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY); 274 275 // Make new terrain to enter on the right 276 if (--newTerrainDuration == 0) { 277 if (newTerrainType == TERRAIN_EMPTY) { 278 newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK; 279 newTerrainDuration = 2 + random(10); 280 } else { 281 newTerrainType = TERRAIN_EMPTY; 282 newTerrainDuration = 10 + random(10); 283 } 284 } 285 286 if (buttonPushed) { 287 if (heroPos <= HERO_POSITION_RUN_LOWER_2) heroPos = HERO_POSITION_JUMP_1; 288 buttonPushed = false; 289 } 290 291 if (drawHero(heroPos, terrainUpper, terrainLower, distance >> 3)) { 292 playing = false; // The hero collided with something. Too bad. 293 score = 0; 294 } else { 295 if (heroPos == HERO_POSITION_RUN_LOWER_2 || heroPos == HERO_POSITION_JUMP_8) { 296 heroPos = HERO_POSITION_RUN_LOWER_1; 297 } else if ((heroPos >= HERO_POSITION_JUMP_3 && heroPos <= HERO_POSITION_JUMP_5) && terrainLower[HERO_HORIZONTAL_POSITION] != SPRITE_TERRAIN_EMPTY) { 298 heroPos = HERO_POSITION_RUN_UPPER_1; 299 } else if (heroPos >= HERO_POSITION_RUN_UPPER_1 && terrainLower[HERO_HORIZONTAL_POSITION] == SPRITE_TERRAIN_EMPTY) { 300 heroPos = HERO_POSITION_JUMP_5; 301 } else if (heroPos == HERO_POSITION_RUN_UPPER_2) { 302 heroPos = HERO_POSITION_RUN_UPPER_1; 303 } else { 304 ++heroPos; 305 } 306 ++distance; 307 308 digitalWrite(PIN_AUTOPLAY, terrainLower[HERO_HORIZONTAL_POSITION + 2] == SPRITE_TERRAIN_EMPTY ? HIGH : LOW); 309 if(score >= 100) 310 { 311 // if your score is greater than 100, it displays victory message 312 lcd.clear(); 313 lcd.print("You Win!!");// change the message to whatever you want 314 score = 0; 315 delay(2000); 316 } 317 } 318 delay(100); 319}
Downloadable files
The Circuit
The Circuit
The Circuit
The Circuit
Comments
Only logged in users can leave comments