A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
Diff: main.cpp
- Revision:
- 4:c6305031f80d
- Parent:
- 3:57bab3b1c974
- Child:
- 5:a0e4a67266d6
--- a/main.cpp Mon Apr 13 20:59:14 2015 +0000 +++ b/main.cpp Tue Apr 14 02:06:45 2015 +0000 @@ -28,12 +28,18 @@ AnalogIn xPot(p15); AnalogIn yPot(p16); +// create local file system +//LocalFileSytem local("local"); + // boundary conditions int cells [84][48]; // Globabl Variables ///////////////////////// -// global variables for movement +// player's score +int score = 0; + +// global variables for movement (pixelNinja) int a1 = 22; int a2 = 24; int a3 = 23; @@ -206,7 +212,7 @@ lcd.drawRect(29,27,0,0,1); //present options - lcd.printString("Play Game",3,4); + //lcd.printString("Play Game",3,4); } @@ -273,39 +279,38 @@ } -// when player gets struck, game ends -void endGame() +// resets back to initial values +void resetGame() { - int n = 0; + score = 0; - // contact points - if(lcd.getPixel((a1+4),32)) - n++; - if(lcd.getPixel((a1),32)) - n++; - if(lcd.getPixel((a7),32)) - n++; + a1 = 22; + a2 = 24; + a3 = 23; + a4 = 25; + a5 = 20; + a6 = 26; + a7 = 19; + a8 = 21; - // play again option - if ( n !=0) { - lcd.printString("Game Over!!",8,25); - wait(0.5); - lcd.inverseMode(); - wait(0.5); - lcd.normalMode(); - wait(0.5); - lcd.inverseMode(); - wait(0.5); - lcd.normalMode(); - wait(0.5); - lcd.printString("Play Again?",8,25); - lcd.printString("YES",20,2); - lcd.printString("NO",51,2); - wait(3.0); + // in this case the X values are given a + // new random variable each time the player + // dies or exits and starts a new game + randX1 = rand() % 74 + 5; + randX2 = rand() % 74 + 5; + randX3 = rand() % 74 + 5; + randX4 = rand() % 74 + 5; + randX5 = rand() % 74 + 5; + randX6 = rand() % 74 + 5; - if(buttonA == 1) { - } - } + randY1 = 0; + randY2 = 0; + randY3 = 0; + randY4 = 0; + randY5 = 0; + randY6 = 0; + lcd.clear(); + } // draws falling hazards @@ -359,6 +364,8 @@ if (randY6>=48) { randY6=0; + score = score++; + randX1 = rand() % 74 + 5;// psuedo random number randX2 = rand() % 74 + 5;// between 5 and 74 randX3 = rand() % 74 + 5; @@ -403,7 +410,7 @@ } } -// MAIN ///////////////////////////////////////////////// +//// MAIN ///////////////////////////////////////////////// int main() { @@ -433,7 +440,6 @@ lcd.init();//initialise screen welcome();//welcome screen lcd.clear();//clear pixels - mainMenu();//menu screen /////// Menu Selection Loop //////////////////////////////////////////////////////////// @@ -443,6 +449,8 @@ // loop 1 - menu while(1) { + mainMenu(); + // joystick selection if (printFlag) { //if flag set, clear flag and print joystick values to serial port printFlag = 0; @@ -458,7 +466,7 @@ if (joystick.direction == DOWN) { serial.printf(" DOWN\n"); option = option++; - if (option > 1)option = 1; + if (option > 2)option = 2; } // Centre / Unknown orientation if (joystick.direction == CENTRE) @@ -470,73 +478,161 @@ if (option == 0) { lcd.printString("Play Game",3,4); } - // 'Options' option 2 + // 'High Scores' option 2 if (option == 1) { + lcd.printString(" Scores ",3,4); + } + // 'Options' option 3 + if (option == 2) { lcd.printString(" Options ",3,4); } } + //////////// Game Loop ///////////////////////////////////////////////////////////// // 'Play Game' selected if ((option == 0)&&(buttonA == 1)) { + lcd.clear(); + + actionButton(); + backButton(); backGround(); - // loop 2 - Exit menu + // loop 3 - Game loop while(1) { - // loop 3 - Game loop - while(buttonB != 1) { + actionButton(); + backButton(); + + pixelNinja();//set character + hazards();//initiates hazards + hazardFall();//increments hazards towards floor + + if (printFlag) { //if flag set, clear flag and print joystick values to serial port + printFlag = 0; + serial.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button); - pixelNinja();//set character - hazards();//initiates hazards - hazardFall();//increments hazards towards floor + // check joystick direction + if (joystick.direction == LEFT) { + serial.printf(" LEFT\n"); + a1 = a1-=2; + a2 = a2-=2; + a3 = a3-=2; + a4 = a4-=2; + a5 = a5-=2; + a6 = a6-=2; + a7 = a7-=2; + a8 = a8-=2; + } + if (joystick.direction == RIGHT) { + serial.printf(" RIGHT\n"); + a1 = a1+=2; + a2 = a2+=2; + a3 = a3+=2; + a4 = a4+=2; + a5 = a5+=2; + a6 = a6+=2; + a7 = a7+=2; + a8 = a8+=2; + } + if (joystick.direction == CENTRE) + serial.printf(" CENTRE\n"); + if (joystick.direction == UNKNOWN) + serial.printf(" Unsupported direction\n"); - if (printFlag) { //if flag set, clear flag and print joystick values to serial port - printFlag = 0; - serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button); + // escape code (character hit) + int x = 0; + + // contact points + if(lcd.getPixel((a1+4),32)) + x++; + if(lcd.getPixel((a1),32)) + x++; + if(lcd.getPixel((a7),32)) + x++; - // check joystick direction - if (joystick.direction == LEFT) { - serial.printf(" LEFT\n"); - a1 = a1-=2; - a2 = a2-=2; - a3 = a3-=2; - a4 = a4-=2; - a5 = a5-=2; - a6 = a6-=2; - a7 = a7-=2; - a8 = a8-=2; - } - if (joystick.direction == RIGHT) { - serial.printf(" RIGHT\n"); - a1 = a1+=2; - a2 = a2+=2; - a3 = a3+=2; - a4 = a4+=2; - a5 = a5+=2; - a6 = a6+=2; - a7 = a7+=2; - a8 = a8+=2; - } - if (joystick.direction == CENTRE) - serial.printf(" CENTRE\n"); - if (joystick.direction == UNKNOWN) - serial.printf(" Unsupported direction\n"); + // game over + if ( x !=0) { + lcd.printString("Game Over!!",8,25); + wait(0.5); + lcd.inverseMode(); + wait(0.5); + lcd.normalMode(); + wait(0.5); + lcd.inverseMode(); + wait(0.5); + lcd.normalMode(); + wait(0.5); + resetGame(); + break; + } + startrek();//clears unset pixels, keeps set pixels + + // escape code (Back button pressed) + if(buttonB == 1) { + backButton(); + resetGame();//resets co-ordinates + break; + } + serial.printf("Score: %i \n",score); + + }//if + }//while + }//if + +////////// High Scores Loop ////////////////////////////////////////////////// + + if((option == 1)&&(buttonA == 1)) { - //endGame(); - startrek();//clears unset pixels, keeps set pixels + actionButton(); + backButton(); + + lcd.clear();//clear screen + lcd.drawRect(3,6,77,10,0);//title outline + backGround();//set background + lcd.printString("High Scores",10,7);//title + + while(1) { - } - + actionButton(); + backButton(); + + // back to menu + if(buttonB == 1) { + lcd.clear(); + break; + } + } + } + +////////// OPTIONS LOOP ///////////////////////////////////////////////////// - // break code e.g. person dies / exits game - if (buttonB == 1) { + if((option == 2)&&(buttonA == 1)) { + + actionButton(); + backButton(); - lcd.printString("Exit Game?",9,25); - if(buttonA == 1) - break; -} + lcd.clear();//clear screen + + lcd.drawRect(3,6,77,10,0);//title outline + lcd.drawRect(0,47,84,0,1);//bottom border + lcd.drawRect(0,0,84,2,1);//top border + lcd.printString("Options",20,7);//title + lcd.printString("Difficulty",5,3);//difficulty menu + lcd.printString("Sound FX",5,4);//sounf fx on / off + + int o = 0; + + while(1) { + + actionButton(); + backButton(); + + // back to menu + if(buttonB == 1) { + lcd.clear(); + break; } } }