Dmitrijs Griskovs / Mbed 2 deprecated el17dg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hud.h Source File

hud.h

00001 #ifndef HUD_H
00002 #define HUD_H
00003 
00004 /**
00005  * Hud Class
00006  * @brief Renders interface elemets such as score and lifes
00007  * @author Dmitrijs Griskovs
00008  * @date 15/04/2019
00009  */
00010 class Hud {
00011 
00012 public:
00013      /** 
00014      * Constructor. 
00015      * @brief calls for the reset of red LED. 
00016      */ 
00017     Hud() { resetRedLed(); }
00018     
00019     /** @brief Resets red LED state and counter. */
00020     void resetRedLed() {
00021         red_led_flashing = 0;
00022         red_led_state = false;
00023     }
00024     
00025     /** @brief Draws an in-game score on the screen during the gameplay. */
00026     void drawScore(){
00027         char buffer[16];
00028         sprintf(buffer," Score: %i", GameGlobals::game_score);
00029         lcd.printString(buffer,0,0);    
00030     }
00031     
00032     /**@brief Displays in the main menu the highest score reached. */
00033     void drawHighScore(){
00034         char buffer[16];
00035         sprintf(buffer,"High Score %i", GameGlobals::high_score);
00036         lcd.printString(buffer,0,0);
00037     }
00038      
00039     /** 
00040      * @brief Display life counter using LEDs.
00041      * @details Checks the palyer's life value and lights the LEDs on/off 
00042      * accordingly to how many lifes are left. green = 3, yellow = 2 and red = 1.
00043      */
00044     void displayLifes(){
00045         if (GameGlobals::player_lifes == 3){
00046             playerHasThreeLives();  
00047         }
00048         else if (GameGlobals::player_lifes == 2){
00049             playerHasTwoLives();
00050         }
00051         else if (GameGlobals::player_lifes == 1){
00052             playerHasOneLife();
00053         }
00054         else {
00055             // all LEDs are flashing
00056             gamepad.leds_off();
00057         }        
00058     }
00059    
00060 private:
00061     void playerHasThreeLives(){
00062         //turn all LEDs on
00063         gamepad.leds_on();   
00064     }
00065     
00066     void playerHasTwoLives(){
00067       // only yelow and red are lit (total 4)
00068         gamepad.led(6,0.0);
00069         gamepad.led(3,0.0);
00070     }
00071     
00072     void playerHasOneLife(){
00073         // red LED is lit and flashes.
00074         gamepad.led(2,0.0);
00075         gamepad.led(5,0.0);
00076         gamepad.led(6,0.0);
00077         if (red_led_flashing == 5){
00078             gamepad.led(1,(float)red_led_state);
00079             gamepad.led(4,(float)red_led_state);
00080             gamepad.led(1,(float)!red_led_state);
00081             gamepad.led(4,(float)!red_led_state);
00082             red_led_flashing = 0;
00083             red_led_state = !red_led_state;
00084         }        
00085         red_led_flashing += 1;
00086     }
00087     
00088     int red_led_flashing;
00089     bool red_led_state;
00090 };
00091 
00092 #endif