contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Tue May 07 15:17:21 2019 +0000
Revision:
40:13b8467526d0
Parent:
36:30dde2e7aa11
Final Submission. I have read and agreed with Statement of Academic Integrity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OmarAlebiary 6:958376d55d70 1 #ifndef ROCKETRACER_H
OmarAlebiary 6:958376d55d70 2 #define ROCKETRACER_H
OmarAlebiary 6:958376d55d70 3 #include "mbed.h"
OmarAlebiary 6:958376d55d70 4 #include "N5110.h"
OmarAlebiary 9:edb39a8334ee 5 #include <cstdlib>
OmarAlebiary 9:edb39a8334ee 6 #include <ctime>
OmarAlebiary 6:958376d55d70 7 #include "Gamepad.h"
OmarAlebiary 27:771d186b1bc8 8 #include "GameSprites.h"
OmarAlebiary 28:39607fb67e88 9 #include "GameTones.h"
OmarAlebiary 30:c5060010a1e6 10 #include "FXOS8700CQ.h"
OmarAlebiary 29:e660274d8222 11
OmarAlebiary 17:989ff25612ad 12 /** RocketRacer class
OmarAlebiary 6:958376d55d70 13
OmarAlebiary 30:c5060010a1e6 14 @brief University of Leeds,C++ class containing the game methods and the interface
OmarAlebiary 17:989ff25612ad 15
OmarAlebiary 17:989ff25612ad 16 @version 1.0
OmarAlebiary 17:989ff25612ad 17
OmarAlebiary 17:989ff25612ad 18 @author Omar Alebiary
OmarAlebiary 17:989ff25612ad 19
OmarAlebiary 17:989ff25612ad 20 @date April 2019
OmarAlebiary 17:989ff25612ad 21
OmarAlebiary 17:989ff25612ad 22 @code
OmarAlebiary 17:989ff25612ad 23 #include "mbed.h"
OmarAlebiary 17:989ff25612ad 24 #include "N5110.h"
OmarAlebiary 17:989ff25612ad 25 #include "Gamepad.h"
OmarAlebiary 17:989ff25612ad 26 #include "RocketRacer.h"
OmarAlebiary 17:989ff25612ad 27
OmarAlebiary 17:989ff25612ad 28 // objects
OmarAlebiary 17:989ff25612ad 29 Gamepad pad;
OmarAlebiary 29:e660274d8222 30 RocketRacer Rocket_Racer;
OmarAlebiary 28:39607fb67e88 31
OmarAlebiary 19:ad9dc8e418c9 32
OmarAlebiary 17:989ff25612ad 33 // prototypes
OmarAlebiary 17:989ff25612ad 34 void setup();
OmarAlebiary 17:989ff25612ad 35
OmarAlebiary 17:989ff25612ad 36 int main(){
OmarAlebiary 17:989ff25612ad 37
OmarAlebiary 28:39607fb67e88 38 //initializes the gamepad and the lcd
OmarAlebiary 17:989ff25612ad 39 setup();
OmarAlebiary 28:39607fb67e88 40
OmarAlebiary 28:39607fb67e88 41 // this method displays the game scoresand frames
OmarAlebiary 28:39607fb67e88 42 // used in the gameplay
OmarAlebiary 28:39607fb67e88 43
OmarAlebiary 28:39607fb67e88 44 Rocket_Racer.Main_Game_Display(lcd);
OmarAlebiary 35:4c9b39c6fd72 45 // this method checks the accelerometer state
OmarAlebiary 35:4c9b39c6fd72 46 // if it rolled right prints "its right" else if it rolled
OmarAlebiary 35:4c9b39c6fd72 47 // left prints "its left" else prints "centre"
OmarAlebiary 36:30dde2e7aa11 48 Rocket_Racer.accelerometer_position(pad);
OmarAlebiary 28:39607fb67e88 49
OmarAlebiary 28:39607fb67e88 50 // this method checks the joystick position
OmarAlebiary 28:39607fb67e88 51 // if it's right prints "its right" else if its
OmarAlebiary 28:39607fb67e88 52 // left prints "its left" else prints "centre"
OmarAlebiary 28:39607fb67e88 53 Rocket_Racer.Joystick_position(pad);
OmarAlebiary 28:39607fb67e88 54 //this method generates a random number between 1 and 3
OmarAlebiary 28:39607fb67e88 55 //and prints it to the terminal
OmarAlebiary 28:39607fb67e88 56 Rocket_Racer.Generate_New_Enemy();
OmarAlebiary 28:39607fb67e88 57 //this method checks if the rocket collide with the enemy
OmarAlebiary 28:39607fb67e88 58 //and displays game over screen
OmarAlebiary 28:39607fb67e88 59 Rocket_Racer.Check_Enemy_Dead(lcd,pad);
OmarAlebiary 28:39607fb67e88 60 //main game loop that has all the method calls in it
OmarAlebiary 17:989ff25612ad 61 while(1){
OmarAlebiary 22:3e6ff378d651 62 Rocket_Racer.Game_Loop(lcd,pad);
OmarAlebiary 17:989ff25612ad 63 }
OmarAlebiary 28:39607fb67e88 64 //method that adds difficulty to the game
OmarAlebiary 28:39607fb67e88 65 //and has 6 levels of difficulty
OmarAlebiary 28:39607fb67e88 66 Rocket_Racer.Game_difficulty(pad);
OmarAlebiary 28:39607fb67e88 67 //this method places the enemy sprite according
OmarAlebiary 28:39607fb67e88 68 //to the randomly generated number(1->3)& the current phase
OmarAlebiary 28:39607fb67e88 69 //i added the 2nd and 3rd argument manually for testing
OmarAlebiary 28:39607fb67e88 70 Rocket_Racer.enemy_position(lcd,2,3);
OmarAlebiary 28:39607fb67e88 71 //this method places the player sprite
OmarAlebiary 28:39607fb67e88 72 //according to the postion of the joystick
OmarAlebiary 28:39607fb67e88 73 //i added the 2nd argument manually for testing
OmarAlebiary 28:39607fb67e88 74 Rocket_Racer.player_position(lcd, 3);
OmarAlebiary 28:39607fb67e88 75 //this method displays the gameover screen
OmarAlebiary 28:39607fb67e88 76 //and the high score achieved
OmarAlebiary 28:39607fb67e88 77 Rocket_Racer.End_Game(pad,lcd);
OmarAlebiary 17:989ff25612ad 78
OmarAlebiary 17:989ff25612ad 79
OmarAlebiary 17:989ff25612ad 80 }
OmarAlebiary 17:989ff25612ad 81
OmarAlebiary 17:989ff25612ad 82 @endcode
OmarAlebiary 17:989ff25612ad 83 */
OmarAlebiary 16:93a8147a4358 84
OmarAlebiary 6:958376d55d70 85
OmarAlebiary 6:958376d55d70 86 class RocketRacer{
OmarAlebiary 6:958376d55d70 87
OmarAlebiary 6:958376d55d70 88 public:
OmarAlebiary 17:989ff25612ad 89 /**
OmarAlebiary 29:e660274d8222 90 * @brief Default Constructor
OmarAlebiary 29:e660274d8222 91 * @details Creates the object of class RocketRacer
OmarAlebiary 17:989ff25612ad 92 */
OmarAlebiary 13:cec06eb1d7b0 93 RocketRacer();
OmarAlebiary 29:e660274d8222 94 /**
OmarAlebiary 29:e660274d8222 95 * @brief Default destructor
OmarAlebiary 29:e660274d8222 96 * @details destroys the object of class RocketRacer
OmarAlebiary 29:e660274d8222 97 */
OmarAlebiary 29:e660274d8222 98 ~RocketRacer();
OmarAlebiary 17:989ff25612ad 99 /**
OmarAlebiary 17:989ff25612ad 100 * @brief method that has all the screen rendering
OmarAlebiary 17:989ff25612ad 101 * @param lcd @details calls the lcd object to draw strings and objects on the display
OmarAlebiary 17:989ff25612ad 102 */
OmarAlebiary 12:1d3b0218d8d0 103 void Main_Game_Display(N5110 &lcd);
OmarAlebiary 17:989ff25612ad 104 /**
OmarAlebiary 17:989ff25612ad 105 * @brief method that dispalys the game over screen with the high score achieved
OmarAlebiary 17:989ff25612ad 106 * @param lcd @details calls the lcd object to draw strings on the display
OmarAlebiary 17:989ff25612ad 107 * @param pad @details calls the Gamepad object to access methods from the Gamepad class
OmarAlebiary 17:989ff25612ad 108 */
OmarAlebiary 12:1d3b0218d8d0 109 void End_Game(Gamepad &pad,N5110 &lcd);
OmarAlebiary 13:cec06eb1d7b0 110
OmarAlebiary 17:989ff25612ad 111 /**
OmarAlebiary 17:989ff25612ad 112 * @brief method that adds difficulty to the game after proceeding each level
OmarAlebiary 17:989ff25612ad 113 * @param pad @details calls the Gamepad object to access methods from the Gamepad class
OmarAlebiary 17:989ff25612ad 114 */
OmarAlebiary 12:1d3b0218d8d0 115 void Game_difficulty(Gamepad &pad);
OmarAlebiary 17:989ff25612ad 116
OmarAlebiary 17:989ff25612ad 117 /**
OmarAlebiary 17:989ff25612ad 118 * @brief method that generates random enemies
OmarAlebiary 17:989ff25612ad 119 * @param none @details seeds the rand function then generate a random enemies
OmarAlebiary 17:989ff25612ad 120 */
OmarAlebiary 11:d4aaa959bb20 121 void Generate_New_Enemy();
OmarAlebiary 17:989ff25612ad 122 /**
OmarAlebiary 17:989ff25612ad 123 * @brief method that checks if the randomly generated enemies crossed the player
OmarAlebiary 17:989ff25612ad 124 * and calls the End_Game method accordingly if they collide else increments the score
OmarAlebiary 17:989ff25612ad 125 * @param lcd @details calls the lcd object to be passed to the End_Game method
OmarAlebiary 17:989ff25612ad 126 * @param pad @details calls the Gamepad object to be passed to the End_Game method
OmarAlebiary 17:989ff25612ad 127 */
OmarAlebiary 11:d4aaa959bb20 128 void Check_Enemy_Dead(N5110 &lcd,Gamepad &pad);
OmarAlebiary 17:989ff25612ad 129 /**
OmarAlebiary 17:989ff25612ad 130 * @brief method that checks the joystick (and L &R buttons) direction whether its left,right or centre
OmarAlebiary 17:989ff25612ad 131 * and increment or decrement according to the current position
OmarAlebiary 17:989ff25612ad 132 * @param pad @details calls the Gamepad object to access the get_direction method from the Gamepad class
OmarAlebiary 17:989ff25612ad 133 */
OmarAlebiary 11:d4aaa959bb20 134 void Joystick_position(Gamepad &pad);
OmarAlebiary 17:989ff25612ad 135 /**
OmarAlebiary 17:989ff25612ad 136 * @brief method that has the game loop which calls all the methods(from the same class)for the game to operate
OmarAlebiary 17:989ff25612ad 137 * @param lcd @details calls the lcd object to be passed to the methods called inside this method
OmarAlebiary 17:989ff25612ad 138 * @param pad @details calls the Gamepad object to be passed to the methods called inside this method
OmarAlebiary 17:989ff25612ad 139 */
OmarAlebiary 12:1d3b0218d8d0 140 void Game_Loop(N5110 &lcd,Gamepad &pad);
OmarAlebiary 17:989ff25612ad 141 /**
OmarAlebiary 17:989ff25612ad 142 * @brief method that draws the rocket sprite according to the player position
OmarAlebiary 17:989ff25612ad 143 * @param lcd @details calls the lcd object to access the drawSprite method
OmarAlebiary 17:989ff25612ad 144 * @param RocketPosition @details the position of the rocket
OmarAlebiary 17:989ff25612ad 145 */
OmarAlebiary 9:edb39a8334ee 146 void player_position(N5110 &lcd,char RocketPosition);
OmarAlebiary 17:989ff25612ad 147 /**
OmarAlebiary 17:989ff25612ad 148 * @brief method that draws the enemy sprite
OmarAlebiary 17:989ff25612ad 149 * @param lcd @details calls the lcd object to access the drawSprite method
OmarAlebiary 17:989ff25612ad 150 * @param place @details the position of the rocket
OmarAlebiary 17:989ff25612ad 151 * @param phase @details the phase of the rocket
OmarAlebiary 17:989ff25612ad 152 */
OmarAlebiary 9:edb39a8334ee 153 void enemy_position(N5110 &lcd,int place, int phase);
OmarAlebiary 29:e660274d8222 154 /**
OmarAlebiary 29:e660274d8222 155 * @brief Sets the position
OmarAlebiary 29:e660274d8222 156 * @param first_enemy_position @details position of the first enemy (as an int)
OmarAlebiary 29:e660274d8222 157 */
OmarAlebiary 29:e660274d8222 158 void set_first_position(int first_enemy_position);
OmarAlebiary 29:e660274d8222 159 /**
OmarAlebiary 29:e660274d8222 160 * @brief Sets the position
OmarAlebiary 29:e660274d8222 161 * @param second_enemy_position @details position of the second enemy (as an int)
OmarAlebiary 29:e660274d8222 162 */
OmarAlebiary 29:e660274d8222 163 void set_second_position(int second_enemy_position);
OmarAlebiary 29:e660274d8222 164 /**
OmarAlebiary 29:e660274d8222 165 * @brief Sets the phase
OmarAlebiary 29:e660274d8222 166 * @param enemy_phase @details phase of the enemy (as an int)
OmarAlebiary 29:e660274d8222 167 */
OmarAlebiary 29:e660274d8222 168 void set_enemy_phase(int enemy_phase);
OmarAlebiary 29:e660274d8222 169 /**
OmarAlebiary 29:e660274d8222 170 * @brief Sets the game speed
OmarAlebiary 29:e660274d8222 171 * @param game_speed @details speed of the game (as an int)
OmarAlebiary 29:e660274d8222 172 */
OmarAlebiary 29:e660274d8222 173 void set_game_speed(int game_speed);
OmarAlebiary 29:e660274d8222 174 /**
OmarAlebiary 29:e660274d8222 175 * @brief Sets the game score
OmarAlebiary 29:e660274d8222 176 * @param score @details score of the game (as an int)
OmarAlebiary 29:e660274d8222 177 */
OmarAlebiary 29:e660274d8222 178 void set_game_score(int score);
OmarAlebiary 29:e660274d8222 179 /**
OmarAlebiary 29:e660274d8222 180 * @brief set enemy dead flag
OmarAlebiary 29:e660274d8222 181 * @param enemy_dead @details flag to indicate dead enemy(as a bool)
OmarAlebiary 29:e660274d8222 182 */
OmarAlebiary 29:e660274d8222 183 void set_enemy_dead(bool enemy_dead) ;
OmarAlebiary 29:e660274d8222 184 /**
OmarAlebiary 29:e660274d8222 185 * @brief set control flag
OmarAlebiary 29:e660274d8222 186 * @param control @details flag for joystick control(as a bool)
OmarAlebiary 29:e660274d8222 187 */
OmarAlebiary 29:e660274d8222 188 void set_control(bool control);
OmarAlebiary 29:e660274d8222 189 /**
OmarAlebiary 29:e660274d8222 190 * @brief set initial position
OmarAlebiary 29:e660274d8222 191 * @param Init_position @details initial position of the player(as an int)
OmarAlebiary 29:e660274d8222 192 */
OmarAlebiary 29:e660274d8222 193 void set_init_position(int Init_position);
OmarAlebiary 30:c5060010a1e6 194 /**
OmarAlebiary 30:c5060010a1e6 195 * @brief checks accelerometer positon
OmarAlebiary 30:c5060010a1e6 196 * @param Init_position @details initial position of the player(as an int)
OmarAlebiary 30:c5060010a1e6 197 * @param pad @details calls the Gamepad object to be passed to the methods called inside this method
OmarAlebiary 30:c5060010a1e6 198 */
OmarAlebiary 30:c5060010a1e6 199 void accelerometer_position(Gamepad &pad);
OmarAlebiary 30:c5060010a1e6 200 /**
OmarAlebiary 30:c5060010a1e6 201 * @brief method that has the game loop which calls all the methods and control with accelerometer
OmarAlebiary 30:c5060010a1e6 202 * @param lcd @details calls the lcd object to be passed to the methods called inside this method
OmarAlebiary 30:c5060010a1e6 203 * @param pad @details calls the Gamepad object to be passed to the methods called inside this method
OmarAlebiary 30:c5060010a1e6 204 */
OmarAlebiary 30:c5060010a1e6 205 void Game_Loop_accelerometer(N5110 &lcd,Gamepad &pad);
OmarAlebiary 40:13b8467526d0 206 /**
OmarAlebiary 40:13b8467526d0 207 * @brief method that generates one random enemy
OmarAlebiary 40:13b8467526d0 208 * @details seeds the rand function then generate a random enemy between 1->3
OmarAlebiary 40:13b8467526d0 209 */
OmarAlebiary 40:13b8467526d0 210 void Generate_One_Enemy();
OmarAlebiary 9:edb39a8334ee 211
OmarAlebiary 31:4d4a9d78cae5 212
OmarAlebiary 31:4d4a9d78cae5 213
OmarAlebiary 9:edb39a8334ee 214 private:
OmarAlebiary 28:39607fb67e88 215
OmarAlebiary 15:8a768106c297 216 int first_enemy_position;
OmarAlebiary 15:8a768106c297 217 int second_enemy_position;
OmarAlebiary 15:8a768106c297 218 int enemy_phase;
OmarAlebiary 13:cec06eb1d7b0 219 int game_speed;
OmarAlebiary 13:cec06eb1d7b0 220 int score;
OmarAlebiary 40:13b8467526d0 221 float pitchAngle;
OmarAlebiary 14:8df7e6fced07 222 char Init_position;
OmarAlebiary 13:cec06eb1d7b0 223 bool enemy_dead;
OmarAlebiary 13:cec06eb1d7b0 224 bool control;
OmarAlebiary 28:39607fb67e88 225 GameTones tones;
OmarAlebiary 28:39607fb67e88 226
OmarAlebiary 29:e660274d8222 227
OmarAlebiary 30:c5060010a1e6 228
OmarAlebiary 6:958376d55d70 229 };
OmarAlebiary 6:958376d55d70 230
OmarAlebiary 6:958376d55d70 231 #endif