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: mbed
Diff: main.cpp
- Revision:
- 18:71ecb1a428b3
- Parent:
- 12:3bfde5b4d834
- Child:
- 19:538c93b1b0c8
--- a/main.cpp Thu May 09 12:38:51 2019 +0000
+++ b/main.cpp Thu May 09 13:59:41 2019 +0000
@@ -29,24 +29,53 @@
SpaceRebEngine spacerebellion;
///////////// prototypes ///////////////
-void init();
-void update_game(UserInput input);
-void render();
-void welcome();
-void menu();
-void Story();
-void Controls();
-void Credits();
-void Game1Over();
-void x1_1();
-void x1_2();
-void x2_1();
-void x2_2();
-void x3_1();
-void x3_2();
+ /**
+ * @brief Initialises spaceinvader, missiles, lcd and pad
+ * @param sets spaceinvader @details Sets height and width of the spaceinvader private variables
+ * @param sets missiles @details Sets size and speed of the missiles private variables
+ */
+ void init();
+
+ void update_game(UserInput input);
+ void render();
+ void welcome();
+ void menu();
+ void Story();
+ void Controls();
+ void Credits();
+ void Game1Over();
+ /**
+ * @brief Draws first half of first X
+ * @return draw initial first X @details Draws the first half of the first X
+ */
+ void x1_1();
+ /**
+ * @brief Draws first X
+ * @return draw first X @details Draws the complete first X
+ */
+ void x1_2();
+ /**
+ * @brief Draws first half of second X
+ * @return draw initial second X @details Draws the first half of the second X
+ */
+ void x2_1();
+ /**
+ * @brief Draws second X
+ * @return draw second X @details Draws the complete second X
+ */
+ void x2_2();
+ /**
+ * @brief Draws first half of third X
+ * @return draw initial third X @details Draws the first half of the third X
+ */
+ void x3_1();
+ /**
+ * @brief Draws third X
+ * @return draw third X @details Draws the complete third X
+ */
+ void x3_2();
///////////// gloabal variable ////////////////
bool end = false;
-float counter = 1.0;
int score = 0;
int speed = MISSILES_SPEED;
int i=0;
@@ -100,103 +129,98 @@
{
int fps = 8; // frames per second
- init(); // initialise and then display welcome screen...
- welcome(); // waiting for the user to start
+ init(); // initialises the lcd, pad and SpaceRebEngine
+ welcome(); // opening screen displaying game name
menu:
- menu();
- init();
- end = false;
+ menu(); //menu to decide whether player wants to enter story, play, controls or credits
+ init(); //will reinitialise when play option is chosen so that screen does not skip to game1over() function
+ end = false; //set the value of end to equal false so game can be re_run multople times
render(); // first draw the initial frame
- wait(1.0f/fps); // and wait for one frame period
+ wait(1.0f/fps); // waits for one frame period
- // game loop - read input, update the game state and render the display
- while (!end) {
- spacerebellion.read_input(pad);
- spacerebellion.update(pad);
+ while (!end) { //this means game will play until boolean of end is inverted, equals true
+ spacerebellion.read_input(pad); //takes the input from the joystick
+ spacerebellion.update(pad); //runs the game mechanics
render();
- end = spacerebellion.game_end();
+ end = spacerebellion.game_end(); //when player dies game will end as valu of end will be true
wait(1.0f/fps);
- counter = counter + 1/8;
}
lcd.clear();
i = 0;
- while(i < 200) {
- lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_BLACK);
+ while(i < 200) { //creates internal loop that displays animation until i = 200
+ lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_BLACK); //pitch black screen
lcd.refresh();
- i++;
+ i++; //increases integer i by 1
}
lcd.clear();
i = 0;
while(i < 200) {
- x1_1();
+ x1_1(); //draws first half of first x
+ lcd.refresh();
+ pad.tone(1200.0,0.2); //plays low beep
+ i++;
+ }
+ i = 0;
+ while(i < 200) {
+ x1_2(); //draws first x
+ lcd.refresh();
+ pad.tone(1500.0,0.2); //plays high beep
+ i++;
+ }
+ i = 0;
+ while(i < 200) {
+ x2_1(); //draws first half of second x
lcd.refresh();
pad.tone(1200.0,0.2);
i++;
}
i = 0;
while(i < 200) {
- x1_2();
+ x2_2(); //draws second x
lcd.refresh();
pad.tone(1500.0,0.2);
i++;
}
i = 0;
while(i < 200) {
- x2_1();
- lcd.refresh();
- pad.tone(1200.0,0.2);
- i++;
- }
- i = 0;
- while(i < 200) {
- x2_2();
- lcd.refresh();
- pad.tone(1500.0,0.2);
- i++;
- }
- i = 0;
- while(i < 200) {
- x3_1();
+ x3_1(); //draws first half of third x
lcd.refresh();
pad.tone(1200.0,0.2);
i++;
}
i = 0;
while(i < 200) {
- x3_2();
+ x3_2(); //draws third x
lcd.refresh();
pad.tone(1500.0,0.2);
i++;
}
lcd.clear();
- Game1Over();
- goto menu;
+ Game1Over(); //displays the game over sequance
+ goto menu; //loops code back to the menu function
}
-// initialies all classes and libraries
+
void init()
{
- // need to initialise LCD and Gamepad
- lcd.init();
- pad.init();
-
- // initialise the game with correct ball and paddle sizes
+
+ lcd.init(); //initialises lcd display
+ pad.init(); //initialises gamepad
+ //initialises spaceinvader and missiles
spacerebellion.init(SPACEINVADER_WIDTH,SPACEINVADER_HEIGHT,MISSILES_SIZE,MISSILES_SPEED);
}
-// this function draws each frame on the LCD
+
void render()
{
- // clear screen, re-draw and refresh
lcd.clear();
- spacerebellion.draw(lcd);
+ spacerebellion.draw(lcd); //draws game
lcd.refresh();
}
-// simple splash screen displayed on start-up
-void welcome() {
+void welcome() { //startup screen
lcd.printString(" Space ",0,1);
lcd.printString(" Rebellion! ",0,2);
@@ -205,18 +229,17 @@
lcd.drawSprite(WIDTH - 16,3,8,11,(int *)invader); //adds spaceinvader into top right corner
lcd.refresh();
- // wait flashing LEDs until start button is pressed
while ( pad.check_event(Gamepad::START_PRESSED) == false) {
pad.leds_on();
- wait(0.1);
+ wait(0.15);
pad.leds_off();
- wait(0.1);
+ wait(0.15); //flashes leds on and of until start is pressed
}
lcd.clear();
lcd.refresh();
}
-void menu() {
+void menu() { //menu screen
lcd.clear();
lcd.printString(" MENU ",0,0);
lcd.printString("A ==> Story ",0,1);
@@ -228,22 +251,20 @@
// wait flashing LEDs until start button is pressed
while (choose == 0) {
if (pad.check_event(Gamepad::A_PRESSED) == true) {
- choose = 1;
- Story();
- menu();
+ choose = 1; //used to break menu while loop
+ Story(); //displays story
+ menu(); //returns to menu screen
} else if (pad.check_event(Gamepad::B_PRESSED) == true) {
choose = 1;
} else if (pad.check_event(Gamepad::X_PRESSED) == true) {
choose = 1;
- Controls();
+ Controls(); //displays controls
menu();
} else if (pad.check_event(Gamepad::Y_PRESSED) == true) {
choose = 1;
- Credits();
+ Credits(); //displays credits
menu();
}
-
-
pad.leds_on();
wait(0.1);
pad.leds_off();
@@ -259,11 +280,11 @@
lcd.printString("Humans Control",0,2);
lcd.printString("Sectors 3-8 Of",0,3);
lcd.printString("The Milky Way.",0,4);
- lcd.printString(" Press A ",0,5);
- lcd.drawSprite(WIDTH - 12,HEIGHT - 7,6,11,(int *)arrowd);
- lcd.drawSprite(1,HEIGHT - 7,6,11,(int *)arrowd);
+ lcd.printString(" Press A ",0,5);
+ lcd.drawSprite(WIDTH - 12,HEIGHT - 7,6,11,(int *)arrowd); //adds arrow in bottom right corner
+ lcd.drawSprite(1,HEIGHT - 7,6,11,(int *)arrowd); //adds arrow in bottom left corner
lcd.refresh();
- int story = 0;
+ int story = 0; //used to give story value of zero for while loops
// wait flashing LEDs until start button is pressed
while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
if (pad.check_event(Gamepad::A_PRESSED) == true) {
@@ -274,13 +295,13 @@
lcd.printString(" Are Captured ",0,3);
lcd.printString("Used for Sport",0,4);
lcd.printString(" Press A ",0,5);
- lcd.drawSprite(WIDTH - 12,HEIGHT - 7,6,11,(int *)arrowd);
- lcd.drawSprite(1,HEIGHT - 7,6,11,(int *)arrowd);
+ lcd.drawSprite(WIDTH - 12,HEIGHT - 7,6,11,(int *)arrowd); //adds arrow in bottom right corner
+ lcd.drawSprite(1,HEIGHT - 7,6,11,(int *)arrowd); //adds arrow in bottom left corner
lcd.refresh();
- story = 1;
+ story = 1; //sets story to equal 1 to enter next while loop when a is pressed
while (story == 1) {
if (pad.check_event(Gamepad::A_PRESSED) == true) {
- story = 2;
+ story = 2; //if a is pressed awe go to page 3 of story
}
}
while (story == 2){
@@ -293,11 +314,11 @@
lcd.printString("**Press Back**",0,5);
lcd.refresh();
if ( pad.check_event(Gamepad::BACK_PRESSED) == true){
- break;
+ break; //breaks while loop when back is pressed in third page of story
}
}
- break;
+ break; //goes back to menu screen
}
pad.leds_on();
wait(0.1);
@@ -320,8 +341,8 @@
lcd.drawSprite(1,HEIGHT - 7,6,11,(int *)arrowd);
lcd.refresh();
// wait flashing LEDs until start button is pressed
- while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
- if (pad.check_event(Gamepad::X_PRESSED) == true) {
+ while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { //if back is pressed we will return to menu screen
+ if (pad.check_event(Gamepad::X_PRESSED) == true) { //if x is pressed then the next page of controls will be displayed
lcd.clear();
lcd.printString(" Controls ",0,0);
lcd.printString(" How: ",0,1);
@@ -348,7 +369,8 @@
lcd.printString("**Press Back** ",0,5);
lcd.refresh();
// wait flashing LEDs until start button is pressed
- while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
+ while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { //if back is pressed then will return to main menu after 'easter egg' loop
+ // easter egg is lights will flash my surname RIGBY in morse code accompanied by beeps
wait(1);
pad.leds_on();
pad.tone(1500.0,0.1);
@@ -441,13 +463,13 @@
void Game1Over() {
lcd.printString(" You Died! ",0,1);
lcd.printString(" Score =",0,2);
- spacerebellion.print_scores(lcd);
- lcd.drawSprite(5,3,11,11,(int *)X_2);
- lcd.drawSprite(WIDTH - 13,3,11,11,(int *)X_2);
+ spacerebellion.print_scores(lcd); //this is used to print the score after game is played
+ lcd.drawSprite(5,3,11,11,(int *)X_2); //draws an X in top left corner
+ lcd.drawSprite(WIDTH - 13,3,11,11,(int *)X_2); //draws an X in top right corner
lcd.printString(" Press BACK...",0,4);
lcd.refresh();
// wait flashing LEDs until start button is pressed
- while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
+ while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { // if back pressed will return to menu
pad.leds_on();
wait(0.1);
pad.leds_off();
@@ -457,31 +479,31 @@
}
void x1_1() {
- lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_1);
+ lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); //draws rectangle around outline of lcd display
+ lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_1); //draws first half of first x
}
void x1_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_2);
+ lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_2); //draws first x
}
void x2_1() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_1);
+ lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_1); //draws first half of second x
}
void x2_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_2);
+ lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_2); //draws second x
}
void x3_1() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_1);
+ lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_1); //draws first half of third x
}
void x3_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_2);
+ lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_2); //draws third x
}
\ No newline at end of file