Jump Over People Game
You press a button to make the person jump over other people in the way and when you fail, it gives you a score.
Components and supplies
1
Breadboard (generic)
1
Resistor 10k ohm
1
Rotary potentiometer (generic)
1
Standard LCD - 16x2 White on Blue
1
Pushbutton switch 12mm
1
Arduino UNO
1
Jumper wires (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
game.ino
c_cpp
1#include <LiquidCrystal.h> 2 3const int btn = 8; //button input pin 4 5//texts to display 6char* introText[]={"Jump over the PEOPLE!!!","Created on:", "Hit the button..."}; 7 8//variable declarations 9int game=0; 10int buttonPress = 0; 11int buttonState=0; 12int obstaclePos; 13int obsDelay; 14int score=0; 15unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 16unsigned long debounceDelay = 50; 17int lastButtonState=LOW; 18 19//GRAPHICS 20byte runnin1[8] = { 21 0b01100, 22 0b01100, 23 0b00000, 24 0b11100, 25 0b01110, 26 0b01100, 27 0b11110, 28 0b00011 29}; 30byte runnin2[8]={ 31 0b00110, 32 0b00110, 33 0b00000, 34 0b00111, 35 0b01110, 36 0b00110, 37 0b01110, 38 0b00101 39}; 40byte jump[8]={ 41 0b00100, 42 0b01100, 43 0b11100, 44 0b01110, 45 0b01100, 46 0b11111, 47 0b00000, 48 0b00001 49}; 50byte def1[8] = { 51 0b00100, 52 0b00110, 53 0b00111, 54 0b01110, 55 0b00110, 56 0b01111, 57 0b01000, 58 0b00000 59}; 60byte def2[8]={ 61 0b00100, 62 0b00110, 63 0b00111, 64 0b01110, 65 0b00110, 66 0b00110, 67 0b00110, 68 0b00010 69}; 70byte slide[8]={ 71 0b00000, 72 0b00000, 73 0b00011, 74 0b00011, 75 0b00000, 76 0b01111, 77 0b11111, 78 0b00000 79}; 80 81 82LiquidCrystal lcd(4,6,10,11,12,13); 83void setup() { 84 85 Serial.begin(9600); 86 pinMode(btn,INPUT); 87 lcd.begin(16,2); 88 lcd.createChar(0,runnin1); 89 lcd.createChar(1,runnin2); 90 lcd.createChar(2,jump); 91 lcd.createChar(3,def1); 92 lcd.createChar(4,def2); 93 lcd.createChar(5,slide); 94 buttonState=digitalRead(btn); 95} 96 97void loop() 98{ 99 100 intro(); 101 while(game==1) 102 ballActive(); 103 endGame(); 104} 105 106 void intro() 107 { 108 score=0; 109 lcd.clear(); 110 lcd.print(introText[0]); //Instead of declaring introText, you can directly print the text with " ... " 111 lcd.setCursor(1,1); 112 lcd.print("By Aedan_Barr"); 113 delay(2000); 114 lcd.clear(); 115 lcd.print(introText[1]); 116 lcd.setCursor(0,1); 117 lcd.print("3/3/2019"); 118 delay(2000); 119 lcd.clear(); 120 while(button()!=1) //wait for the user to hit the button, display the text until that. 121 { 122 lcd.setCursor(0,0); 123 lcd.print(introText[2]); 124 delay(100); 125 } 126 if(button()==1) 127 game=1; 128 } 129 130 void reset() 131 { 132 if(obstaclePos%2==0) //Changing the display of the player from one pose to another to show he is running, smartly using obstaclePos. 133 { 134 lcd.clear(); 135 lcd.setCursor(0,1); 136 lcd.write(byte(0)); 137 } 138 else 139 { 140 lcd.clear(); 141 lcd.setCursor(0,1); 142 lcd.write(1); 143 } 144 } 145 146 void ballActive() 147 { 148 obsDelay=200; //initial delay for the defender 149 obstaclePos=15; //since it is a 16x2 lcd, and index starts from 0. 150 while(game==1) 151 { 152 reset(); 153 if(button()==1) //if button is pressed. 154 { 155 lcd.clear(); 156 lcd.setCursor(0,0); 157 lcd.write(2); //print the player jumping 158 obstacle(obstaclePos); //print the defender 159 delay(400); //This is to prevent the display disappearing fast, and going unnoticed. 160 lcd.clear(); 161 lcd.setCursor(0,1); 162 lcd.write(1); //print the player back on ground 163 obstacle(obstaclePos); //print the defender 164 165 } 166 else 167 { 168 if(obstaclePos!=0) //if the defender has not approached the player, then continue. 169 { 170 reset(); 171 obstacle(obstaclePos); 172 } 173 else if(obstaclePos==0) //if the defender is at 0 position,i.e. he has approached the player when he is on ground, then end the game. 174 { 175 game=0; 176 break; 177 } 178 } 179 obstaclePos--; //brings the defender closer to the player 180 if(obstaclePos<0) //if the player successfully dodged then, 181 { 182 obsDelay=obsDelay-20; //decrease the delay (increase speed), 183 obstaclePos=15; //bring the defender to starting position, 184 score++; //and increase the score by 1. 185 } 186 if(obsDelay==0) //if 10 players have been dodged (because 200/20=10), then end the game 187 game=0; 188 189 delay(obsDelay); 190 } 191 } 192int button() 193{ 194 int reading = digitalRead(btn); 195 // If the switch changed, due to noise or pressing: 196 if (reading != lastButtonState) { 197 // reset the debouncing timer 198 lastDebounceTime = millis(); 199 } 200 201 if ((millis() - lastDebounceTime) > debounceDelay) { 202 buttonState = reading; 203 if (buttonState == HIGH) { 204 lastButtonState = reading; 205 return 1; 206 } 207 } 208 lastButtonState = reading; 209} 210void obstacle(int i) 211{ 212 if(obstaclePos%2==0&& obstaclePos>2) //if defender is far away from the player, then show him running 213 { 214 lcd.setCursor(i,1); 215 lcd.write(3); 216 } 217 else if(obstaclePos%2!=0&& obstaclePos>2) 218 { 219 lcd.setCursor(i,1); 220 lcd.write(4); 221 } 222 else //if he is 2 steps close to the player, then show him sliding 223 { 224 lcd.setCursor(i,1); 225 lcd.write(5); 226 } 227} 228void endGame() 229{ 230 if(score==10) 231 { 232 lcd.clear(); 233 lcd.print("Congratulations!"); 234 lcd.setCursor(0,1); 235 lcd.print("You beat 'em all"); 236 delay(3000); 237 } 238 else if(score<3) 239 { 240 lcd.clear(); 241 lcd.print("Your ok"); 242 lcd.setCursor(0,1); 243 lcd.print("Score: "); 244 lcd.print(score); 245 delay(3000); 246 } 247 else if(score<6) 248 { 249 lcd.clear(); 250 lcd.print("Not bad!"); 251 lcd.setCursor(0,1); 252 lcd.print("Score: "); 253 lcd.print(score); 254 delay(3000); 255 } 256 else 257 { 258 lcd.clear(); 259 lcd.print("Great!"); 260 lcd.setCursor(0,1); 261 lcd.print("Score: "); 262 lcd.print(score); 263 delay(3000); 264 } 265} 266
Downloadable files
How to put it together
How to put it together
Comments
Only logged in users can leave comments