Components and supplies
1
Jumper wires (generic)
1
Pushbutton switch 12mm
2
Resistor 10k ohm
1
Standard LCD - 16x2 White on Blue
1
Arduino UNO
1
Resistor 1k ohm
Project description
Code
lcd_1.ino
arduino
1/* 2 Dodge the defs!! 3 Created by : Shreyas R P 4 Created on: 21-06-2018 5 E-mail ID : shreyasrp888@gmail.com 6 A simple game for 16x2 LCD with a button. 7 If you make any improvements or changes to the game, please add your credits below the line: 8 Enjoy :) 9 Copyright (C) 2018 Shreyas R P 10 11 This program is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program. If not, see <https://www.gnu.org/licenses/>. 23 --------------------------------------------------------------------------------------------- 24 */ 25#include <LiquidCrystal.h> 26 27const int btn = 8; //button input pin 28 29//texts to display 30char* introText[]={"Dodge the defs!!","Created on:", "Hit the button..."}; 31 32//variable declarations 33int game=0; 34int buttonPress = 0; 35int buttonState=0; 36int obstaclePos; 37int obsDelay; 38int score=0; 39unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 40unsigned long debounceDelay = 50; 41int lastButtonState=LOW; 42 43//GRAPHICS 44byte runnin1[8] = { 45 0b01100, 46 0b01100, 47 0b00000, 48 0b11100, 49 0b01110, 50 0b01100, 51 0b11110, 52 0b00011 53}; 54byte runnin2[8]={ 55 0b00110, 56 0b00110, 57 0b00000, 58 0b00111, 59 0b01110, 60 0b00110, 61 0b01110, 62 0b00101 63}; 64byte jump[8]={ 65 0b00100, 66 0b01100, 67 0b11100, 68 0b01110, 69 0b01100, 70 0b11111, 71 0b00000, 72 0b00001 73}; 74byte def1[8] = { 75 0b00100, 76 0b00110, 77 0b00111, 78 0b01110, 79 0b00110, 80 0b01111, 81 0b01000, 82 0b00000 83}; 84byte def2[8]={ 85 0b00100, 86 0b00110, 87 0b00111, 88 0b01110, 89 0b00110, 90 0b00110, 91 0b00110, 92 0b00010 93}; 94byte slide[8]={ 95 0b00000, 96 0b00000, 97 0b00011, 98 0b00011, 99 0b00000, 100 0b01111, 101 0b11111, 102 0b00000 103}; 104 105 106LiquidCrystal lcd(4,6,10,11,12,13); 107void setup() { 108 109 Serial.begin(9600); 110 pinMode(btn,INPUT); 111 lcd.begin(16,2); 112 lcd.createChar(0,runnin1); 113 lcd.createChar(1,runnin2); 114 lcd.createChar(2,jump); 115 lcd.createChar(3,def1); 116 lcd.createChar(4,def2); 117 lcd.createChar(5,slide); 118 buttonState=digitalRead(btn); 119} 120 121void loop() 122{ 123 124 intro(); 125 while(game==1) 126 ballActive(); 127 endGame(); 128} 129 130 void intro() 131 { 132 score=0; 133 lcd.clear(); 134 lcd.print(introText[0]); //Instead of declaring introText, you can directly print the text with " ... " 135 lcd.setCursor(7,1); 136 lcd.print("By SRP888"); 137 delay(2000); 138 lcd.clear(); 139 lcd.print(introText[1]); 140 lcd.setCursor(0,1); 141 lcd.print("21-06-2018"); 142 delay(2000); 143 lcd.clear(); 144 while(button()!=1) //wait for the user to hit the button, display the text until that. 145 { 146 lcd.setCursor(0,0); 147 lcd.print(introText[2]); 148 delay(100); 149 } 150 if(button()==1) 151 game=1; 152 } 153 154 void reset() 155 { 156 if(obstaclePos%2==0) //Changing the display of the player from one pose to another to show he is running, smartly using obstaclePos. 157 { 158 lcd.clear(); 159 lcd.setCursor(0,1); 160 lcd.write(byte(0)); 161 } 162 else 163 { 164 lcd.clear(); 165 lcd.setCursor(0,1); 166 lcd.write(1); 167 } 168 } 169 170 void ballActive() 171 { 172 obsDelay=200; //initial delay for the defender 173 obstaclePos=15; //since it is a 16x2 lcd, and index starts from 0. 174 while(game==1) 175 { 176 reset(); 177 if(button()==1) //if button is pressed. 178 { 179 lcd.clear(); 180 lcd.setCursor(0,0); 181 lcd.write(2); //print the player jumping 182 obstacle(obstaclePos); //print the defender 183 delay(400); //This is to prevent the display disappearing fast, and going unnoticed. 184 lcd.clear(); 185 lcd.setCursor(0,1); 186 lcd.write(1); //print the player back on ground 187 obstacle(obstaclePos); //print the defender 188 189 } 190 else 191 { 192 if(obstaclePos!=0) //if the defender has not approached the player, then continue. 193 { 194 reset(); 195 obstacle(obstaclePos); 196 } 197 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. 198 { 199 game=0; 200 break; 201 } 202 } 203 obstaclePos--; //brings the defender closer to the player 204 if(obstaclePos<0) //if the player successfully dodged then, 205 { 206 obsDelay=obsDelay-20; //decrease the delay (increase speed), 207 obstaclePos=15; //bring the defender to starting position, 208 score++; //and increase the score by 1. 209 } 210 if(obsDelay==0) //if 10 players have been dodged (because 200/20=10), then end the game 211 game=0; 212 213 delay(obsDelay); 214 } 215 } 216int button() 217{ 218 int reading = digitalRead(btn); 219 // If the switch changed, due to noise or pressing: 220 if (reading != lastButtonState) { 221 // reset the debouncing timer 222 lastDebounceTime = millis(); 223 } 224 225 if ((millis() - lastDebounceTime) > debounceDelay) { 226 buttonState = reading; 227 if (buttonState == HIGH) { 228 lastButtonState = reading; 229 return 1; 230 } 231 } 232 lastButtonState = reading; 233} 234void obstacle(int i) 235{ 236 if(obstaclePos%2==0&& obstaclePos>2) //if defender is far away from the player, then show him running 237 { 238 lcd.setCursor(i,1); 239 lcd.write(3); 240 } 241 else if(obstaclePos%2!=0&& obstaclePos>2) 242 { 243 lcd.setCursor(i,1); 244 lcd.write(4); 245 } 246 else //if he is 2 steps close to the player, then show him sliding 247 { 248 lcd.setCursor(i,1); 249 lcd.write(5); 250 } 251} 252void endGame() 253{ 254 if(score==10) 255 { 256 lcd.clear(); 257 lcd.print("Congratulations!"); 258 lcd.setCursor(0,1); 259 lcd.print("You beat 'em all"); 260 delay(3000); 261 } 262 else if(score<3) 263 { 264 lcd.clear(); 265 lcd.print("You suck"); 266 lcd.setCursor(0,1); 267 lcd.print("Score: "); 268 lcd.print(score); 269 delay(3000); 270 } 271 else if(score<6) 272 { 273 lcd.clear(); 274 lcd.print("Not bad!"); 275 lcd.setCursor(0,1); 276 lcd.print("Score: "); 277 lcd.print(score); 278 delay(3000); 279 } 280 else 281 { 282 lcd.clear(); 283 lcd.print("Great!"); 284 lcd.setCursor(0,1); 285 lcd.print("Score: "); 286 lcd.print(score); 287 delay(3000); 288 } 289} 290 291 292
Downloadable files
Complete schematic
Complete schematic
Complete schematic
Complete schematic
Comments
Only logged in users can leave comments