IR Sensor and uLCD Dodging Game
Description
This is a game in which the IR sensor is used to control the player character in order to dodge lasers. Circles on the right hand side of the uLCD screen change from red to yellow and then finally white as they charge up. They then fire lasers that you must dodge with the player character on the left. The distance of your hand from the IR sensor governs the height of the character. Each dodged shot is counted and added to your final score.
There are three possible heights for your character and the enemy laser. The farther your hand is from the sensor, the higher on the screen your character is.
Video Demo
Wiring
Required Parts:
- mbed NXP LPC1768 or equivalent
- Sharp IR distance sensor
- 4D Systems uLCD mini screen
- Pinout Table*
Mbed | Part |
---|---|
VU | IR Red and uLCD +5V |
Gnd | IR Black and uLCD Gnd |
p20 | IR Yellow |
p27 | uLCD Rx |
p28 | uLCD Tx |
p30 | uLCD Res |
Troubleshooting!
The wires leading from the IR sensor may need to be tipped with tin using a soldering iron in order to connect them to the board effectively.
Dodged
As seen above, avoid being on the same level as the charging circle in order to dodge the fired laser.
Shot
If the red laser hits your character as shown in the picture above, the game ends and displays your score.
Code Repository
Import programIRSensorGame
Game with the IR sensor.
IR Game Main.cpp
/**Code that runs a dodging game with IR distance sensing controls*/ #include "mbed.h" #include "uLCD_4DGL.h" #include "rtos.h" uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin for uLCD; AnalogIn distance(p20); //Read IR distance from pin 20 //Main variables int height; //which height the player is on int tier; int oldTier; int dodged; //the number dodged int gameOver; //Whether the player has lost int nm1=0; int stage1=0; /** Main code. * Begins by initializing uLCD screen and then enters an endless while loop. */ int main() { uLCD.baudrate(1000000); //baud rate max for faster display uLCD.background_color(BLACK); uLCD.text_width(2); //4X size text uLCD.text_height(2); while (true) { ///Resets values between games. dodged=0; //Numbe of lasers dodged gameOver=0; //Flags whether the game over state should be entered tier=1; //The height of the character oldTier=1; //Old height of the character last iteration ///Begins the game with instructions and a count down uLCD.cls(); uLCD.printf("Dodge the lasers. \nGame begins in \n3...\n"); wait(1); uLCD.printf("2...\n"); wait(1); uLCD.printf("1\n"); wait(1); uLCD.printf("Start"); wait(0.3); uLCD.cls(); ///Main game loop that runs until the character is hit while(gameOver==0){ ///Find the new character location based on analog IR sensor input height=int(distance*100); if(height>30) tier = 3; else if(height>15) tier = 2; else if(height<=15) tier = 1; ///Wait between loops to slow the game down to something playable wait(0.1); ///Erase the old character circle if the character has moved if(oldTier!=tier){ uLCD.filled_circle(10, 33*oldTier, 30, BLACK); //delete old oldTier=tier; } ///Place the character based on new location uLCD.filled_circle(10, 33*tier, 10, BLUE); //x,y,size,color ///Handle the enemy state after it has spawned. Cover charging and firing lasers if(stage1>0){ stage1++; if(stage1==7) uLCD.filled_circle(100, 33*nm1, 15, YELLOW); if(stage1==14) { //stage2=1; uLCD.filled_circle(100, 33*nm1, 15, WHITE); } ///Checks to see if the fired laser hit the character and updates gameOver flag if(stage1==20) { stage1=0; uLCD.filled_circle(100, 33*nm1, 15, BLACK); uLCD.line(0, 33*nm1, 100, 33*nm1, RED); wait(0.2); uLCD.line(0, 33*nm1, 100, 33*nm1, BLACK); if(nm1==tier)gameOver=1; else dodged++; } } ///Spawns a new laser charger if one does not exist if(stage1==0){ stage1++; nm1=rand()%3+1; uLCD.filled_circle(100, 33*nm1, 15, RED); } } ///Game Over screen that displays score if the game has ended uLCD.cls(); uLCD.printf("GAME OVER\n"); uLCD.printf("Lasers dodged:%d\n",dodged); ///Wait 3 seconds before restarting the game wait(3); } }
Please log in to post comments.