ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Coin.h Source File

Coin.h

00001 #ifndef COIN_H
00002 #define COIN_H
00003 
00004 #include "mbed.h"
00005 
00006 /** Coin Class
00007 * @brief Generates a coin for the skateboarder to collect *@author Lewis Wooltorton
00008 * @date April 2019
00009 
00010 @code
00011 
00012 #include "mbed.h"
00013 #include "N5110.h"
00014 #include "Gamepad.h"
00015 #include "Coin.h"
00016 #include <cstdlib>
00017 #include <ctime>
00018 
00019 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00020 Gamepad gamepad;
00021 Coin _coin;
00022 
00023 int _skater_x;
00024 int _skater_y;
00025 int _player_score;
00026 bool _coin_collision_flag;
00027 
00028 int main() {
00029   _coin.init();
00030   srand(time(NULL));
00031   _player_score = 0;
00032   _skater_x = 50;
00033   _skater_y = 20;
00034   while(1) {
00035     
00036     // If the skater collides with the coin, set the flag, add 1 to the score 
00037     // move the coin to a new random position and make a noise on the buzzer.
00038     if (_skater_x == _coin.get_coin_x() 
00039       && (_skater_y == _coin.get_coin_y() - 10)) {  
00040     _coin_collision_flag = true;
00041     _player_score++;
00042     _coin.set_coin((rand() % 100),(abs(rand() % 100 - 20)));  // Place coin 
00043     // on a constrained random position.
00044     gamepad.tone(1500, 0.05);  // Make collection noise on buzzer.
00045     wait(0.05);
00046     gamepad.tone(3000, 0.05);   
00047     }
00048     
00049     // Print the coin.
00050     lcd.drawSprite(_coin.get_coin_x(),_coin.get_coin_y(),5,5,
00051                    (int*)_coin.get_coin_sprite());
00052   }
00053 }
00054        
00055 @endcode
00056 */
00057 
00058 class Coin {
00059  public:
00060   // Constructor and Destructor.
00061   /**
00062   * @brief Constructor @details Non user specified.
00063   */
00064   Coin();
00065   /**
00066   * @brief Destructor @details Non user specified.
00067   */
00068   ~Coin();
00069   
00070   // Mutators.
00071   /** 
00072   * @breif Initialises Coin object. 
00073   */
00074   void init();
00075   /**
00076   * @breif Sets the Coin coordinates.
00077   * @param rand_x @details a random number that determines the x coordinate
00078   * @param rand_y @details a random number that determines if the Coin is generated on the upper or lower platforms
00079   */
00080   void set_coin(int rand_x, int rand_y);
00081   
00082   // Accessors.
00083   /**
00084   * @breif Gets the Coin sprite.
00085   * @returns The Coin sprite (an integer array)
00086   */
00087   int * get_coin_sprite();
00088   /**
00089   * @breif Gets the x coordinate.
00090   * @returns The x coordinate of the Coin
00091   */ 
00092   int get_coin_x();
00093   /**
00094   * @breif Gets the y coordinate.
00095   * @returns The y coordinate of the Coin
00096   */
00097   int get_coin_y(); 
00098   
00099   // Member Methods.
00100   /**
00101   * @breif Generates the coin. @details Selects the coin sprite
00102   */
00103   void generate_coin();
00104     
00105  private:
00106   int _x;
00107   int _y;
00108   int _coin_counter;
00109   bool _rotate_coin;
00110 };
00111 #endif