Simple Breakout game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:92b180c8d407 1 #ifndef BREAKOUTENGINE_H
jamesheavey 0:92b180c8d407 2 #define BREAKOUTENGINE_H
jamesheavey 0:92b180c8d407 3
jamesheavey 0:92b180c8d407 4 #include "mbed.h"
jamesheavey 0:92b180c8d407 5 #include "N5110.h"
jamesheavey 0:92b180c8d407 6 #include "Gamepad.h"
jamesheavey 0:92b180c8d407 7 #include "Ball.h"
jamesheavey 0:92b180c8d407 8 #include "Paddle.h"
jamesheavey 0:92b180c8d407 9 #include "Brick.h"
jamesheavey 0:92b180c8d407 10 #include "Laser.h"
jamesheavey 0:92b180c8d407 11 #include "Life_Powerup.h"
jamesheavey 0:92b180c8d407 12
jamesheavey 0:92b180c8d407 13 #include <list>
jamesheavey 0:92b180c8d407 14 #include <iterator>
jamesheavey 0:92b180c8d407 15 #include <ctime> // For time()
jamesheavey 0:92b180c8d407 16 #include <cstdlib> // For srand() and rand()
jamesheavey 0:92b180c8d407 17
jamesheavey 0:92b180c8d407 18 #define GAP_TOP 10
jamesheavey 0:92b180c8d407 19 #define GAP 2
jamesheavey 0:92b180c8d407 20
jamesheavey 0:92b180c8d407 21 /** BreakoutEngine Class
jamesheavey 0:92b180c8d407 22 @author James Heavey, University of Leeds
jamesheavey 0:92b180c8d407 23 @brief Controls the Breakout game
jamesheavey 0:92b180c8d407 24 @date May 2019
jamesheavey 0:92b180c8d407 25 */
jamesheavey 0:92b180c8d407 26
jamesheavey 0:92b180c8d407 27 class BreakoutEngine
jamesheavey 0:92b180c8d407 28 {
jamesheavey 0:92b180c8d407 29
jamesheavey 0:92b180c8d407 30 public:
jamesheavey 0:92b180c8d407 31
jamesheavey 0:92b180c8d407 32 /** Constructor declaration */
jamesheavey 0:92b180c8d407 33 BreakoutEngine();
jamesheavey 0:92b180c8d407 34
jamesheavey 0:92b180c8d407 35 /** Destructor declaration */
jamesheavey 0:92b180c8d407 36 ~BreakoutEngine();
jamesheavey 0:92b180c8d407 37
jamesheavey 0:92b180c8d407 38 /** Initialises game variables
jamesheavey 0:92b180c8d407 39 * @param paddle_width @details integer width of paddle
jamesheavey 0:92b180c8d407 40 * @param paddle_height @details integer height of paddle
jamesheavey 0:92b180c8d407 41 * @param ball_size @details integer diameter of ball in pixels
jamesheavey 0:92b180c8d407 42 * @param speed @details integer initial speed of ball
jamesheavey 0:92b180c8d407 43 */
jamesheavey 0:92b180c8d407 44 void init(int paddle_width,int paddle_height,int ball_size,int speed);
jamesheavey 0:92b180c8d407 45
jamesheavey 0:92b180c8d407 46 /** Reads inputs from the Gamepad
jamesheavey 0:92b180c8d407 47 * @param pad @details a Gamepad pointer
jamesheavey 0:92b180c8d407 48 */
jamesheavey 0:92b180c8d407 49 void read_input(Gamepad &pad);
jamesheavey 0:92b180c8d407 50
jamesheavey 0:92b180c8d407 51 /** Updates the Game attributes
jamesheavey 0:92b180c8d407 52 * @param pad @details a Gamepad pointer
jamesheavey 0:92b180c8d407 53 */
jamesheavey 0:92b180c8d407 54 void update(Gamepad &pad);
jamesheavey 0:92b180c8d407 55
jamesheavey 0:92b180c8d407 56 /** Draws the objects' at their respective coordinates on the LCD
jamesheavey 0:92b180c8d407 57 * @param lcd @details a N5110 pointer
jamesheavey 0:92b180c8d407 58 */
jamesheavey 0:92b180c8d407 59 void draw(N5110 &lcd);
jamesheavey 0:92b180c8d407 60
jamesheavey 0:92b180c8d407 61 /** Updates the Gamepad LEDs with lives remaining
jamesheavey 0:92b180c8d407 62 * @param pad @details a Gamepad pointer
jamesheavey 0:92b180c8d407 63 */
jamesheavey 0:92b180c8d407 64 void lives_leds(Gamepad &pad);
jamesheavey 0:92b180c8d407 65
jamesheavey 0:92b180c8d407 66 /** Sets the variable _prev_score to current score
jamesheavey 0:92b180c8d407 67 * @param prev_score @details the new previous score which is the score achieved at victory
jamesheavey 0:92b180c8d407 68 */
jamesheavey 0:92b180c8d407 69 void set_prev_score(int prev_score);
jamesheavey 0:92b180c8d407 70
jamesheavey 0:92b180c8d407 71 /** Increments the laser index */
jamesheavey 0:92b180c8d407 72 void inc_index();
jamesheavey 0:92b180c8d407 73
jamesheavey 0:92b180c8d407 74 /** Resets the laser index to 0 */
jamesheavey 0:92b180c8d407 75 void reset_index();
jamesheavey 0:92b180c8d407 76
jamesheavey 0:92b180c8d407 77 /** Returns the game (and all relevant objects) to initialised state */
jamesheavey 0:92b180c8d407 78 void reset_game();
jamesheavey 0:92b180c8d407 79
jamesheavey 0:92b180c8d407 80 /** Sets the variable _number_left to 18 */
jamesheavey 0:92b180c8d407 81 void reset_num_left();
jamesheavey 0:92b180c8d407 82
jamesheavey 0:92b180c8d407 83 /** Increment the multiplier if continue is selected upon victory */
jamesheavey 0:92b180c8d407 84 void inc_mult();
jamesheavey 0:92b180c8d407 85
jamesheavey 0:92b180c8d407 86 /** Resets the multiplier to 0
jamesheavey 0:92b180c8d407 87 * @returns _prev_score
jamesheavey 0:92b180c8d407 88 */
jamesheavey 0:92b180c8d407 89 void reset_mult();
jamesheavey 0:92b180c8d407 90
jamesheavey 0:92b180c8d407 91 /** Sets the paddle motion options for use in game
jamesheavey 0:92b180c8d407 92 * @param tilt @details a bool that sets tilt if true and joystick if false
jamesheavey 0:92b180c8d407 93 * @param sens @details a float that is derived from the pot, it multiplies the velocity of the paddle
jamesheavey 0:92b180c8d407 94 */
jamesheavey 0:92b180c8d407 95 void set_paddle_motion(bool tilt, float sens);
jamesheavey 0:92b180c8d407 96
jamesheavey 0:92b180c8d407 97 /** Sets the powerup to a random x position on screen if conditions are met */
jamesheavey 0:92b180c8d407 98 void check_life_powerup();
jamesheavey 0:92b180c8d407 99
jamesheavey 0:92b180c8d407 100 /** Resets the paddle lives to 6, used to reset the game */
jamesheavey 0:92b180c8d407 101 void reset_paddle_lives();
jamesheavey 0:92b180c8d407 102
jamesheavey 0:92b180c8d407 103 /** Decrements the number of bricks left on screen when one is destroyed */
jamesheavey 0:92b180c8d407 104 void dec_num_left();
jamesheavey 0:92b180c8d407 105
jamesheavey 0:92b180c8d407 106 /** Returns the number of Bricks remaining on screen
jamesheavey 0:92b180c8d407 107 * @returns _number_left
jamesheavey 0:92b180c8d407 108 */
jamesheavey 0:92b180c8d407 109 int get_num_left();
jamesheavey 0:92b180c8d407 110
jamesheavey 0:92b180c8d407 111 /** Returns the score achieved on victory in the previous iteration of the game
jamesheavey 0:92b180c8d407 112 * @returns _prev_score
jamesheavey 0:92b180c8d407 113 */
jamesheavey 0:92b180c8d407 114 int get_prev_score();
jamesheavey 0:92b180c8d407 115
jamesheavey 0:92b180c8d407 116 /** Returns the number of lives remaining
jamesheavey 0:92b180c8d407 117 * @returns the paddle variable _lives
jamesheavey 0:92b180c8d407 118 */
jamesheavey 0:92b180c8d407 119 int get_paddle_lives();
jamesheavey 0:92b180c8d407 120
jamesheavey 0:92b180c8d407 121 /** Returns the current multiplier value
jamesheavey 0:92b180c8d407 122 * @returns _multiplier
jamesheavey 0:92b180c8d407 123 */
jamesheavey 0:92b180c8d407 124 int get_mult();
jamesheavey 0:92b180c8d407 125
jamesheavey 0:92b180c8d407 126 /** Checks if the ball goes past the screen y boundary
jamesheavey 0:92b180c8d407 127 * @param pad @details a Gamepad pointer
jamesheavey 0:92b180c8d407 128 * @returns a bool; true if ball is off screen, false
jamesheavey 0:92b180c8d407 129 */
jamesheavey 0:92b180c8d407 130 bool check_loss(Gamepad &pad);
jamesheavey 0:92b180c8d407 131
jamesheavey 0:92b180c8d407 132 /** Returns the current score to print to LCD in the game
jamesheavey 0:92b180c8d407 133 * @returns _score
jamesheavey 0:92b180c8d407 134 */
jamesheavey 0:92b180c8d407 135 int get_score();
jamesheavey 0:92b180c8d407 136
jamesheavey 0:92b180c8d407 137 private:
jamesheavey 0:92b180c8d407 138
jamesheavey 0:92b180c8d407 139 void check_wall_collisions(Gamepad &pad);
jamesheavey 0:92b180c8d407 140 void check_paddle_collisions(Gamepad &pad);
jamesheavey 0:92b180c8d407 141 void check_brick_collisions(Gamepad &pad);
jamesheavey 0:92b180c8d407 142 void check_laser_collisions(Gamepad &pad);
jamesheavey 0:92b180c8d407 143 void check_powerup_collisions(Gamepad &pad);
jamesheavey 0:92b180c8d407 144 void print_scores(N5110 &lcd);
jamesheavey 0:92b180c8d407 145
jamesheavey 0:92b180c8d407 146 int _paddle_width;
jamesheavey 0:92b180c8d407 147 int _paddle_height;
jamesheavey 0:92b180c8d407 148 int _ball_size;
jamesheavey 0:92b180c8d407 149 int _speed;
jamesheavey 0:92b180c8d407 150 int _index;
jamesheavey 0:92b180c8d407 151 int _multiplier;
jamesheavey 0:92b180c8d407 152 int _paddley;
jamesheavey 0:92b180c8d407 153 int _number_left;
jamesheavey 0:92b180c8d407 154 int _prev_score;
jamesheavey 0:92b180c8d407 155 int _score;
jamesheavey 0:92b180c8d407 156 double _cool_time;
jamesheavey 0:92b180c8d407 157
jamesheavey 0:92b180c8d407 158 Direction _d;
jamesheavey 0:92b180c8d407 159 float _mag;
jamesheavey 0:92b180c8d407 160
jamesheavey 0:92b180c8d407 161 std::list<Laser> listofLasers;
jamesheavey 0:92b180c8d407 162 std::list<Laser>::iterator it_L;
jamesheavey 0:92b180c8d407 163
jamesheavey 0:92b180c8d407 164 std::list<Brick> listofBricks;
jamesheavey 0:92b180c8d407 165 std::list<Brick>::iterator it_R;
jamesheavey 0:92b180c8d407 166
jamesheavey 0:92b180c8d407 167 Paddle _paddle;
jamesheavey 0:92b180c8d407 168
jamesheavey 0:92b180c8d407 169 Ball _ball;
jamesheavey 0:92b180c8d407 170
jamesheavey 0:92b180c8d407 171 Brick _brick11;
jamesheavey 0:92b180c8d407 172 Brick _brick12;
jamesheavey 0:92b180c8d407 173 Brick _brick13;
jamesheavey 0:92b180c8d407 174 Brick _brick14;
jamesheavey 0:92b180c8d407 175 Brick _brick15;
jamesheavey 0:92b180c8d407 176 Brick _brick16;
jamesheavey 0:92b180c8d407 177 Brick _brick21;
jamesheavey 0:92b180c8d407 178 Brick _brick22;
jamesheavey 0:92b180c8d407 179 Brick _brick23;
jamesheavey 0:92b180c8d407 180 Brick _brick24;
jamesheavey 0:92b180c8d407 181 Brick _brick25;
jamesheavey 0:92b180c8d407 182 Brick _brick26;
jamesheavey 0:92b180c8d407 183 Brick _brick31;
jamesheavey 0:92b180c8d407 184 Brick _brick32;
jamesheavey 0:92b180c8d407 185 Brick _brick33;
jamesheavey 0:92b180c8d407 186 Brick _brick34;
jamesheavey 0:92b180c8d407 187 Brick _brick35;
jamesheavey 0:92b180c8d407 188 Brick _brick36;
jamesheavey 0:92b180c8d407 189
jamesheavey 0:92b180c8d407 190 Laser _laser1;
jamesheavey 0:92b180c8d407 191 Laser _laser2;
jamesheavey 0:92b180c8d407 192 Laser _laser3;
jamesheavey 0:92b180c8d407 193
jamesheavey 0:92b180c8d407 194 Life_Powerup _powerup;
jamesheavey 0:92b180c8d407 195
jamesheavey 0:92b180c8d407 196 };
jamesheavey 0:92b180c8d407 197
jamesheavey 0:92b180c8d407 198 #endif