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.
Map.h
00001 #ifndef MAP_H 00002 #define MAP_H 00003 00004 #include "mbed.h" 00005 #include "N5110.h" 00006 #include "Gamepad.h" 00007 #include "Paddle.h" 00008 #include "Ball.h" 00009 #include <vector> 00010 00011 00012 /** Struct containing all parameters for the bricks */ 00013 struct Brick { 00014 int x, y, w, h; // x and y coords, width, height 00015 }; 00016 00017 00018 // Forward declaration 00019 class Map; 00020 00021 00022 /** Level Class 00023 , Creates the levels in the Breakout game 00024 @author Kostadin Chakarov, University of Leeds 00025 @date April 2019 00026 */ 00027 00028 00029 class Level 00030 { 00031 public: 00032 /** Draws the bricks and stores them in the levels vector, 00033 * virtual as it changes for each level 00034 */ 00035 virtual void initBricks(Map &map) {} 00036 }; 00037 00038 /** Inherits from level and doesn't have any additional functions */ 00039 class Level1 : public Level 00040 { 00041 public: 00042 /** Draws the bricks for level 1 and stores them in the levels vector */ 00043 virtual void initBricks(Map &map); 00044 }; 00045 00046 /** Inherits from level and doesn't have any additional functions */ 00047 class Level2 : public Level 00048 { 00049 public: 00050 /** Draws the bricks for level 2 and stores them in the levels vector */ 00051 virtual void initBricks(Map &map); 00052 }; 00053 00054 /** Inherits from level and doesn't have any additional functions */ 00055 class Level3 : public Level 00056 { 00057 public: 00058 /** Draws the bricks for level 3 and stores them in the levels vector */ 00059 virtual void initBricks(Map &map); 00060 }; 00061 00062 00063 /* 00064 Power ups Coding strategy: 00065 1. Define the PowerUp and it's draw method, set up all it's parameters in ctor 00066 2. Lifetime cycle management: 00067 2.a. Identify the spawn event (e.g. when brick is killed), then go to that code 00068 2.b. Create the power up and put it in the map's powerUps vector 00069 2.c. During update, iterate over all powerUps and move them 00070 2.d. In checkCollisions, iterate over all powerUps and check if any hit the pad 00071 2.d.i. If hit the paddle, remove and apply power-up effect 00072 2.d.ii. else if fell below paddle height, just remove and don't apply effect 00073 */ 00074 00075 00076 // Forward declaration since needed in PowerUpType struct 00077 class PowerUp; 00078 00079 /** Struct which contains all the powerup parameters and functions */ 00080 struct PowerUpType { 00081 int type; /** Stores the type of the powerup */ 00082 int* sprite; /** Stores the sprite of the powerup */ 00083 00084 /** Constructor */ 00085 PowerUpType(int type, int* sprite) : type(type), sprite(sprite) {} 00086 00087 /** Gives the powerup effect to the player */ 00088 virtual void giveBonus(Paddle &paddle, Ball &ball) {} 00089 /** Chooses which powerup to draw on the LCD depending on its type */ 00090 virtual void draw(N5110 &lcd, PowerUp &pUp); 00091 }; 00092 00093 /** Inherits from the powerUpType struct and only changes the giveBonus function */ 00094 struct PaddleSizePUpType : public PowerUpType { 00095 /** Constructor */ 00096 PaddleSizePUpType(int type, int* sprite) : PowerUpType(type, sprite) {} 00097 /** Gives the paddle size increase powerup effect to the player */ 00098 virtual void giveBonus(Paddle &paddle, Ball &ball); 00099 }; 00100 00101 /** Inherits from the powerUpType struct and only changes the giveBonus function */ 00102 struct PaddleSpeedPUpType : public PowerUpType { 00103 /** Constructor */ 00104 PaddleSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} 00105 /** Gives the paddle speed incrase powerup effect to the player */ 00106 virtual void giveBonus(Paddle &paddle, Ball &ball); 00107 }; 00108 00109 /** Inherits from the powerUpType struct and only changes the giveBonus function */ 00110 struct BallSpeedPUpType : public PowerUpType { 00111 /** Constructor */ 00112 BallSpeedPUpType(int type, int* sprite) : PowerUpType(type, sprite) {} 00113 /** Gives the ball speed decrease powerup effect to the player */ 00114 virtual void giveBonus(Paddle &paddle, Ball &ball); 00115 }; 00116 00117 #define PowerUpW 3 00118 #define PowerUpH 4 00119 #define PowerUpDropChancePct 15 00120 00121 00122 /** PowerUp Class 00123 , Creates the power ups in the Breakout game 00124 @author Kostadin Chakarov, University of Leeds 00125 @date April 2019 00126 */ 00127 00128 class PowerUp : public GameObject { 00129 //PowerUpType& pUpType; 00130 00131 // we cannot store a reference because (for some reason) it is not copy-constructable 00132 // and for some reason, PowerUp needs to be that. 00133 PowerUpType* _pUpType; 00134 00135 00136 00137 public: 00138 /** Constructor 00139 * @param x the initial x-position of the PowerUp 00140 * @param y the initial y-position of the PowerUp 00141 * @param pUpType the type of the PowerUp 00142 */ 00143 PowerUp(float x, float y, PowerUpType& pUpType); 00144 /** Destructor - empty */ 00145 ~PowerUp() {}; 00146 00147 /** Determines the bonus depending on the type of the powerup */ 00148 void giveBonus(Paddle &paddle, Ball &ball) { 00149 _pUpType->giveBonus(paddle, ball); 00150 } 00151 /** Draws each powerup on the LCD after type has been assigned*/ 00152 virtual void draw(N5110 &lcd); 00153 }; 00154 00155 00156 /** Map Class 00157 , Creates the map and controls collision between objects in the Breakout game 00158 @author Kostadin Chakarov, University of Leeds 00159 @date April 2019 00160 */ 00161 00162 class Map 00163 { 00164 private: 00165 int currentLevel; /** Keeps track of the current level */ 00166 std::vector<Level*> levels; /** Stores the levels in a vector */ 00167 std::vector<Brick> bricks; /** Stores the bricks in a vector */ 00168 std::vector<PowerUp> powerUps; /** Stores the powerups in a vector */ 00169 00170 public: 00171 /** Constructor */ 00172 Map(); 00173 /** Destructor */ 00174 ~Map(); 00175 /** Gets the current level 00176 * @return the number of the current level 00177 */ 00178 int getCurrentLevel() { return currentLevel; } 00179 /** Checks if all levels are defeated 00180 * @return true if current level == number of levels 00181 */ 00182 bool hasWon() { return currentLevel >= levels.size(); } 00183 /** Get the level object based on the current level number and call initBricks() on it */ 00184 void initBricks(); 00185 /** Updates the moving powerups on map */ 00186 void update(); 00187 /** Creates powerUps depending on the PowerUpDropChancePct */ 00188 void onBrickHit(const Brick&); 00189 /** Draws all the bricks on the level, as well as powerUps */ 00190 void drawMap(N5110 &lcd); 00191 /** Checks if brick <-> ball collided */ 00192 void checkCollision(Ball &ball, Paddle &paddle); 00193 /** Resolves the collision */ 00194 void resolveCollision(const Brick &brick, GameObject &obj); 00195 /** Checks if we cleared the level and if we won 00196 * @return true if level is cleared 00197 */ 00198 bool checkLevel(); 00199 /** Resets the whole map when game is (re-)started */ 00200 void reset(); 00201 /** Adds the brick to the vector */ 00202 void addBrick(Brick& brick) { bricks.push_back(brick); } 00203 /** Stores the score of the game */ 00204 int score; 00205 }; 00206 #endif
Generated on Thu Jul 14 2022 02:02:53 by
1.7.2