Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Fork of Stick_Runner by
Revision 3:0c690f1c04d8, committed 2017-05-03
- Comitter:
- el15ss
- Date:
- Wed May 03 09:27:04 2017 +0000
- Parent:
- 2:98a41609c827
- Child:
- 4:2fdafb53eac2
- Commit message:
- Added DOxygen commenting to all files
Changed in this revision
--- a/Character/Character.cpp Tue May 02 18:42:45 2017 +0000
+++ b/Character/Character.cpp Wed May 03 09:27:04 2017 +0000
@@ -4,8 +4,12 @@
void Character::init()
{
+ //Initializing the X and Y co ordinates of the character
+
charPosX = 42;
charPosY = 75;
+
+ //variable to store the status of the character
charStatus = true;
}
@@ -14,7 +18,7 @@
{
-
+ //Drawing the character
lcd.setPixel(charPosX,charPosY);
lcd.setPixel(charPosX-1,charPosY);
lcd.setPixel(charPosX+1,charPosY);
@@ -119,24 +123,28 @@
}
-
+//Returns the postion (x,y) of the character on the screen
Vector2D Character::getCharacterPos() {
Vector2D p = {charPosX,charPosY};
return p;
}
+
+//Function to check if the character has been hit by a obstacle and update the status if yes
void Character::characterStatus(Vector2D p)
{
+ //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)))
{
-
+ //Updateing the status
charStatus = false;
}
}
-
+//Function to return the status of the character
bool Character::getCharacterStatus()
{
+ //Used to determine whether the character has been and hit and the game is over
return charStatus;
}
--- a/Character/Character.h Tue May 02 18:42:45 2017 +0000
+++ b/Character/Character.h Wed May 03 09:27:04 2017 +0000
@@ -4,30 +4,70 @@
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
+/** Class Character
+@brief Class responsible for all the functionality of the charachter including intialization, drawing, moving and updating it
+@author Samrudh Sharma
+@date
+*/
+
class Character
{
public:
-
+/** Initialise display
+*
+* Powers up the display and turns on backlight (50% brightness default).
+* Intialises the charchter status, x and y co-ordinate of the character.
+*/
void init();
+
+/** Function to draw the character
+*
+* Draws the character to the display using the N5110 library and its object lcd.
+*
+*/
void draw(N5110 &lcd);
+
+/** Updates/Moves Character
+*
+* Helps the charater move using the joystick.
+* @param d - the direction from the joystick
+* @param mag - initalses the speed of the character from the magnitude of the force on the joystick
+*/
void updateCharacter(Direction d,float mag);
+
+ /** Character Status
+*
+* This fuction helps to check if there are any obstacles/gems around the character on the screen
+* . It checks if any pixels near the body dimensions of the character are lit up and sets the status of the character to false
+* if any.
+* @param p - stores the 2D location i.e. the (x,y) co-ordinate of the cetre point of the character on the screen .
+*/
void characterStatus(Vector2D p);
-//void characterScore(Vector2D p);
-
+
+/** Character Position
+*
+* Returns the 2D location i.e. the (x,y) co-ordinate of the cetre point of the character on the screen .
+*/
Vector2D getCharacterPos();
+
+
+/** Return Character Status
+*
+* Returns the status of the character which is sent to the main, where it is used to determine whether the character
+* should be drawn. If true, the character is drwan else not.
+*/
bool getCharacterStatus();
private:
- int _height;
- int _width;
- int charPosX;
- int charPosY;
- int _speed;
- int _score;
- bool charStatus;
- // int counter;
+
+ int charPosX; // X cocordinate of the character
+ int charPosY; // Y cocordinate of the character
+ int _speed; //Variable to help determine the speed of the character from the magnitude of the joystick
+
+ bool charStatus; //Variable to store the Cgaracters status
+
};
#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FXOS8700CQ.lib Wed May 03 09:27:04 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/trm/code/FXOS8700CQ/#e2fe752b881e
--- a/Gems/Gems.cpp Tue May 02 18:42:45 2017 +0000
+++ b/Gems/Gems.cpp Wed May 03 09:27:04 2017 +0000
@@ -3,9 +3,11 @@
void Gems::init()
{
+ //Initializing the X and Y co ordinates of the character
gemPosX = rand() % 84;
gemPosY = rand() % 42-42;
+ //variable to store the status of the gem
gStatus = true;
}
@@ -13,10 +15,7 @@
void Gems::draw(N5110 &lcd)
{
- //lcd.drawRect(gemPosX-4,gemPosY-3,3,3,FILL_BLACK);
- //lcd.drawLine(gemPosX-3, gemPosY-3,gemPosX-3,gemPosY+1,1);
-
- //lcd.drawCircle(gemPosX,gemPosY,2,FILL_BLACK);
+ //Drawing the gems
lcd.setPixel(gemPosX,gemPosY);
lcd.setPixel(gemPosX+1,gemPosY);
lcd.setPixel(gemPosX-1,gemPosY);
@@ -30,13 +29,11 @@
-int Gems::gemScore()
-{
- //return counter;
-}
+//To move the gem
void Gems::updateGems()
{
+ //Updating the position of the gem on the screen and setting its speed
gemPosY =gemPosY+2;
}
@@ -44,15 +41,16 @@
void Gems::gemStatus(Vector2D p)
{
+ //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))
{
- //counter++;
+
gStatus = false;
}
-
+ //To check if the gem has reached the bottom of the screen so we can intialise and render again
if(gemPosY > HEIGHT)
{
gStatus = false;
@@ -60,6 +58,7 @@
}
+//Returns the postion (x,y) of the gems on the screen
Vector2D Gems::getGemPos()
{
@@ -67,8 +66,10 @@
return p;
}
+//Returns the status of the obstacle
bool Gems::getGemStatus()
{
+ //Used to check when to initialise and render the gem
return gStatus;
}
--- a/Gems/Gems.h Tue May 02 18:42:45 2017 +0000
+++ b/Gems/Gems.h Wed May 03 09:27:04 2017 +0000
@@ -5,25 +5,69 @@
#include "N5110.h"
#include "Gamepad.h"
+
+/** Class Character
+@brief Class responsible for all the functionality of the gem including intialization, drawing, moving, checking whether the character consumes it
+@brief and updating it
+@author Samrudh Sharma
+@date
+*/
class Gems
{
public:
+/** Initialise display
+*
+* Powers up the display and turns on backlight (50% brightness default).
+* Intialises the charchter status, x and y co-ordinate of the gem.
+*/
void init();
+
+/** Function to draw the gem
+*
+* Draws the obstacle to the display using the N5110 library and its object lcd.
+*
+*/
void draw(N5110 &lcd);
+
+/** Updates/Moves Obstacle
+*
+* Helps the Gem move by updating its position on the screen by 2 pixel each time
+* therefore setting its speed aswell.
+*/
void updateGems();
+
+/** Gem Status
+*
+* This fuction helps to check whether the character has touched a gem and updating it status to make diappear from the screen
+* and also to check if it has reached the bottom of the screen and updates the obstacle status
+* to false so that the gems can be generated again this creating a continuous flow of gems
+*
+* @param p - stores the 2D location i.e. the (x,y) co-ordinate of the cetre point of the gem on the screen .
+*/
void gemStatus(Vector2D p);
- int gemScore();
+/** Gem Position
+*
+* Returns the 2D location i.e. the (x,y) co-ordinate of the cetre point of the gem on the screen .
+*/
Vector2D getGemPos();
+/** Return Gem Status
+*
+* Returns the status of the gem which is sent to the main, where it is used to determine whether the gems have to be intialized or
+* or renderd again.
+*/
+
bool getGemStatus();
private:
- int gemPosX;
- int gemPosY;
-bool gStatus;
+
+//Variables
+ int gemPosX; // X cocordinate of the gem
+ int gemPosY; // Y cocordinate of the gem
+ bool gStatus; //Variable to store the Obstacles status
};
#endif
\ No newline at end of file
--- a/Obstacles/Obstacles.cpp Tue May 02 18:42:45 2017 +0000
+++ b/Obstacles/Obstacles.cpp Wed May 03 09:27:04 2017 +0000
@@ -5,17 +5,17 @@
void Obstacles::init()
{
+ //Initializing the X and Y co ordinates of the obstacle
obsPosX = rand() % 84;
obsPosY = rand() % 42-42;
+
+ //variable to store the status of the obstacle
obStatus = true;
}
void Obstacles::draw(N5110 &lcd)
{
- //lcd.drawRect(obsPosX-4,obsPosY-3,3,3,FILL_BLACK);
- //lcd.drawLine(obsPosX-3, obsPosY-3,obsPosX-3,obsPosY+1,1);
-
- //lcd.drawCircle(obsPosX,obsPosY,2,FILL_BLACK);
+ //Drawing the character
lcd.setPixel(obsPosX,obsPosY);
@@ -24,36 +24,35 @@
}
-
+//To move the obstacle
void Obstacles::updateObstacle()
{
+ //Updating the position of the obstacle on the screen and setting its speed
obsPosY =obsPosY+1;
}
+//Function to check if the obstacle has reached the end of the screen
void Obstacles::obstacleStatus(Vector2D p)
{
- /* if(((p.x<obsPosX+3)&&(p.x>obsPosX-3))&&((p.y<obsPosY+3)&&(p.y>obsPosY-3))){
-
- obstacleStatus = false;
- }
- */
+ //Update status if at end of the screen
if(obsPosY > HEIGHT)
{
obStatus = false;
}
}
-
+//Returns the postion (x,y) of the obstacle on the screen
Vector2D Obstacles::getObstaclePos()
{
Vector2D p = {obsPosX,obsPosY};
return p;
}
-
+//Returns the status of the obstacle
bool Obstacles::getObstacleStatus()
{
+ //Used to check when to initialise and render the obstacle
return obStatus;
}
--- a/Obstacles/Obstacles.h Tue May 02 18:42:45 2017 +0000
+++ b/Obstacles/Obstacles.h Wed May 03 09:27:04 2017 +0000
@@ -4,26 +4,68 @@
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
+/** Class Character
+@brief Class responsible for all the functionality of the obstacles including intialization, drawing, moving and updating it
+@author Samrudh Sharma
+@date
+*/
+
+
class Obstacles
{
public:
-// Obstacles();
- // ~Obstacles();
+/** Initialise display
+*
+* Powers up the display and turns on backlight (50% brightness default).
+* Intialises the charchter status, x and y co-ordinate of the obstacle.
+*/
void init();
+
+ /** Function to draw the obstacle
+*
+* Draws the obstacle to the display using the N5110 library and its object lcd.
+*
+*/
void draw(N5110 &lcd);
+
+/** Updates/Moves Obstacle
+*
+* Helps the Obstacle move by updating its position on the screen by 1 pixel each time
+* therefore setting its speed aswell.
+*/
void updateObstacle();
+
+/** Obstacle Status
+*
+* This fuction helps to check whether the obstacle has reached the bottom of the screen and updates the obstacle status
+* to false so that the obstacles can be generated again this creating a continuous flow of obstacles
+*
+* @param p - stores the 2D location i.e. the (x,y) co-ordinate of the cetre point of the obstacle on the screen .
+*/
void obstacleStatus(Vector2D p);
- void add_score();
- int get_score();
+
+/** Obstacle Position
+*
+* Returns the 2D location i.e. the (x,y) co-ordinate of the cetre point of the obstacle on the screen .
+*/
Vector2D getObstaclePos();
+
+/** Return Obstacle Status
+*
+* Returns the status of the obstacle which is sent to the main, where it is used to determine whether the obstacles have to be intialized or
+* or renderd again.
+*/
bool getObstacleStatus();
private:
- int obsPosX;
- int obsPosY;
-bool obStatus;
+
+//Variables
+ int obsPosX; // X cocordinate of the obstacle
+ int obsPosY; // Y cocordinate of the obstacle
+ bool obStatus; //Variable to store the Obstacles status
+
};
#endif
\ No newline at end of file
--- a/main.cpp Tue May 02 18:42:45 2017 +0000
+++ b/main.cpp Wed May 03 09:27:04 2017 +0000
@@ -13,7 +13,14 @@
#define No_OBS 8
#define No_GEMS 4
+//Variables
+// i - to loop through the obstacles
+// j - to loop through the gems
+// counter - to keep track of score
+// highScore - to store high score
int i,j,counter,highScore;
+
+//To helo convert counter(int) to string to display on the screen
char score[50];
//Structs
@@ -26,7 +33,7 @@
Gamepad pad;
Character c;
Obstacles obstacle[No_OBS];
-Gems gems[No_GEMS],g;
+Gems gems[No_GEMS];
SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd");
FILE *file;
@@ -40,33 +47,164 @@
void menu();
void over();
void Instructions();
-
-
+void stickRunner();
+void displayHighScore();
/* Functions */
int main()
{
- int fps = 8;
+
/* Intialization */
init();
/* Drawing the intial frame */
welcome();
- wait(1.0f/fps);
+
+}
+
+
+void init()
+{
+//Need to initialize the lcd and gamepad
+ lcd.init();
+ pad.init();
+
+//Intialzing the charachter
+ c.init();
+
+//Intialzing the obstacles
+ for(i=0;i<No_OBS;i++)
+ {
+ obstacle[i].init();
+ }
+
+//Intialzing the gems
+ for(j=0;j<No_GEMS;j++)
+ {
+ gems[j].init();
+ }
+
+
+
+}
+
+
+
+//Funstion to display the Welcome page
+void welcome() {
+
+
+
+ lcd.printString("Stick Runner! ",0,1);
+ lcd.printString(" Press Start ",0,4);
+ lcd.refresh();
+ // pad.tone(1500.0,0.5);
+ // pad.tone(1500.0,0.5);
+
+
+ //Flashes LEDS aslong as START is not pressed
+ while ( pad.check_event(Gamepad::START_PRESSED) == false)
+ {
+ pad.leds_on();
+ wait(0.1);
+ pad.leds_off();
+ wait(0.1);
+
+ }
+
+ menu();
+}
+
+
+//Function to display the Menu page
+void menu() {
+ //int fps =8;
+ bool i = true;
-
+ lcd.clear();
+ lcd.printString(" Menu ",0,0);
+ lcd.printString("A)New Game ",0,2);
+ lcd.printString("B)Continue ",0,3);
+ lcd.printString("X)Instructions ",0,4);
+ lcd.printString("Y)High Score ",0,5);
+
+ lcd.refresh();
+
+
+ while(i == true) {
+ // wait flashing LEDs until start button is pressed
+ //Condition to start a new game
+ if( pad.check_event(Gamepad::A_PRESSED) ) {
+ //pad.tone(1000.0,0.5);
+ i = false;
+
+ //Clear, refresh and intialize the game again so we can start a new game
+ lcd.clear();
+ lcd.refresh();
+ init();
+ stickRunner();
+
+ //break;
+ }
+
+ //To continue the same game
+ else if( pad.check_event(Gamepad::B_PRESSED) ) {
+ //lcd.clear();
+ // pad.tone(1000.0,0.5);
+ i = false;
+
+ //Simply refreshes the page and continues from where the user left the game
+ // as the intialize function init() is not called again
+ lcd.refresh();
+ stickRunner();
+
+
+ }
+
+ //To read the game instructions
+ else if( pad.check_event(Gamepad::X_PRESSED) ) {
+ // pad.tone(1000.0,0.5);
+ Instructions();
+
+ i = false;
+ }
+
+ //To see the game high score
+ else if( pad.check_event(Gamepad::Y_PRESSED) ) {
+ //pad.tone(1000.0,0.5);
+ displayHighScore();
+
+ i = false;
+ }
+
+
+ }
+}
+
+
+
+
+//This function is responsible for running the game
+void stickRunner()
+{
+ int fps = 8;
+
render();
wait(1.0f/fps);
/* Main game loop to read input, render the display and update the game state */
- // game loop - read input, update the game state and render the display
+
while (1) {
+ //As long as the character survives update the score
counter++;
+ //Using the gamepad library to move the character using the joystick
c.updateCharacter(pad.get_direction(),pad.get_mag());
+
+ //Condition to ckeck if the user wants to pause the game
if(pad.check_event(Gamepad::BACK_PRESSED))
{
lcd.clear();
@@ -74,23 +212,32 @@
menu();
}
+ //Loop to make the generation of obstacles a continious loop and also to check if the user has been killed
for(i=0;i<No_OBS;i++)
{
+ //To retrieve the status of the obstacle on the screen
obstacle[i].obstacleStatus(obstacle[i].getObstaclePos());
if(obstacle[i].getObstacleStatus() == false)
{
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
c.characterStatus(obstacle[i].getObstaclePos());
- // obstacle[i].obstacleStatus(c.getCharacterPos());
+
}
-
+ //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++)
{
+ //To check whether the character has collected a gem by comparing the position of each gem
+ // relative to the character
gems[j].gemStatus(c.getCharacterPos());
+
+
if(gems[j].getGemStatus() == false)
{
@@ -102,6 +249,8 @@
}
//To make the obstacles and gems move along the screen
+
+
i =0;
for(i=0;i<No_OBS;i++)
@@ -122,34 +271,18 @@
wait(1.0f/fps);
}
+
}
-void init()
-{
-
- lcd.init();
- pad.init();
- c.init();
-
- for(i=0;i<No_OBS;i++)
- {
- obstacle[i].init();
- }
- for(j=0;j<No_GEMS;j++)
- {
- gems[j].init();
- }
-
-
-
-}
-
+//Function to draw out the pixels on the screen
void render()
{
lcd.clear();
+
+ //Only draws the character as long as it survives
if(c.getCharacterStatus())
{
c.draw(lcd);
@@ -161,7 +294,7 @@
}
- //obstacle[i].draw(lcd);
+ //Draws the obstacles if the status returned is true
for(i=0;i<No_OBS;i++)
{
@@ -171,7 +304,7 @@
}
}
-
+ //Draws the gems if the status returned is true
for(j=0;j<No_GEMS;j++)
{
if(gems[j].getGemStatus())
@@ -180,9 +313,7 @@
}
- /*else{
- counter++;
- }*/
+
}
@@ -195,43 +326,27 @@
}
-void welcome() {
-
-
-
- lcd.printString("Stick Runner! ",0,1);
- lcd.printString(" Press Start ",0,4);
- lcd.refresh();
- // pad.tone(1500.0,0.5);
- //pad.tone(1500.0,0.5);
-
-
-
- while ( pad.check_event(Gamepad::START_PRESSED) == false)
- {
- pad.leds_on();
- wait(0.1);
- pad.leds_off();
- wait(0.1);
-
- }
- menu();
-}
+//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() {
-
+ //pad.tone(1000.0,0.5);
pad.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);
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");
@@ -240,6 +355,8 @@
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);
@@ -261,7 +378,7 @@
lcd.refresh();
sd.unmount();
-
+ //Takes the user back to the main for a new game
while ( pad.check_event(Gamepad::START_PRESSED) == false) {
pad.leds_on();
//pad.tone(1000.0,0.5);
@@ -277,24 +394,28 @@
}
+
+//Function to display the current High score fo the game and also reset it to 0
void displayHighScore()
{
sd.mount();
lcd.clear();
-
+ //Open file
file = fopen("/sd/scoreFile.txt", "r");
if(file ==NULL)
- {
+ {
+
highScore = 0;
}
- else{
+ else{
+ //Read the high score from the file
fscanf(file,"%d", &highScore);
fclose(file);
}
-
+ //Convert highscore(int) to score(String) to print on the lcd
sprintf (score, "High Score : %d",highScore);
lcd.printString(score,0,2);
@@ -305,11 +426,12 @@
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");
@@ -318,7 +440,7 @@
displayHighScore();
}
-
+ //Back to menu
if( pad.check_event(Gamepad::BACK_PRESSED)) {
menu();
}
@@ -330,58 +452,9 @@
}
-void menu() {
- int fps =8;
- bool i = true;
- while(i == true)
- {
- lcd.clear();
- lcd.printString(" Menu ",0,0);
- lcd.printString("A)New Game ",0,2);
- lcd.printString("B)Continue ",0,3);
- lcd.printString("X)Instructions ",0,4);
- lcd.printString("Y)High Score ",0,5);
-
- lcd.refresh();
-
- // wait flashing LEDs until start button is pressed
- if( pad.check_event(Gamepad::A_PRESSED) ) {
- //pad.tone(1000.0,0.5);
- i = false;
- lcd.clear();
- lcd.refresh();
- init();
-
- //break;
- }
-
- else if( pad.check_event(Gamepad::B_PRESSED) ) {
- //lcd.clear();
- // pad.tone(1000.0,0.5);
- i = false;
- lcd.refresh();
-
-
- }
-
- else if( pad.check_event(Gamepad::X_PRESSED) ) {
- // pad.tone(1000.0,0.5);
- Instructions();
- wait(1.0f/fps);
- i = false;
- }
-
- else if( pad.check_event(Gamepad::Y_PRESSED) ) {
- // pad.tone(1000.0,0.5);
- displayHighScore();
- wait(10.0f/fps);
- i = false;
- }
-
-
- }
-}
+
+//Function to display the Instructions for the game
void Instructions()
{
bool i = true;
