![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 6:bf601a65cb27
- Parent:
- 5:1bf7c83f86cc
- Child:
- 7:887651afda26
diff -r 1bf7c83f86cc -r bf601a65cb27 main.cpp --- a/main.cpp Thu May 04 09:50:18 2017 +0000 +++ b/main.cpp Thu May 04 13:04:14 2017 +0000 @@ -8,6 +8,7 @@ #include "Obstacles.h" #include "Gems.h" #include "SDFileSystem.h" +#include "FXOS8700CQ.h" #define No_OBS 8 @@ -38,6 +39,8 @@ SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); FILE *file; +FXOS8700CQ device(I2C_SDA,I2C_SCL); +Data values; /* Function Prototypes */ @@ -57,9 +60,11 @@ /* Intialization */ + //fprintf("Entering init() from main"); init(); -/* Drawing the initial frame */ +/* Drawing the initial frame */ + //fprintf("Entering the Welcome() function from main"); welcome(); } @@ -67,6 +72,8 @@ void init() { + +//fprintf("in init()"); //Need to initialize the lcd and gamepad lcd.init(); pad.init(); @@ -77,12 +84,14 @@ //Intialzing the obstacles for(i=0;i<No_OBS;i++) { + //fprintf("Obstacle intialised"); obstacle[i].init(); } //Intialzing the gems for(j=0;j<No_GEMS;j++) { + //fprintf("Gems initalised"); gems[j].init(); } @@ -97,7 +106,7 @@ { - + //fprintf("In welcome()"); lcd.printString("Stick Runner! ",0,1); lcd.printString(" Press Start ",0,4); lcd.refresh(); @@ -114,7 +123,7 @@ wait(0.1); } - + //fprintf("Entering menu() from wlelcome"); menu(); } @@ -125,7 +134,7 @@ { - + //fprintf("In menu"); lcd.clear(); lcd.printString(" Menu ",0,0); lcd.printString("A)New Game ",0,2); @@ -147,8 +156,10 @@ //Clear, refresh and intialize the game again so we can start a new game lcd.clear(); + //fprintf("Entering init from menu()"); lcd.refresh(); init(); + //fprintf("Entering the game when A is pressed"); stickRunner(); @@ -164,6 +175,12 @@ //Simply refreshes the page and continues from where the user left the game // as the intialize function init() is not called again lcd.refresh(); + + pad.led(2,0); + pad.led(5,0); + + + //fprintf("Entering the game when B is pressed from menu"); stickRunner(); @@ -173,21 +190,28 @@ else if( pad.check_event(Gamepad::X_PRESSED) ) { // pad.tone(1000.0,0.5); + //fprintf("entering instructions when X is pressed from menu"); Instructions(); } + //To see the game high score else if( pad.check_event(Gamepad::Y_PRESSED) ) { //pad.tone(1000.0,0.5); + //fprintf("Displayibng highscore when Y is pressed from menu"); displayHighScore(); } + + + + sleep(); @@ -200,7 +224,8 @@ //This function is responsible for running the game void stickRunner() { - int fps = 8; + //fprintf("IN the game(stickrunner())"); + int fps = 10; render(); wait(1.0f/fps); @@ -209,35 +234,56 @@ while (1) { + lcd.setBrightness(pad.read_pot()); + //fprintf("printing out the value of counter before updating in each iteration %d",counter); //As long as the character survives update the score counter++; + //fprintf("printing out the value of counter after updating in each iteration %d",counter); //Using the gamepad library to move the character using the joystick + //fprintf("Calling UpdateCharacter from the game func to make it move"); c.updateCharacter(pad.get_direction(),pad.get_mag()); + //values = device.get_values(); + // c.accMove(values); + //Condition to ckeck if the user wants to pause the game if(pad.check_event(Gamepad::BACK_PRESSED)) { lcd.clear(); - lcd.refresh(); + lcd.refresh(); + + + pad.led(2,1); + pad.led(5,1); + + + + //fprintf("Game paused and sent to menu from the game when BACK is pressed"); menu(); } - //Loop to make the generation of obstacles a continious loop and also to check if the user has been killed + //Loop to make the generation of obstacles a continious loop by checking the status and also to check if the user has been killed for(i=0;i<No_OBS;i++) - { + { //fprintf("in to loop to check when to initialise the obstacles and "); + //To retrieve the status of the obstacle on the screen + //fprintf("Obstacle Status called from stickrunner()"); obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); + //fprintf("Value returned from obstacle status %d", obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); if(obstacle[i].getObstacleStatus() == false) { + //fprintf("Init clalled from stickrunner() in obstaccle"); obstacle[i].init(); } //To check whether the character has been hit by an obstacle by comparing the position of each obstacle // relative to the character + //fprintf("Character status called from stickrunner() in obstacle loop"); c.characterStatus(obstacle[i].getObstaclePos()); + //fprintf("the value returned by character status %d", c.characterStatus(obstacle[i].getObstaclePos())); } @@ -245,16 +291,21 @@ //Loop to make the generation of gems a continious loop and also to check if the user has collected them for(j=0;j<No_GEMS;j++) { + //fprintf("in to loop to check when to initialise the gems and "); + //To check whether the character has collected a gem by comparing the position of each gem // relative to the character + //fprintf("Gems Status called from stickrunner()"); gems[j].gemStatus(c.getCharacterPos()); - - + //fprintf("Value returned from gem status %d", gem[i].gemStatus(obstacle[i].getGemPos()); if(gems[j].getGemStatus() == false) { + + //fprintf("Init clalled from stickrunner() in gem"); gems[j].init(); + } @@ -267,6 +318,8 @@ for(i=0;i<No_OBS;i++) { + + //fprintf("update obstacle clalled from stickrunner()"); obstacle[i].updateObstacle(); } @@ -275,10 +328,12 @@ for(j=0;j<No_GEMS;j++) { + //fprintf("update gem clalled from stickrunner()"); gems[j].updateGems(); } - + + //fprintf("render called from stickrunner()"); render(); wait(1.0f/fps); @@ -290,18 +345,20 @@ //Function to draw out the pixels on the screen void render() { - + //fprintf("In render"); lcd.clear(); //Only draws the character as long as it survives if(c.getCharacterStatus()) { + //fprintf("Character drawn"); c.draw(lcd); } if(c.getCharacterStatus() == false) { + //fprintf("over called from render()"); over(); } @@ -312,6 +369,7 @@ { if(obstacle[i].getObstacleStatus()) { + //fprintf("obstacle drawn"); obstacle[i].draw(lcd); } } @@ -321,6 +379,7 @@ { if(gems[j].getGemStatus()) { + //fprintf("gem drawn"); gems[j].draw(lcd); } @@ -338,56 +397,27 @@ //Function to display end of game and also check whether the user got a new highscore and if not write it on the SD card void over() { + //fprintf("In over()"); + + + //pad.tone(1000.0,0.5); pad.init(); - // lcd.init(); - // lcd.clear(); + - //Mounting on the SD card to read/write in it - //sd.mount(); - + //Converting the counter into a string 'score' to display on the lcd sprintf (score, " Score : %d",counter); + //fprintf("Counter converted to string %s",score); lcd.printString(score,0,2); lcd.printString("GAME OVER!! ",0,0); - //lcd.printString(" ",0,1); - - //Opening file on the SD card - /*file = fopen("/sd/scoreFile.txt", "r"); - - //If file is empty and score to it and display it as the High Score - if(file ==NULL) - { - file = fopen("/sd/scoreFile.txt", "w"); - fprintf(file,"%d",counter); - fclose(file); - lcd.printString("HIGH SCORE",0,3); - - } - //if not empty compare against the exsisting high score and display whether the user has made a new high score - else - { - fscanf(file,"%d", &highScore); - fclose(file); - - if(counter>highScore) - { - - file = fopen("/sd/scoreFile.txt", "w"); - fprintf(file,"%d",counter); - fclose(file); - lcd.printString("HIGH SCORE",0,3); - } - - }*/ - - + lcd.printString(" PRESS START ",0,5); lcd.refresh(); - //sd.unmount(); + //Takes the user back to the main for a new game while ( pad.check_event(Gamepad::START_PRESSED) == false) @@ -400,6 +430,7 @@ wait(0.1); if( pad.check_event(Gamepad::START_PRESSED)) { + //fprintf("main called from over() to start a new game"); main(); wait(1); } @@ -411,55 +442,41 @@ //Function to display the current High score fo the game and also reset it to 0 void displayHighScore() { - sd.mount(); + //fprintf("In highscore()"); + + lcd.clear(); //Open file - file = fopen("/sd/scoreFile.txt", "r"); - if(file ==NULL) - { - highScore = 0; + if(highScore <= counter) + { + highScore = counter; + //fprintf("value of highscore from if %d", highScore); + lcd.printString(" BACK - menu ",0,5); + } - } - else - { - //Read the high score from the file - fscanf(file,"%d", &highScore); - fclose(file); - - } + else + { + highScore = highScore; + + } //Convert highscore(int) to score(String) to print on the lcd - sprintf (score, "High Score : %d",highScore); - - lcd.printString(score,0,2); - lcd.printString(" START - reset ",0,4); + sprintf (score, " %d",highScore); + //fprintf("value of score when updated with highscore %s", score); + lcd.printString("High Score :",0,2); + lcd.printString(score,0,3); + lcd.printString(" BACK - menu ",0,5); lcd.refresh(); sd.unmount(); while(1) { - //To reset the highscore - if( pad.check_event(Gamepad::START_PRESSED)) - { - sd.mount(); - file = fopen("/sd/scoreFile.txt", "r"); - if(!file ==NULL) - { - //Delete the file if it is empty - fclose(file); - remove("/sd/scoreFile.txt"); - - } - sd.unmount(); - displayHighScore(); - - } - //Back to menu if( pad.check_event(Gamepad::BACK_PRESSED)) { + //fprintf("menu called from highscore when back is pressed"); menu(); } @@ -475,6 +492,7 @@ //Function to display the Instructions for the game void Instructions() { + //fprintf("in instructions()"); bool i = true; lcd.clear(); lcd.printString("INSTURCTIONS: ",0,0); @@ -491,6 +509,8 @@ { //pad.tone(1000.0,0.5); i = false; + + //fprintf("menu called from instructions when back pressed"); menu(); } }