Peter's version

Fork of ScoreCount by George Cochrane

main.cpp

Committer:
gcme93
Date:
2013-05-01
Revision:
2:289c6ccc4e86
Parent:
1:d41b5b18175b
Child:
3:8e42915e7375

File content as of revision 2:289c6ccc4e86:

#include "mbed.h"
#include "TSISensor.h"
#include "TextLCD.h"


//INITIAL PARAMETERS
int hit=0;
int lives=10;       //How many lives to start game with
float a;
TSISensor tsi;

//RGB LED SETUP
DigitalOut Red(PTC6);
DigitalOut Blue(PTC10);
DigitalOut Green(PTC11);

//LCD SCREEN SETUP
TextLCD lcd(PTD7, PTD6, PTA17, PTA16, PTC17, PTC16); // rs, e, d4-d7



int main() 
{
while(1) 
{
    
    lcd.cls();
    
    while (lives>=0)
    {
    Blue=1;     //Blue light always on when playing
    Red=0;      //Red light off when no hits registered
    
    if (lives==0)
    {
    Red=1;      //Magenta if no lives remain
    }
   
    
    
    //TRIGGER FOR HIT (CURRENTLY TOUCH PAD)
    a=tsi.readPercentage();
    
    if (a>=0.01)
    {hit=1;}
    else
    {hit=0;}

    
    //Print no. of lives
    lcd.locate(0,0);
    lcd.printf("Lives remaining: %d \n", lives);


                if (hit==1)
                {
                unsigned char i;
       
                
                lives--;
                
                if(lives<0)
                {
                goto end;        //Skip to the end (avoids read out of -1 lives)
                }
                
                //Update lives on LCD
                lcd.locate(0,0);
                lcd.printf("Lives remaining: %d \n", lives);
                
                //Flash to register hit
                for(i=0;i<4;i++)            //INCREASE i TO EXTEND TIME THAT GUN IS DISABLED! Maybe later add that being hit disables your own trigger?
                    {
                    Blue=0;
                    Red=1;
                    wait(0.2);
                    Red=0;
                    Blue=1;
                    wait(0.2);                   
                    }
                }
    
    }
    
end:

Blue=0;
lcd.cls();
lcd.locate(0,0);
lcd.printf(" - GAME  OVER - ...you suck");


unsigned char i;
for(i=0;i<50;i++)           //Flashing to indicate game over - INCREASE i FOR LONGER BREAK BEFORE RESET (perhaps button to reset, not auto reset?)
                    {
                    Red=1;
                    wait(0.05);
                    Red=0;
                    wait(0.05); 
                    }
                    
//Pretty green light and resetting
Green=1;  
lives= 10;
wait(2);    
Green=0;        
lcd.cls(); 
}
}