
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Revision 6:bf601a65cb27, committed 2017-05-04
- Comitter:
- el15ss
- Date:
- Thu May 04 13:04:14 2017 +0000
- Parent:
- 5:1bf7c83f86cc
- Child:
- 7:887651afda26
- Commit message:
- StickRunner v1.0;
Changed in this revision
--- a/Character/Character.cpp Thu May 04 09:50:18 2017 +0000 +++ b/Character/Character.cpp Thu May 04 13:04:14 2017 +0000 @@ -4,6 +4,8 @@ void Character::init() { + //fprintf("in character init"); + //Initializing the X and Y co ordinates of the character charPosX = 42; @@ -19,6 +21,7 @@ //Drawing the character + //fprintf("character being drawn"); lcd.setPixel(charPosX,charPosY); lcd.setPixel(charPosX-1,charPosY); lcd.setPixel(charPosX+1,charPosY); @@ -49,6 +52,7 @@ //Function to move the character using the joystick void Character::updateCharacter(Direction d,float mag) { + //fprintf("updating th character speed to make it move"); _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future // update y value depending on direction of movement @@ -123,9 +127,11 @@ } + //Returns the postion (x,y) of the character on the screen Vector2D Character::getCharacterPos() { + //fprintf("in character get pos"); Vector2D p = {charPosX,charPosY}; return p; } @@ -134,6 +140,7 @@ //Function to check if the character has been hit by a obstacle and update the status if yes void Character::characterStatus(Vector2D p) { + //fprintf("IN character status"); //Using the dimensions of the character we check if there any pixels near it if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y))) { @@ -145,6 +152,11 @@ //Function to return the status of the character bool Character::getCharacterStatus() { + + //fprintf("in get character status"); + + //fprintf("The value of character status is (in 0/1) %d",charStatus); + //Used to determine whether the character has been and hit and charstatus returns false the game is over return charStatus;
--- a/Character/Character.h Thu May 04 09:50:18 2017 +0000 +++ b/Character/Character.h Thu May 04 13:04:14 2017 +0000 @@ -4,6 +4,8 @@ #include "mbed.h" #include "N5110.h" #include "Gamepad.h" +#include "FXOS8700CQ.h" + /** Class Character @brief Class responsible for all the functionality of the charachter including intialization, drawing, moving and updating it @author Samrudh Sharma @@ -60,6 +62,8 @@ */ bool getCharacterStatus(); + + private: //Variables
--- a/FXOS8700CQ.lib Thu May 04 09:50:18 2017 +0000 +++ b/FXOS8700CQ.lib Thu May 04 13:04:14 2017 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/trm/code/FXOS8700CQ/#e2fe752b881e +https://mbed.org/users/eencae/code/FXOS8700CQ/#1a98f69712e8
--- a/Gems/Gems.cpp Thu May 04 09:50:18 2017 +0000 +++ b/Gems/Gems.cpp Thu May 04 13:04:14 2017 +0000 @@ -3,6 +3,8 @@ void Gems::init() { + //fprintf("in gem init"); + //Initializing the X and Y co ordinates of the character gemPosX = rand() % 84; gemPosY = rand() % 42-42; @@ -16,6 +18,7 @@ { //Drawing the gems + //fprintf("gem being drawn"); lcd.setPixel(gemPosX,gemPosY); lcd.setPixel(gemPosX+1,gemPosY); lcd.setPixel(gemPosX-1,gemPosY); @@ -34,6 +37,7 @@ void Gems::updateGems() { //Updating the position of the gem on the screen and setting its speed + //fprintf("updating th gem to make it move"); gemPosY =gemPosY+2; } @@ -41,6 +45,7 @@ void Gems::gemStatus(Vector2D p) { + //fprintf("in gem status"); //Loop to check if a gem has touched the character and update its staus to make it siappear from the screen if(((gemPosX>p.x-5)&&(gemPosX<p.x+5))&&(gemPosY>p.y)) { @@ -48,7 +53,7 @@ gStatus = false; } - + //fprintf("gem status vale after checking if consumed %d",gStatus ); //To check if the gem has reached the bottom of the screen so we can intialise and render again if(gemPosY > HEIGHT) @@ -62,6 +67,7 @@ Vector2D Gems::getGemPos() { + //fprintf("in gem get pos"); Vector2D p = {gemPosX,gemPosY}; return p; } @@ -69,6 +75,11 @@ //Returns the status of the obstacle bool Gems::getGemStatus() { + + //fprintf("in get gem status"); + + //fprintf("The value of gem status is (in 0/1) %d",gStatus); + //Used to check when to initialise and render the gem return gStatus; }
--- a/Obstacles/Obstacles.cpp Thu May 04 09:50:18 2017 +0000 +++ b/Obstacles/Obstacles.cpp Thu May 04 13:04:14 2017 +0000 @@ -5,6 +5,7 @@ void Obstacles::init() { + //fprintf("in obstacle init"); //Initializing the X and Y co ordinates of the obstacle obsPosX = rand() % 84; obsPosY = rand() % 42-42; @@ -15,19 +16,19 @@ void Obstacles::draw(N5110 &lcd) { + //Drawing the character + //fprintf("Obstacle being drawn"); lcd.setPixel(obsPosX,obsPosY); - - - } //To move the obstacle void Obstacles::updateObstacle() { //Updating the position of the obstacle on the screen and setting its speed + //fprintf("updating th obstaclee to make it move"); obsPosY =obsPosY+1; } @@ -35,6 +36,8 @@ //Function to check if the obstacle has reached the end of the screen void Obstacles::obstacleStatus(Vector2D p) { + //fprintf("in obstacle status"); + //Update status if at end of the screen if(obsPosY > HEIGHT) { @@ -45,13 +48,21 @@ //Returns the postion (x,y) of the obstacle on the screen Vector2D Obstacles::getObstaclePos() { + //fprintf("in obstacle get pos"); + Vector2D p = {obsPosX,obsPosY}; + //fprintf("the value of p is %d", p); + return p; } //Returns the status of the obstacle bool Obstacles::getObstacleStatus() { + //fprintf("in get obstacle status"); + + //fprintf("The value of obstacle status is (in 0/1) %d",obStatus); + //Used to check when to initialise and render the obstacle return obStatus;
--- 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(); } }