Game with the IR sensor.

Dependencies:   mbed

Fork of Ticker_HelloWorld by mbed official

main.cpp

Committer:
doctorgrue
Date:
2015-10-22
Revision:
1:a0bf30d5af2f
Parent:
0:5014bf742e9b

File content as of revision 1:a0bf30d5af2f:

#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

//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;

int nm2=0;
int stage2=0;

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) {
        dodged=0; //resets dodged
        gameOver=0; //removes game over flag
        tier=1;
        oldTier=1;
        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();
        
        while(gameOver==0){
            height=int(distance*100);
            if(height>30) tier = 3;
            else if(height>15) tier = 2;
            else if(height<=15) tier = 1;
            //uLCD.printf("%d",tier);
            wait(0.1);
            if(oldTier!=tier){
                    uLCD.filled_circle(10, 33*oldTier, 30, BLACK); //delete old
                    oldTier=tier;
                    }
            if(tier==1){
                uLCD.filled_circle(10, 33, 10, BLUE); //x,y,size,color
                }
            if(tier==2){
                uLCD.filled_circle(10, 66, 10, BLUE);
                }
            if(tier==3){
                uLCD.filled_circle(10, 99, 10, BLUE);
                }
                
            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);
                    }
                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++;
                    }
                }
            if(stage1==0){
                stage1++;
                nm1=rand()%3+1;
                //if(nm1==nm2) nm1++;
                uLCD.filled_circle(100, 33*nm1, 15, RED);
               // uLCD.filled_rectangle(90, 20, 20, 20, RED);//length,height,x,y,color
                }
                
            /*if(stage2>0){
                if(stage2==1){
                    nm2=rand()%3+1;
                    if(nm2==nm1) nm2--;
                    uLCD.filled_circle(100, 33*nm2, 15, RED);
                    }
                stage2++;
                if(stage1==10) uLCD.filled_circle(100, 33*nm1, 15, YELLOW);
                if(stage1==20) uLCD.filled_circle(100, 33*nm1, 15, WHITE);
                if(stage1==35) {
                    stage2=0;
                    uLCD.filled_circle(100, 33*nm1, 15, BLACK);}
                }*/
                
            } 
             uLCD.cls();
             uLCD.printf("GAME OVER\n");
             uLCD.printf("Lasers dodged:%d\n",dodged);
             wait(3);
             
            
            
        
    }  
}