A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
Diff: tower.h
- Revision:
- 34:9dc844bdc776
- Parent:
- 33:bc730c9b4b1c
- Child:
- 35:9f55326c1b0a
--- a/tower.h Mon Apr 20 23:46:32 2015 +0000 +++ b/tower.h Tue Apr 21 14:34:45 2015 +0000 @@ -3,6 +3,7 @@ #define DIRECTION_TOLERANCE 0.05 #include <string> +#include "mbed.h" //using namespace :: std; @@ -37,6 +38,8 @@ // sound FX toggle int FX = 0; +int preDir; + // timer flags to check state of buttons int buttonFlagA = 0; int buttonFlagB = 0; @@ -51,14 +54,14 @@ int score = 0; //high score details -int highScore1 = 2; -int highScore2 = 1; -int highScore3 = 0; +int highScore1 ; +int highScore2 ; +int highScore3 ; //player initials -char player1initials[14]; -char player2initials[14]; -char player3initials[14]; +char player1initials[14] = {"1.AAA.....00"}; +char player2initials[14] = {"2.BBB.....00"}; +char player3initials[14] = {"3.CCC.....00"}; // difficulty - number of pixels hazards incrememnt by int fall = 2; @@ -98,9 +101,9 @@ }; typedef const struct State STYP; -int state1 = 0;//integer for changing state 1 -int state2 = 0;//integer for changing state 2 -int state3 = 0;//integer for changing state 3 +int state1 = 5;//integer for changing state 1 +int state2 = 3;//integer for changing state 2 +int state3 = 1;//integer for changing state 3 // array for Alphabet STYP fsm[27] = { @@ -869,8 +872,11 @@ // if any of the high scores are beaten, they are replaced. // player enters initials using struct void newScore() -{ +{ + char x,y,z; if(score >= highScore3) {//entry condition + buttonFlagA = 0; + buttonFlagB = 0; lcd.clear(); backGround(); @@ -887,7 +893,11 @@ if (printFlag) {//if flag set, clear flag,print joystick values printFlag = 0; // option up - if (joystick.direction == UP) { + + if(joystick.direction==CENTRE) { + preDir=0; + } + if (joystick.direction == UP && preDir==0) { serial.printf(" UP\n"); state1 = state1--; if (state1 < 0)state1 = 0; @@ -895,9 +905,10 @@ if (state2 < 0)state2 = 0; state3 = state3--; if (state3 < 0)state3 = 0; + preDir=1; } // option down - if (joystick.direction == DOWN) { + if (joystick.direction == DOWN && preDir==0) { serial.printf(" DOWN\n"); state1 = state1++; if (state1 > 26)state1 = 26; @@ -905,77 +916,103 @@ if (state2 > 26)state2 = 26; state3 = state3++; if (state3 > 26)state3 = 26; + preDir=1; } - if (joystick.direction == LEFT) { + if (joystick.direction == LEFT && preDir==0) { serial.printf(" LEFT\n"); initial = initial--; if (initial < 0)initial = 0; + preDir=1; } - if (joystick.direction == RIGHT) { + if (joystick.direction == RIGHT && preDir==0) { serial.printf(" RIGHT\n"); initial = initial++; if (initial > 2)initial = 2; - + preDir=1; + } // Centre / Unknown orientation if (joystick.direction == CENTRE) serial.printf(" CENTRE\n"); if (joystick.direction == UNKNOWN) serial.printf(" Unsupported direction\n"); + + } - char x[1];//create storage - char y[1];//for initials - char z[1]; +// char x[1];//create storage +// char y[1];//for initials +// char z[1]; + // if initial 3 display selected character if (initial == 0) { - int initial1 = sprintf(x,"%c",fsm[state1].output1);//insert into letter in x - lcd.printString(x,25,26);//print x buffer + x=fsm[state1].output1; + lcd.printChar(x,25,26); +// int initial1 = sprintf(x,"%c",fsm[state1].output1);//insert into letter in x +// lcd.printString(x,25,26);//print x buffer } // if initial 1 display selected character if(initial == 1) { - int initial2 = sprintf(y,"%c",fsm[state2].output2);//insert into letter in y - lcd.printString(y,37,26);//print y buffer + y=fsm[state2].output2; + lcd.printChar(y,37,26); + +// int initial2 = sprintf(y,"%c",fsm[state2].output2);//insert into letter in y +// lcd.printString(y,37,26);//print y buffer } // if initial 2 display selected character if(initial == 2) { - int initial3 = sprintf(z,"%c",fsm[state3].output3);//insert into letter z - lcd.printString(z,49,26);//print z buffer - } - // if player beats High Score 3, replace it with new score - if(score >= highScore3 && score<highScore2 ) { - n = score; - highScore3 = n; - // - int insert3 = sprintf (player3initials, "3.%s%s%s....%i",x,y,z,highScore3); + z=fsm[state3].output3; + lcd.printChar(z,49,26); + +// int initial3 = sprintf(z,"%c",fsm[state3].output3);//insert into letter z +// lcd.printString(z,49,26);//print z buffer } - // if player beats High Score 3 and 2, replace HighScore2 with new score - if(score >= highScore2 && score< highScore1) { - highScore3 = highScore2; - n = score; - highScore2 = n; - int insert2 = sprintf (player2initials, "2.%s%s%s....%i",x,y,z,highScore2); - } - // if player beats High Score 1, 2 and 3, replace highScore1 with new score - if(score >= highScore1 ) { - highScore3 = highScore2; - highScore2 = highScore1; - n = score; - highScore1 = n; - int insert1 = sprintf (player1initials, "1.%s%s%s....%i",x,y,z,highScore1); - } - // goes back to main menu after storing initials + if(buttonFlagA) { buttonFlagA = 0; buttonFlagB = 0; break; } } + // if player beats High Score 3, replace it with new score + if(score >= highScore3 && score<highScore2 ) { + //n = score; + highScore3 = score; + // + int insert3 = sprintf (player3initials, "3.%c%c%c.....%i",x,y,z,highScore3); + } + // if player beats High Score 3 and 2, replace HighScore2 with new score + if(score >= highScore2 && score< highScore1) { + int insert2; + highScore3 = highScore2; + n = score; + highScore2 = n; + insert2=sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3); + insert2 = sprintf (player2initials, "2.%c%c%c.....%i",x,y,z,highScore2); + } + // if player beats High Score 1, 2 and 3, replace highScore1 with new score + if(score >= highScore1 ) { + int insert1; + highScore3 = highScore2; + highScore2 = highScore1; + n = score; + highScore1 = n; + insert1=sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3); + insert1=sprintf (player2initials, "1.%c%c%c.....%i",player1initials[2],player1initials[3],player1initials[4],highScore2); + insert1 = sprintf (player1initials, "1.%c%c%c.....%i",x,y,z,highScore1); + } + // goes back to main menu after storing initials +// if(buttonFlagA) { +// buttonFlagA = 0; +// buttonFlagB = 0; +// break; +// } } } + // actual game void game(int& exitFlag, int& exitOption) { @@ -1094,7 +1131,7 @@ buttonFlagB = 0; break; } - sleep();//put while to sleep + //sleep();//put while to sleep } // if 'exit' option YES, resets // game values returns to main menu @@ -1133,7 +1170,7 @@ lcd.clear(); break; } - sleep();//put while to sleep + //sleep();//put while to sleep } } @@ -1167,7 +1204,7 @@ lcd.clear();//clear screen break;//return back } - sleep();//put while to sleep + //sleep();//put while to sleep } drawOptionsMenu(); } @@ -1189,7 +1226,7 @@ lcd.clear();//clear screen break;//return back } - sleep();//put while to sleep + //sleep();//put while to sleep } drawOptionsMenu(); } @@ -1200,7 +1237,7 @@ lcd.clear();//clear break;//return back } - sleep();//put while to sleep + //sleep();//put while to sleep } }