Components and supplies
Jumper wires (generic)
Standard LCD - 16x2 White on Blue
Pushbutton switch 12mm
Breadboard (generic)
Resistor 10k ohm
Buzzer
Rotary Potentiometer, 10 kohm
Arduino Mega 2560
Apps and platforms
Arduino IDE
Project description
Code
Hill Run v2 code
arduino
The code. Change DUCK_PIN to 3 if you want to use an UNO instead. The code is pretty compact and clean, if I say so myself.
1/* 2 * Copyright (c) 2020 by Miles C. 3 An improved version of hill run with interrupts to make code better. 4 NOTICE DUCK_PIN IS 18 INSTEAD OF 4 because of interrupt capablilities! 5 Hill run, a fun game where you jump over hills and duck under crows. 6 Wiring: 7 Arduino +5V to breadboard power bus 8 Arduino Ground to breadboard ground bus 9 Jump button with 10k pull down to ground, and to digital pin 2, and other side to +5V. 10 Duck button with 10k pull down to ground, and to digital pin 18, and other side to +5V. 11 Passive buzzer with one side on ground and one side on digital PWN pin 5. 12 16x2 16-pin Lcd screen with normal arduino wiring, as in the lesson for the lcd screen: 13 -K to ground 14 -A to +5V 15 -D4 through D7 to pins 9 through 12 16 -E to pin 8 17 -RW to ground 18 -RS to pin 7 19 -V0 to potentiometer output, for brightness, potentiometer is connected to +5V and ground 20 -VDD to +5V 21 -VSS to ground 22*/ 23#include <LiquidCrystal.h> 24#include "pitches.h" 25LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 26 27const int JUMP_PIN = 2; 28const int BUZZER_PIN = 5; 29const int DUCK_PIN = 18; //change to 3 if you want to use an UNO instead 30 31const int JUMP_PITCH = 2700; //sounds when button pressed 32const int JUMP_PITCH_DURATION = 50; //sounds when button pressed 33const int DUCK_PITCH = 1350; //sounds when button pressed 34const int DUCK_PITCH_DURATION = 50; //sounds when button pressed 35const int DIE_PITCH = 200; //sounds on death 36const int DIE_PITCH_DURATION = 500; //sounds on death 37const int TICKSPEED = 90; //ms per gametick, 1 gametick per hill move. 38const int JUMP_LENGTH = 3; //chars jumped over when jump is pressed. 39const byte stickStep1[8] = { 40 B01110, 41 B01110, 42 B00101, 43 B11111, 44 B10100, 45 B00110, 46 B11001, 47 B00001, 48}; 49const byte stickStep2[8] = { 50 B01110, 51 B01110, 52 B00101, 53 B11111, 54 B10100, 55 B00110, 56 B01011, 57 B01000, 58}; 59const byte stickJump[8] = { 60 B01110, 61 B01110, 62 B00100, 63 B11111, 64 B00100, 65 B11111, 66 B10001, 67 B00000, 68}; 69const byte stickDuck[8] = { 70 B00000, 71 B00000, 72 B00000, 73 B01110, 74 B01110, 75 B11111, 76 B00100, 77 B11111, 78}; 79const byte hill[8] = { 80 B00000, 81 B00100, 82 B01010, 83 B01110, 84 B11101, 85 B10101, 86 B11001, 87 B11111, 88}; 89const byte crow1[8] = { 90 B00111, 91 B00100, 92 B00110, 93 B01111, 94 B11111, 95 B01111, 96 B00110, 97 B00111, 98}; 99const byte crow2[8] { 100 B00111, 101 B00110, 102 B01111, 103 B11111, 104 B01111, 105 B00110, 106 B00110, 107 B00111, 108}; 109 110volatile int jumpPhase = JUMP_LENGTH + 1; 111int gameTick = 0; 112int crowX = 40; 113int hillX = 25; 114bool playerY = 0; 115volatile bool ducking = LOW; 116bool loopBreaker = 1; 117bool crowGo = 0; 118int score = 0; 119 120void setup() { 121 pinMode(JUMP_PIN, INPUT); 122 pinMode(BUZZER_PIN, OUTPUT); 123 lcd.begin(16, 2); 124 lcd.createChar(0, hill); 125 lcd.createChar(1, stickStep1); 126 lcd.createChar(2, stickStep2); 127 lcd.createChar(3, stickJump); 128 lcd.createChar(4, stickDuck); 129 lcd.createChar(5, crow1); 130 lcd.createChar(6, crow2); 131 attachInterrupt(digitalPinToInterrupt(JUMP_PIN), seeJumping, RISING); 132 attachInterrupt(digitalPinToInterrupt(DUCK_PIN), seeDucking, CHANGE); 133} 134 135void loop() { 136 playerY = 0; 137 if (jumpPhase < JUMP_LENGTH) { 138 playerY = 1; 139 } 140 141 drawSprites(); 142 143 loopBreaker = 1; 144 if (hillX < 16) { 145 if (crowX < hillX) { 146 hillX += 8; 147 loopBreaker = 0; 148 } 149 if (loopBreaker) { 150 lcd.setCursor(hillX, 1); 151 lcd.write((byte)0); 152 } 153 } 154 if (hillX < 1) { 155 if (jumpPhase < JUMP_LENGTH) { 156 score++; 157 hillX = 16 + rand() % 8; 158 } else { 159 endGame(); 160 } 161 } 162 if (crowX < 16) { 163 lcd.setCursor(crowX, 0); 164 if (gameTick % 8 < 4) { 165 lcd.write((byte)5); 166 } else { 167 lcd.write((byte)6); 168 } 169 } 170 if (crowX < 1) { 171 if (ducking) { 172 score++; 173 crowX = 24 + rand() % 16; 174 } else { 175 endGame(); 176 } 177 } 178 lcd.setCursor(0, playerY); 179 lcd.print(" "); 180 jumpPhase++; 181 hillX--; 182 crowGo = !crowGo; 183 crowX -= crowGo; 184 gameTick++; 185 delay(TICKSPEED); 186} 187 188void endGame() { 189 lcd.clear(); 190 lcd.setCursor(0, 0); 191 lcd.print("Score: "); 192 lcd.setCursor(7, 0); 193 lcd.print(score); 194 tone(BUZZER_PIN, DIE_PITCH, DIE_PITCH_DURATION); 195 while (!digitalRead(JUMP_PIN)) { 196 lcd.setCursor(0, 1); 197 if (millis() % 500 < 250) { 198 lcd.print("Jump to Continue"); 199 } else { 200 lcd.print(" "); 201 } 202 } 203 lcd.clear(); 204 score = 0; 205 hillX = 25; 206 crowX = 40; 207} 208 209void drawSprites() { 210 lcd.setCursor(0, 1 - playerY); 211 212 if (!ducking) { 213 if (!playerY) { 214 if ((gameTick % 4) < 2 ) { 215 lcd.write((byte)1); 216 } else { 217 lcd.write((byte)2); 218 } 219 } else { 220 lcd.write((byte)3); 221 } 222 } else { 223 lcd.write((byte)4); 224 } 225 lcd.setCursor(1, 1); 226 lcd.print(" "); 227 lcd.setCursor(1, 0); 228 lcd.print(" "); 229} 230void seeJumping() { 231 if (jumpPhase > (JUMP_LENGTH + 2) && !ducking) { 232 jumpPhase = 0; 233 tone(BUZZER_PIN, JUMP_PITCH, JUMP_PITCH_DURATION); 234 } 235 236} 237void seeDucking() { 238 ducking = digitalRead(DUCK_PIN); 239 if (ducking) { 240 jumpPhase = JUMP_LENGTH; 241 tone(BUZZER_PIN, DUCK_PITCH, DUCK_PITCH_DURATION); 242 } 243} 244
README
markdown
the information
1"Enjoy this Hillrun game! 2Jump over hills by pressing jump, and duck 3 under crows by holding duck. 4DON'T CRASH! If you die, press jump to continue. 5Score 6 = crows ducked under + hills jumped over 7 8GOTO https://create.arduino.cc/projecthub/PunkyMunky64/lcd-hill-run-v2-runner-game-1b0523 9 for more info. 10 11I am only 12 years old and mostly an amateur at Arduino. 12After 13 reading some project hub projects and learning Arduino, 14I was inspired to create 15 this fun game. It did not take too much debugging and is really cool. 16The code 17 is pretty clean by my standards. ENJOY!! Please respect and comment. 18 19Remember 20 to download the libraries included in this program and put them in the directory 21 folder. If there is an error that it cannot find the library, then just use sketch>include 22 library. Comment if you have any problems. 23Libraries at https://github.com/PunkyMunky64/HillRun2Libraries 24 25IF 26 YOU WANT TO USE AN UNO INSTEAD: in the code change const int DUCK_PIN to 3. 27and 28 rewire the duck button from pin 18 to digital pin 3. 29 30Copyright (c) HillRun2.ino 31 by Miles C. 2020 under GNU General Public License. 32 33For wiring schematic and 34 parts needed, visit the arduino page linked above."
README
markdown
the information
1"Enjoy this Hillrun game! 2Jump over hills by pressing jump, and duck under crows by holding duck. 3DON'T CRASH! If you die, press jump to continue. 4Score = crows ducked under + hills jumped over 5 6GOTO https://create.arduino.cc/projecthub/PunkyMunky64/lcd-hill-run-v2-runner-game-1b0523 for more info. 7 8I am only 12 years old and mostly an amateur at Arduino. 9After reading some project hub projects and learning Arduino, 10I was inspired to create this fun game. It did not take too much debugging and is really cool. 11The code is pretty clean by my standards. ENJOY!! Please respect and comment. 12 13Remember to download the libraries included in this program and put them in the directory folder. If there is an error that it cannot find the library, then just use sketch>include library. Comment if you have any problems. 14Libraries at https://github.com/PunkyMunky64/HillRun2Libraries 15 16IF YOU WANT TO USE AN UNO INSTEAD: in the code change const int DUCK_PIN to 3. 17and rewire the duck button from pin 18 to digital pin 3. 18 19Copyright (c) HillRun2.ino by Miles C. 2020 under GNU General Public License. 20 21For wiring schematic and parts needed, visit the arduino page linked above."
Downloadable files
HillRun wiring diagram Schematic image
Circuit Diagram Schematic hillrun2_zp0SI4wns3.fzz This is the HillRun wiring diagram shematic. IF YOU WANT TO USE AN UNO INSTEAD: in the code change const int DUCK_PIN to 3. and rewire the duck button from pin 18 to digital pin 3.
HillRun wiring diagram Schematic image
HillRun wiring diagram Schematic image
Circuit Diagram Schematic hillrun2_zp0SI4wns3.fzz This is the HillRun wiring diagram shematic. IF YOU WANT TO USE AN UNO INSTEAD: in the code change const int DUCK_PIN to 3. and rewire the duck button from pin 18 to digital pin 3.
HillRun wiring diagram Schematic image
Comments
Only logged in users can leave comments
PunkyMunky64
0 Followers
•0 Projects
Table of contents
Intro
85
0