Police Run
Run away and escape from 10 police who chase you to collect bribe.
Components and supplies
1
RGB LCD Shield Kit, 16x2 Character Display
1
Pushbutton switch 12mm
1
Arduino UNO
1
Rotary potentiometer (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
CODE
c_cpp
Copy and paste the code.
1#include <LiquidCrystal.h> 2 3const int btn = 8; //button input pin 4 5char* introText[]={"Police Run","Created on:", "Hit the button..."}; 6 7//variable declarations 8int game=0; 9int buttonPress = 0; 10int buttonState = 0; 11int obstaclePos; 12int obsDelay; 13int score=0; 14unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 15unsigned long debounceDelay = 50; 16int lastButtonState=LOW; 17 18//GRAPHICS 19byte runnin1[8] = { 20 0b01100, 21 0b01100, 22 0b00000, 23 0b11100, 24 0b01110, 25 0b01100, 26 0b11110, 27 0b00011 28}; 29byte runnin2[8]={ 30 0b00110, 31 0b00110, 32 0b00000, 33 0b00111, 34 0b01110, 35 0b00110, 36 0b01110, 37 0b00101 38}; 39byte jump[8]={ 40 0b00100, 41 0b01100, 42 0b11100, 43 0b01110, 44 0b01100, 45 0b11111, 46 0b00000, 47 0b00001 48}; 49byte def1[8] = { 50 0b00100, 51 0b00110, 52 0b00111, 53 0b01110, 54 0b00110, 55 0b01111, 56 0b01000, 57 0b00000 58}; 59byte def2[8]={ 60 0b00100, 61 0b00110, 62 0b00111, 63 0b01110, 64 0b00110, 65 0b00110, 66 0b00110, 67 0b00010 68}; 69byte slide[8]={ 70 0b00000, 71 0b00000, 72 0b00011, 73 0b00011, 74 0b00000, 75 0b01111, 76 0b11111, 77 0b00000 78}; 79 80 81LiquidCrystal lcd(4,6,10,11,12,13); 82void setup() { 83 84 Serial.begin(9600); 85 pinMode(btn,INPUT); 86 lcd.begin(16,2); 87 lcd.createChar(0,runnin1); 88 lcd.createChar(1,runnin2); 89 lcd.createChar(2,jump); 90 lcd.createChar(3,def1); 91 lcd.createChar(4,def2); 92 lcd.createChar(5,slide); 93 buttonState=digitalRead(btn); 94} 95 96void loop() 97{ 98 99 intro(); 100 while(game==1) 101 ballActive(); 102 endGame(); 103} 104 105 void intro() 106 { 107 score=0; 108 lcd.clear(); 109 lcd.print("Police Run"); 110 lcd.print("Hit the button"); 111 lcd.setCursor(7,1); 112 lcd.clear(); 113 lcd.setCursor(0,1); 114 lcd.clear(); 115 while(button()!=1) 116 { 117 lcd.setCursor(0,0); 118 lcd.print(introText[2]); 119 delay(100); 120 } 121 if(button()==1) 122 game=1; 123 } 124 125 void reset() 126 { 127 if(obstaclePos%2==0) //Changing the display of the player from one pose to another to show he is running, smartly using obstaclePos. 128 { 129 lcd.clear(); 130 lcd.setCursor(0,1); 131 lcd.write(byte(0)); 132 } 133 else 134 { 135 lcd.clear(); 136 lcd.setCursor(0,1); 137 lcd.write(1); 138 } 139 } 140 141 void ballActive() 142 { 143 obsDelay=200; //initial delay for the defender 144 obstaclePos=15; //since it is a 16x2 lcd, and index starts from 0. 145 while(game==1) 146 { 147 reset(); 148 if(button()==1) //if button is pressed. 149 { 150 lcd.clear(); 151 lcd.setCursor(0,0); 152 lcd.write(2); //print the player jumping 153 obstacle(obstaclePos); //print the defender 154 delay(400); //This is to prevent the display disappearing fast, and going unnoticed. 155 lcd.clear(); 156 lcd.setCursor(0,1); 157 lcd.write(1); //print the player back on ground 158 obstacle(obstaclePos); //print the defender 159 160 } 161 else 162 { 163 if(obstaclePos!=0) //if the defender has not approached the player, then continue. 164 { 165 reset(); 166 obstacle(obstaclePos); 167 } 168 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. 169 { 170 game=0; 171 break; 172 } 173 } 174 obstaclePos--; //brings the defender closer to the player 175 if(obstaclePos<0) //if the player successfully dodged then, 176 { 177 obsDelay=obsDelay-20; //decrease the delay (increase speed), 178 obstaclePos=15; //bring the defender to starting position, 179 score++; //and increase the score by 1. 180 } 181 if(obsDelay==0) //if 10 players have been dodged (because 200/20=10), then end the game 182 game=0; 183 184 delay(obsDelay); 185 } 186 } 187int button() 188{ 189 int reading = digitalRead(btn); 190 // If the switch changed, due to noise or pressing: 191 if (reading != lastButtonState) { 192 // reset the debouncing timer 193 lastDebounceTime = millis(); 194 } 195 196 if ((millis() - lastDebounceTime) > debounceDelay) { 197 buttonState = reading; 198 if (buttonState == HIGH) { 199 lastButtonState = reading; 200 return 1; 201 } 202 } 203 lastButtonState = reading; 204} 205void obstacle(int i) 206{ 207 if(obstaclePos%2==0&& obstaclePos>2) //if defender is far away from the player, then show him running 208 { 209 lcd.setCursor(i,1); 210 lcd.write(3); 211 } 212 else if(obstaclePos%2!=0&& obstaclePos>2) 213 { 214 lcd.setCursor(i,1); 215 lcd.write(4); 216 } 217 else //if he is 2 steps close to the player, then show him sliding 218 { 219 lcd.setCursor(i,1); 220 lcd.write(5); 221 } 222} 223void endGame() 224{ 225 if(score==10) 226 { 227 lcd.clear(); 228 lcd.print("Congratulations!"); 229 lcd.setCursor(0,1); 230 lcd.print("You beat 'em all"); 231 delay(3000); 232 } 233 else if(score<3) 234 { 235 lcd.clear(); 236 lcd.print("You los"); 237 lcd.setCursor(0,1); 238 lcd.print("Score: "); 239 lcd.print(score); 240 delay(3000); 241 } 242 else if(score<6) 243 { 244 lcd.clear(); 245 lcd.print("Not bad!"); 246 lcd.setCursor(0,1); 247 lcd.print("Score: "); 248 lcd.print(score); 249 delay(3000); 250 } 251 else 252 { 253 lcd.clear(); 254 lcd.print("Great!"); 255 lcd.setCursor(0,1); 256 lcd.print("Score: "); 257 lcd.print(score); 258 delay(3000); 259 } 260}
CODE
c_cpp
Copy and paste the code.
1#include <LiquidCrystal.h> 2 3const int btn = 8; //button input pin 4 5char* 6 introText[]={"Police Run","Created on:", "Hit the button..."}; 7 8//variable 9 declarations 10int game=0; 11int buttonPress = 0; 12int buttonState = 0; 13int 14 obstaclePos; 15int obsDelay; 16int score=0; 17unsigned long lastDebounceTime 18 = 0; // the last time the output pin was toggled 19unsigned long debounceDelay 20 = 50; 21int lastButtonState=LOW; 22 23//GRAPHICS 24byte runnin1[8] = { 25 26 0b01100, 27 0b01100, 28 0b00000, 29 0b11100, 30 0b01110, 31 0b01100, 32 33 0b11110, 34 0b00011 35}; 36byte runnin2[8]={ 37 0b00110, 38 0b00110, 39 40 0b00000, 41 0b00111, 42 0b01110, 43 0b00110, 44 0b01110, 45 0b00101 46}; 47byte 48 jump[8]={ 49 0b00100, 50 0b01100, 51 0b11100, 52 0b01110, 53 0b01100, 54 55 0b11111, 56 0b00000, 57 0b00001 58}; 59byte def1[8] = { 60 0b00100, 61 62 0b00110, 63 0b00111, 64 0b01110, 65 0b00110, 66 0b01111, 67 0b01000, 68 69 0b00000 70}; 71byte def2[8]={ 72 0b00100, 73 0b00110, 74 0b00111, 75 76 0b01110, 77 0b00110, 78 0b00110, 79 0b00110, 80 0b00010 81}; 82byte 83 slide[8]={ 84 0b00000, 85 0b00000, 86 0b00011, 87 0b00011, 88 0b00000, 89 90 0b01111, 91 0b11111, 92 0b00000 93}; 94 95 96LiquidCrystal lcd(4,6,10,11,12,13); 97void 98 setup() { 99 100 Serial.begin(9600); 101 pinMode(btn,INPUT); 102 lcd.begin(16,2); 103 104 lcd.createChar(0,runnin1); 105 lcd.createChar(1,runnin2); 106 lcd.createChar(2,jump); 107 108 lcd.createChar(3,def1); 109 lcd.createChar(4,def2); 110 lcd.createChar(5,slide); 111 112 buttonState=digitalRead(btn); 113} 114 115void loop() 116{ 117 118 intro(); 119 120 while(game==1) 121 ballActive(); 122 endGame(); 123} 124 125 void 126 intro() 127 { 128 score=0; 129 lcd.clear(); 130 lcd.print("Police 131 Run"); 132 lcd.print("Hit the button"); 133 lcd.setCursor(7,1); 134 135 lcd.clear(); 136 lcd.setCursor(0,1); 137 lcd.clear(); 138 139 while(button()!=1) 140 { 141 lcd.setCursor(0,0); 142 lcd.print(introText[2]); 143 144 delay(100); 145 } 146 if(button()==1) 147 game=1; 148 149 } 150 151 void reset() 152 { 153 if(obstaclePos%2==0) //Changing 154 the display of the player from one pose to another to show he is running, smartly 155 using obstaclePos. 156 { 157 lcd.clear(); 158 lcd.setCursor(0,1); 159 160 lcd.write(byte(0)); 161 } 162 else 163 { 164 lcd.clear(); 165 166 lcd.setCursor(0,1); 167 lcd.write(1); 168 } 169 } 170 171 172 void ballActive() 173 { 174 obsDelay=200; //initial delay for the defender 175 176 obstaclePos=15; //since it is a 16x2 lcd, and index starts from 0. 177 while(game==1) 178 179 { 180 reset(); 181 if(button()==1) //if button is pressed. 182 183 { 184 lcd.clear(); 185 lcd.setCursor(0,0); 186 lcd.write(2); 187 //print the player jumping 188 obstacle(obstaclePos); //print 189 the defender 190 delay(400); //This is to prevent the display 191 disappearing fast, and going unnoticed. 192 lcd.clear(); 193 lcd.setCursor(0,1); 194 195 lcd.write(1); //print the player back on ground 196 obstacle(obstaclePos); 197 //print the defender 198 199 } 200 else 201 { 202 203 if(obstaclePos!=0) //if the defender has not approached the player, 204 then continue. 205 { 206 reset(); 207 obstacle(obstaclePos); 208 209 } 210 else if(obstaclePos==0) //if the defender is at 0 position,i.e. 211 he has approached the player when he is on ground, then end the game. 212 { 213 214 game=0; 215 break; 216 } 217 } 218 obstaclePos--; 219 //brings the defender closer to the player 220 if(obstaclePos<0) 221 //if the player successfully dodged then, 222 { 223 224 obsDelay=obsDelay-20; //decrease the delay (increase speed), 225 226 obstaclePos=15; //bring the defender to starting position, 227 228 score++; //and increase the score by 1. 229 } 230 231 if(obsDelay==0) //if 10 players have been dodged (because 232 200/20=10), then end the game 233 game=0; 234 235 delay(obsDelay); 236 237 } 238 } 239int button() 240{ 241 int reading = digitalRead(btn); 242 // 243 If the switch changed, due to noise or pressing: 244 if (reading != lastButtonState) 245 { 246 // reset the debouncing timer 247 lastDebounceTime = millis(); 248 249 } 250 251 if ((millis() - lastDebounceTime) > debounceDelay) { 252 253 buttonState = reading; 254 if (buttonState == HIGH) { 255 lastButtonState 256 = reading; 257 return 1; 258 } 259 } 260 lastButtonState 261 = reading; 262} 263void obstacle(int i) 264{ 265 if(obstaclePos%2==0&& 266 obstaclePos>2) //if defender is far away from the player, then show him running 267 268 { 269 lcd.setCursor(i,1); 270 lcd.write(3); 271 272 } 273 else if(obstaclePos%2!=0&& obstaclePos>2) 274 { 275 276 lcd.setCursor(i,1); 277 lcd.write(4); 278 } 279 280 else //if he is 2 steps close to the player, 281 then show him sliding 282 { 283 lcd.setCursor(i,1); 284 lcd.write(5); 285 286 } 287} 288void endGame() 289{ 290 if(score==10) 291 { 292 lcd.clear(); 293 294 lcd.print("Congratulations!"); 295 lcd.setCursor(0,1); 296 lcd.print("You 297 beat 'em all"); 298 delay(3000); 299 } 300 else if(score<3) 301 { 302 303 lcd.clear(); 304 lcd.print("You los"); 305 lcd.setCursor(0,1); 306 307 lcd.print("Score: "); 308 lcd.print(score); 309 delay(3000); 310 311 } 312 else if(score<6) 313 { 314 lcd.clear(); 315 lcd.print("Not 316 bad!"); 317 lcd.setCursor(0,1); 318 lcd.print("Score: "); 319 lcd.print(score); 320 321 delay(3000); 322 } 323 else 324 { 325 lcd.clear(); 326 lcd.print("Great!"); 327 328 lcd.setCursor(0,1); 329 lcd.print("Score: "); 330 lcd.print(score); 331 332 delay(3000); 333 } 334}
Downloadable files
SCHEMATICS
Download this file with fritzing.
SCHEMATICS
Comments
Only logged in users can leave comments