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:
- 7:217810c90306
- Parent:
- 6:fa2928bcec17
- Child:
- 8:457183719776
--- a/main.cpp Tue Apr 14 22:56:49 2015 +0000 +++ b/main.cpp Wed Apr 15 15:30:45 2015 +0000 @@ -39,6 +39,9 @@ // player's score int score = 0; +// difficulty - number of pixels hazards incrememnt by +int fall = 2; + // global variables for movement (pixelNinja) int a1 = 22; int a2 = 24; @@ -57,7 +60,7 @@ int randX5; int randX6; -// global variable for random Y co-ordinates +// global variable for Y co-ordinates int randY1 = 0; int randY2 = 0; int randY3 = 0; @@ -273,6 +276,43 @@ lcd.drawRect(a5,37,0,0,1); lcd.drawRect(a8,38,0,0,1); } +// stops ninja going through walls +void ninjaBoundaries() +{ + if(a6 > 79 ) + a6 = 79; + if(a4 > 78) + a4 = 78; + if(a2 > 77) + a2 = 77; + if(a3 > 76) + a3 = 76; + if(a1 > 75) + a1 = 75; + if(a8 > 74) + a8 = 74; + if(a5 > 73) + a5 = 73; + if(a7 > 72) + a7 = 72; + + if(a6 < 11 ) + a6 = 11; + if(a4 < 10) + a4 = 10; + if(a2 < 9) + a2 = 9; + if(a3 < 8) + a3 = 8; + if(a1 < 7) + a1 = 7; + if(a8 < 6) + a8 = 6; + if(a5 < 5) + a5 = 5; + if(a7 < 4) + a7 = 4; +} // resets back to initial values void resetGame() @@ -305,7 +345,6 @@ randY5 = 0; randY6 = 0; lcd.clear(); - } // draws falling hazards @@ -327,12 +366,12 @@ { // increments randY1 variables // appearing to make them fall - randY1 = randY1+=2; - randY2 = randY2+=2; - randY3 = randY3+=2; - randY4 = randY4+=2; - randY5 = randY5+=2; - randY6 = randY6+=2; + randY1 = randY1 += fall; + randY2 = randY2 += fall; + randY3 = randY3 += fall; + randY4 = randY4 += fall; + randY5 = randY5 += fall; + randY6 = randY6 += fall; // loops the objects once they 'hit the floor' // this imitates a new set of objects falling @@ -383,6 +422,31 @@ lcd.refresh(); } +// clears old pixels and keeps set pixels +void refreshCursor2() +{ + for (int i=70; i<80; i++)// loops through rows + for (int j=32; j<39; j++) + if (cells[i][j]) {// if there's a pixel then keep it + lcd.setPixel(i,j); + } else { + lcd.clearPixel(i,j);// else remove the old ones + } + lcd.refresh(); +} + +// clears old pixels and keeps set pixels +void refreshCursor3() +{ + for (int i=70; i<80; i++)// loops through rows + for (int j=25; j<31; j++) + if (cells[i][j]) {// if there's a pixel then keep it + lcd.setPixel(i,j); + } else { + lcd.clearPixel(i,j);// else remove the old ones + } + lcd.refresh(); +} // sound / light when buttonA is closed void actionButton() { @@ -414,6 +478,7 @@ // Power indicator - RED LED ledR = 1; + // helps return exit menu to main menu loop int exitFlag = 0; // initialize time seed for psuedo randomaisation @@ -429,25 +494,20 @@ randX5 = rand() % 74 + 5; randX6 = rand() % 74 + 5; -// char buffer[14]; -// int length = sprintf(buffer,"Level:%2d",score);//insert int level + calibrateJoystick();//get centred values of joystick + pollJoystick.attach(&updateJoystick,1.0/10.0);//read joystick 10 times per second - calibrateJoystick(); // get centred values of joystick - pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second - - /////// initiate main menu ////////////////////////////////////////////////////////////// - + // initialise menu lcd.init();//initialise screen welcome();//welcome screen lcd.clear();//clear pixels - /////// Menu Selection Loop //////////////////////////////////////////////////////////// - // option counters int mainOption = 0;//counter for main menu int exitOption = 0;//counter for exit menu + int option = 0;//counter for options menu - // loop 1 - menu + // loop 1 - Main menu while(1) { mainMenu(); @@ -501,9 +561,6 @@ backButton(); backGround(); - // if (length <= 12) // if string < 12 characters - // lcd.printString(buffer,3,0);// display on screen - // loop 3 - Game loop while(1) { @@ -517,8 +574,8 @@ backButton(); pixelNinja();//set character - hazards();//initiates hazards - hazardFall();//increments hazards towards floor + // hazards();//initiates hazards + // hazardFall();//increments hazards towards floor if (printFlag) { //if flag set, clear flag and print joystick values to serial port printFlag = 0; @@ -535,6 +592,7 @@ a6 = a6-=2; a7 = a7-=2; a8 = a8-=2; + ninjaBoundaries(); } if (joystick.direction == RIGHT) { serial.printf(" RIGHT\n"); @@ -546,6 +604,7 @@ a6 = a6+=2; a7 = a7+=2; a8 = a8+=2; + ninjaBoundaries(); } if (joystick.direction == CENTRE) serial.printf(" CENTRE\n"); @@ -597,13 +656,13 @@ lcd.drawRect(55,25,0,4,1); lcd.drawRect(56,26,0,2,1); lcd.drawRect(57,27,0,0,1); - + // option arrow - left lcd.drawRect(27,25,0,4,1); lcd.drawRect(26,26,0,2,1); lcd.drawRect(25,27,0,0,1); - + while(1) { @@ -689,8 +748,8 @@ while(1) { - actionButton(); - backButton(); + actionButton();//select + backButton();//back // back to menu if(buttonB == 1) { @@ -709,17 +768,79 @@ lcd.clear();//clear screen + backGround(); 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 + lcd.printString("Difficulty",3,9); + lcd.printString("Sound FX",3,10); while(1) { actionButton(); backButton(); + // refreshCursor(); + + // joystick selection + 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); + + // option up + if (joystick.direction == UP) { + serial.printf(" UP\n"); + option = option--; + if (option < 0)option = 0; + } + // option down + if (joystick.direction == DOWN) { + serial.printf(" DOWN\n"); + option = option++; + if (option > 1)option = 1; + } + // Centre / Unknown orientation + if (joystick.direction == CENTRE) + serial.printf(" CENTRE\n"); + if (joystick.direction == UNKNOWN) + serial.printf(" Unsupported direction\n"); + + // 'Difficulty' option 1 + if (option == 0) { + lcd.drawCircle(72,35,2,1); + refreshCursor3(); + } + // 'Sound FX' option 2 + if (option == 1) { + lcd.drawCircle(72,27,2,1); + refreshCursor2(); + } + +////////////////////// difficulty settings //////////////////////////////////// + if ((option == 1)&&(buttonA == 1)) { + + lcd.clear(); + backGround(); + lcd.drawRect(0,47,84,0,1);//bottom border + lcd.drawRect(0,0,84,2,1);//top border + lcd.printString("*Difficulty*",5,7);//title + lcd.printString("Easy",5,8);//title + lcd.printString("Normal",5,9);//title + lcd.printString("Forget It",5,10);//title + + + while(1) { + + actionButton(); + backButton(); + + if(buttonB == 1) { + lcd.clear(); + break; + } + } + } + } // back to menu if(buttonB == 1) { @@ -731,7 +852,6 @@ } } - // read default positions of the joystick to calibrate later readings void calibrateJoystick() {