Game
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 ELEC2645 Embedded Systems Project 00003 School of Electronic & Electrical Engineering 00004 University of Leeds 00005 2019/20 00006 00007 Name: Henry Triff 00008 Username: el18ht 00009 Student ID Number: 201224295 00010 Date: 10/02/2020 00011 00012 DIAGNOSTICS AND TEST FUNCTIONS 00013 > I created a separate program that allows me to test functions of this code and to test and view the maps that i have 00014 created. The code is 'quick and dirty' but works well. the controls are a little confusing. Stick moves the view of the track 00015 around and X and A zoom in and out. Pots are used to show/hide different aspects of the map. 00016 > It can be found here: https://os.mbed.com//users/HenryWTriff/code/ELEC2645_Project_el18ht_Map_Viewer/ 00017 */ 00018 00019 //DEBUG DEFINITIONS 00020 //#define DEBUG 00021 00022 //LIBRARIES 00023 #include "mbed.h" 00024 #include "Gamepad.h" 00025 #include "N5110.h" 00026 #include "FXOS8700CQ.h" 00027 #include "SDFileSystem.h" 00028 #include "Graphics.h" 00029 #include "Mechanics.h" 00030 #include "Menu.h" 00031 #include "LEDs.h" 00032 #include "Ghost.h" 00033 #include <string> 00034 00035 #ifndef STRUCTS 00036 #define STRUCTS 00037 00038 //STRUCTS 00039 struct Point_2D { 00040 float x; 00041 float y; 00042 }; 00043 00044 struct Line_2D { 00045 Point_2D from; 00046 Point_2D to; 00047 }; 00048 00049 struct Square_2D { 00050 Point_2D TL; 00051 Point_2D BR; 00052 }; 00053 struct Triangle_2D { 00054 Point_2D TL; 00055 Point_2D BR; 00056 int Type; //The direction of the hypoentuse on the right angled triangle ( 1 = Top right, 2 = Bottom right, 3 = Bottom left, 4 = Top Left ) 00057 }; 00058 00059 struct Sprite_2D { 00060 float x; 00061 float y; 00062 int type; 00063 }; 00064 00065 struct Map_Data { 00066 //Information about the number of elements in each array 00067 int number_of_track_lines; 00068 int number_of_dotted_lines; 00069 int number_of_sprites; 00070 int number_of_walls; 00071 int number_of_off_track_squares; 00072 int number_of_off_track_triangles; 00073 int number_of_out_of_bounds_squares; 00074 int number_of_out_of_bounds_triangles; 00075 int number_of_gates; 00076 int number_of_boost_plates; 00077 }; 00078 00079 struct Time { 00080 int mins; 00081 int secs; 00082 int milis; 00083 }; 00084 00085 struct Gyro_Data { 00086 float ax; 00087 float ay; 00088 float az; 00089 float mx; 00090 float my; 00091 float mz; 00092 }; 00093 00094 #endif 00095 00096 //GLOBAL VARIABLES 00097 #ifndef ENUMS 00098 #define ENUMS 00099 enum track {Small, Medium, Large}; //Track Names 00100 enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; //Car Names (Stupid is now the alien space ship) 00101 enum sprites {Flag, Helicopter, People_Standing_1, People_Standing_2, People_Cheering}; 00102 #endif 00103 00104 //OBJECTS 00105 Gamepad Device; //Controller 00106 N5110 LCD; //LCD 00107 FXOS8700CQ Gyro(I2C_SDA,I2C_SCL); //Gyroscope 00108 SDFileSystem SD(PTE3, PTE1, PTE2, PTE4, "sd"); //SD card (For recording ghost data) 00109 Graphics Graphics; //Graphics Class 00110 Mechanics Mechanics; //Mechanics Class 00111 Menu Menu; //Menu Class 00112 LEDs LEDs; //LEDs Class 00113 Ghost Ghost; //Ghost Class (For racing against a previos version of you) 00114 00115 #include "Maps.h" 00116 00117 //Transform 00118 int angle = 0; //The angle of roation of the track 00119 Point_2D translation = {0,0}; //The translation of the track 00120 float horizon_factor = 0.02; //The translation of the track 00121 float squish = 0.15; //The amount of squish of the track 00122 00123 //Vehicle parameters 00124 float speed; //Current speed (Rate of change of translation [see above] ) 00125 float max_speed; //Max speed the car can go 00126 int handling; //The maximum rate of change of angle of the track per frame 00127 float off_track_speed; //Maximum off track speed 00128 float acceleration; 00129 float deceleration; 00130 int car_model = Basic; 00131 00132 //Mechanics 00133 int lap_gate = 0; //The initial lap gate 00134 int laps = 1; //The current lap number 00135 int number_of_laps = 3; //The number of laps 00136 int race_time = 0; //The number of frames elapsed so far since the start of the race 00137 bool ghost_enable = false; 00138 00139 //Game Settings 00140 int game_fps = 60; //Frame rate 00141 bool leds = true; //Turning the LEDs on / off (Useful for people with epilepsy as they flash quickly) 00142 bool gyro_enabled = false; //Gyro steering option 00143 00144 int main() 00145 { 00146 #ifdef DEBUG 00147 printf("INITIALISATION\n"); 00148 #endif 00149 00150 LCD.init(); //Initialising the LCD 00151 Device.init(); //Initialising the Gamepad 00152 Gyro.init(); //Initialising the gyro 00153 Graphics.Change_Contrast(LCD,Device); //Adust Contrast to the amount on the pot 00154 00155 #ifdef DEBUG 00156 printf("LOGO\n"); 00157 #endif 00158 00159 //START LOGO 00160 Graphics.Draw_Logo(LCD); 00161 00162 //MENU 00163 #ifdef DEBUG 00164 printf("MENU\n"); 00165 #endif 00166 do { 00167 while(Menu.Main_Menu(LCD, Device) == true) { 00168 Ghost.SD_init(SD,LCD); //Format SD Card 00169 } 00170 } while(Menu.Game_Setup(LCD, Device) == false); 00171 00172 //RETRIEVING STATIC VARIABLES FROM MENU CLASS 00173 car_model = Menu.Get_Car(); 00174 number_of_laps = Menu.Get_Laps(); 00175 ghost_enable = Menu.Get_Mode(); 00176 leds = Menu.Get_Setting_LED(); 00177 gyro_enabled = Menu.Get_Setting_Gyro(); 00178 00179 //CAR PARAMETERS SETUP 00180 max_speed = Mechanics.Get_Max_Speed(car_model); 00181 acceleration = Mechanics.Get_Acceleration(car_model); 00182 deceleration = Mechanics.Get_Deceleration(car_model); 00183 off_track_speed = Mechanics.Get_Off_Road_Speed(car_model); 00184 handling = Mechanics.Get_Handling(car_model); 00185 00186 #ifdef DEBUG 00187 printf("GAME RUN\n"); 00188 printf("MAP: %i LAPS: %i CAR: %i\n",Menu.Get_Map(),number_of_laps, car_model); 00189 #endif 00190 00191 if(Menu.Get_Map() == Small) { 00192 00193 while(1) { //Main Game While loop 00194 if(ghost_enable == true) { //If ghost mode is enabled 00195 #ifdef DEBUG 00196 printf("READING GHOST DATA\n"); 00197 #endif 00198 Ghost.SD_Read(Small, car_model, SD); //Copy the ghost data from the SD card over to the flash memory (Make take a second) 00199 } 00200 00201 #ifdef DEBUG 00202 printf("COUNTDOWN\n"); 00203 #endif 00204 //COUNTDOWN 00205 for(int count = 3; count >= 0; count--) { //Loop for the countdown 00206 //Draw the map and the count down numbers 00207 LCD.clear(); 00208 Graphics.Draw_Map( 00209 translation, 00210 angle, 00211 squish, 00212 horizon_factor, 00213 (Line_2D *) Map_1_Track_Lines, 00214 (Line_2D *)Map_1_Track_Dotted_Lines, 00215 (Line_2D *)Map_1_Walls, 00216 (Sprite_2D *)Map_1_Sprites, 00217 (Triangle_2D *)Map_1_Boost_Plates, 00218 Map_1 , 00219 car_model, 00220 Ghost.Play(false, race_time), 00221 LCD 00222 ); 00223 Graphics.Start_Sequence(count, LCD); 00224 LCD.refresh(); 00225 LEDs.Start_Sequence(leds, count, Device); //Display the LED traffic light count down 00226 wait(1); 00227 } 00228 LEDs.Clear(Device); 00229 00230 #ifdef DEBUG 00231 printf("RACE START\n"); 00232 #endif 00233 00234 while(laps <= number_of_laps) { //Loop until the number of laps have been completed 00235 //MECHANICS 00236 laps = Mechanics.Get_Laps(laps, Map_1_Gates, Map_1 .number_of_gates, translation, lap_gate); //Get the current lap count 00237 lap_gate = Mechanics.Get_Gate(Map_1_Gates, Map_1 .number_of_gates, translation, lap_gate); //Get the current lap_gate 00238 angle = Mechanics.Get_Angle(angle, handling, gyro_enabled, Gyro, Device); //Get the current angle 00239 translation = Mechanics.Get_Translation(translation, angle, speed, Map_1_Out_Of_Bounds_Square, Map_1_Out_Of_Bounds_Triangle, Map_1 , Device); //Get the translation 00240 speed = Mechanics.Get_Speed( 00241 speed, 00242 max_speed, 00243 acceleration, 00244 deceleration, 00245 off_track_speed, 00246 translation, 00247 Map_1_Off_Track_Square, 00248 Map_1_Off_Track_Triangle, 00249 Map_1_Out_Of_Bounds_Square, 00250 Map_1_Out_Of_Bounds_Triangle, 00251 Map_1_Boost_Plates, 00252 Map_1 , 00253 Device 00254 ); //Get the speed of the player 00255 00256 //LEDS 00257 LEDs.Speed(leds, speed, max_speed, Device); //Change the LEDs depending on the speed 00258 00259 //GHOST 00260 if(ghost_enable == true) { //If ghost mode is enabled 00261 Ghost.Record(translation, race_time); //Record the position of the player 00262 } 00263 00264 //GRAPHICS 00265 LCD.clear(); //Clear the screen buffer 00266 Graphics.Change_Contrast(LCD,Device); //Adjust the contrast 00267 Graphics.Draw_Map( 00268 translation, 00269 angle, 00270 squish, 00271 horizon_factor, 00272 (Line_2D *) Map_1_Track_Lines, 00273 (Line_2D *)Map_1_Track_Dotted_Lines, 00274 (Line_2D *)Map_1_Walls, 00275 (Sprite_2D *)Map_1_Sprites, 00276 (Triangle_2D *)Map_1_Boost_Plates, 00277 Map_1 , 00278 car_model, 00279 Ghost.Play(ghost_enable, race_time), 00280 LCD 00281 ); //Draw the graphics 00282 Graphics.Draw_Laps(laps, LCD); //Draw the lap counter 00283 Graphics.Draw_Time(false, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the time 00284 LCD.refresh(); //Refresh the display 00285 wait((1 / float(game_fps))); //Wait for a short period of time 00286 race_time++; //Increase the race time 00287 00288 } 00289 //RACE OVER 00290 LEDs.Clear(Device); //Turn off the LEDs 00291 LCD.clear(); 00292 Graphics.Draw_Map( 00293 translation, 00294 angle, 00295 squish, 00296 horizon_factor, 00297 (Line_2D *) Map_1_Track_Lines, 00298 (Line_2D *)Map_1_Track_Dotted_Lines, 00299 (Line_2D *)Map_1_Walls, 00300 (Sprite_2D *)Map_1_Sprites, 00301 (Triangle_2D *)Map_1_Boost_Plates, 00302 Map_1 , 00303 car_model, 00304 Ghost.Play(false, race_time), 00305 LCD 00306 ); //Draw the graphics 00307 Graphics.Finish(LCD); //Display the finish logo 00308 Graphics.Draw_Time(true, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the race time 00309 LCD.refresh(); //Refresh the display 00310 if(ghost_enable == true) { //If ghost mode enabled 00311 #ifdef DEBUG 00312 printf("GHOST COPY\n"); 00313 #endif 00314 Ghost.Copy(race_time, Small, car_model, SD); //Copy over the ghost data to the SD card (if race time is quicker) 00315 } 00316 00317 //RESET ALL VARIABLES 00318 angle = 0; 00319 Point_2D test = {0,0}; 00320 translation = test; 00321 squish = 0.15; 00322 speed = 0; 00323 max_speed = 4; 00324 handling = 2; 00325 lap_gate = 0; 00326 laps = 1; 00327 race_time = 0; 00328 00329 wait(3); 00330 00331 #ifdef DEBUG 00332 printf("LOOP\n"); 00333 #endif 00334 00335 } 00336 } else if(Menu.Get_Map() == Medium) { 00337 while(1) { 00338 00339 if(ghost_enable == true) { //If ghost mode is enabled 00340 #ifdef DEBUG 00341 printf("READING GHOST DATA\n"); 00342 #endif 00343 Ghost.SD_Read(Medium, car_model, SD);//Copy the ghost data from the SD card over to the flash memory (Make take a second) 00344 } 00345 00346 #ifdef DEBUG 00347 printf("COUNTDOWN\n"); 00348 #endif 00349 00350 //COUNTDOWN 00351 for(int count = 3; count >= 0; count--) { 00352 //Draw the map and the count down numbers 00353 LCD.clear(); 00354 Graphics.Draw_Map( 00355 translation, 00356 angle, 00357 squish, 00358 horizon_factor, 00359 (Line_2D *) Map_2_Track_Lines, 00360 (Line_2D *)Map_2_Track_Dotted_Lines, 00361 (Line_2D *)Map_2_Walls, 00362 (Sprite_2D *)Map_2_Sprites, 00363 (Triangle_2D *)Map_2_Boost_Plates, 00364 Map_2, 00365 car_model, 00366 Ghost.Play(false, race_time), 00367 LCD 00368 ); 00369 Graphics.Start_Sequence(count, LCD); 00370 LEDs.Start_Sequence(leds, count, Device); //Display the LED traffic light count down 00371 LCD.refresh(); 00372 wait(1); 00373 } 00374 LEDs.Clear(Device); 00375 00376 #ifdef DEBUG 00377 printf("RACE START\n"); 00378 #endif 00379 00380 while(laps <= number_of_laps) { 00381 //MECHANICS 00382 laps = Mechanics.Get_Laps(laps, Map_2_Gates, Map_2.number_of_gates, translation, lap_gate); //Get the current lap count 00383 lap_gate = Mechanics.Get_Gate(Map_2_Gates, Map_2.number_of_gates, translation, lap_gate); //Get the current lap_gate 00384 angle = Mechanics.Get_Angle(angle, handling, gyro_enabled, Gyro, Device); //Get the current angle 00385 translation = Mechanics.Get_Translation(translation, angle, speed, Map_2_Out_Of_Bounds_Square, Map_2_Out_Of_Bounds_Triangle, Map_2, Device); //Get the translation 00386 speed = Mechanics.Get_Speed( 00387 speed, 00388 max_speed, 00389 acceleration, 00390 deceleration, 00391 off_track_speed, 00392 translation, 00393 Map_2_Off_Track_Square, 00394 Map_2_Off_Track_Triangle, 00395 Map_2_Out_Of_Bounds_Square, 00396 Map_2_Out_Of_Bounds_Triangle, 00397 Map_2_Boost_Plates, 00398 Map_2, 00399 Device 00400 ); //Get the speed of the player 00401 //LEDS 00402 LEDs.Speed(leds, speed, max_speed, Device); //Change the LEDs depending on the speed 00403 00404 //GHOST 00405 if(ghost_enable == true) { //If ghost mode is enabled 00406 Ghost.Record(translation, race_time); //Record the position of the player 00407 } 00408 00409 //GRAPHICS 00410 LCD.clear(); //Clear the screen buffer 00411 Graphics.Change_Contrast(LCD,Device); //Adjust the contrast 00412 Graphics.Draw_Map( 00413 translation, 00414 angle, 00415 squish, 00416 horizon_factor, 00417 (Line_2D *) Map_2_Track_Lines, 00418 (Line_2D *)Map_2_Track_Dotted_Lines, 00419 (Line_2D *)Map_2_Walls, 00420 (Sprite_2D *)Map_2_Sprites, 00421 (Triangle_2D *)Map_2_Boost_Plates, 00422 Map_2, 00423 car_model, 00424 Ghost.Play(ghost_enable, race_time), 00425 LCD 00426 ); //Draw the graphics 00427 Graphics.Draw_Laps(laps, LCD); //Draw the lap counter 00428 Graphics.Draw_Time(false, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the time 00429 LCD.refresh(); //Refresh the display 00430 wait((1 / float(game_fps))); //Wait for a short period of time 00431 race_time++; //Increase the race time 00432 00433 } 00434 00435 00436 LEDs.Clear(Device); //Turn off the LEDs 00437 LCD.clear(); 00438 Graphics.Draw_Map( 00439 translation, 00440 angle, 00441 squish, 00442 horizon_factor, 00443 (Line_2D *) Map_2_Track_Lines, 00444 (Line_2D *)Map_2_Track_Dotted_Lines, 00445 (Line_2D *)Map_2_Walls, 00446 (Sprite_2D *)Map_2_Sprites, 00447 (Triangle_2D *)Map_2_Boost_Plates, 00448 Map_2, 00449 car_model, 00450 Ghost.Play(false, race_time), 00451 LCD 00452 ); //Draw the graphics 00453 Graphics.Finish(LCD); //Display the finish logo 00454 Graphics.Draw_Time(true, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the race time 00455 LCD.refresh(); //Refresh the display 00456 if(ghost_enable == true) { //If ghost mode enabled 00457 #ifdef DEBUG 00458 printf("RACE START\n"); 00459 #endif 00460 Ghost.Copy(race_time, Medium, car_model, SD); //Copy over the ghost data to the SD card (if race time is quicker) 00461 } 00462 00463 //RESET ALL VARIABLES 00464 angle = 0; 00465 Point_2D test = {0,0}; 00466 translation = test; 00467 squish = 0.15; 00468 speed = 0; 00469 max_speed = 4; 00470 handling = 2; 00471 lap_gate = 0; 00472 laps = 1; 00473 race_time = 0; 00474 00475 wait(3); 00476 00477 #ifdef DEBUG 00478 printf("LOOP\n"); 00479 #endif 00480 00481 } 00482 } else if(Menu.Get_Map() == Large) { 00483 while(1) { 00484 00485 if(ghost_enable == true) { //If ghost mode is enabled 00486 #ifdef DEBUG 00487 printf("READING GHOST DATA\n"); 00488 #endif 00489 Ghost.SD_Read(Large, car_model, SD); //Copy the ghost data from the SD card over to the flash memory (Make take a second) 00490 } 00491 00492 #ifdef DEBUG 00493 printf("COUNTDOWN\n"); 00494 #endif 00495 00496 //COUNTDOWN 00497 for(int count = 3; count >= 0; count--) { //Loop for the countdown 00498 //Draw the map and the count down numbers 00499 LCD.clear(); 00500 Graphics.Draw_Map( 00501 translation, 00502 angle, 00503 squish, 00504 horizon_factor, 00505 (Line_2D *) Map_3_Track_Lines, 00506 (Line_2D *)Map_3_Track_Dotted_Lines, 00507 (Line_2D *)Map_3_Walls, 00508 (Sprite_2D *)Map_2_Sprites, 00509 (Triangle_2D *)Map_3_Boost_Plates, 00510 Map_3, 00511 car_model, 00512 Ghost.Play(false, race_time), 00513 LCD 00514 ); 00515 Graphics.Start_Sequence(count, LCD); 00516 LCD.refresh(); 00517 LEDs.Start_Sequence(leds, count, Device); //Display the LED traffic light count down 00518 wait(1); 00519 } 00520 LEDs.Clear(Device); 00521 00522 #ifdef DEBUG 00523 printf("RACE START\n"); 00524 #endif 00525 00526 while(laps <= number_of_laps) { //Loop until the number of laps have been completed 00527 //MECHANICS 00528 laps = Mechanics.Get_Laps(laps, Map_3_Gates, Map_3.number_of_gates, translation, lap_gate); //Get the current lap count 00529 lap_gate = Mechanics.Get_Gate(Map_3_Gates, Map_3.number_of_gates, translation, lap_gate); //Get the current lap_gate 00530 angle = Mechanics.Get_Angle(angle, handling, gyro_enabled, Gyro, Device); //Get the current angle 00531 translation = Mechanics.Get_Translation(translation, angle, speed, Map_3_Out_Of_Bounds_Square, Map_3_Out_Of_Bounds_Triangle, Map_3, Device);//Get the translation 00532 speed = Mechanics.Get_Speed( 00533 speed, 00534 max_speed, 00535 acceleration, 00536 deceleration, 00537 off_track_speed, 00538 translation, 00539 Map_3_Off_Track_Square, 00540 Map_3_Off_Track_Triangle, 00541 Map_3_Out_Of_Bounds_Square, 00542 Map_3_Out_Of_Bounds_Triangle, 00543 Map_3_Boost_Plates, 00544 Map_3, 00545 Device 00546 );//Get the speed of the player 00547 00548 //LEDS 00549 LEDs.Speed(leds, speed, max_speed, Device); //Change the LEDs depending on the speed 00550 00551 //GHOST 00552 if(ghost_enable == true) { //If ghost mode is enabled 00553 Ghost.Record(translation, race_time); //Record the position of the player 00554 } 00555 00556 //GRAPHICS 00557 LCD.clear(); //Clear the screen 00558 Graphics.Change_Contrast(LCD,Device); //Adjust the contrast 00559 Graphics.Draw_Map( 00560 translation, 00561 angle, 00562 squish, 00563 horizon_factor, 00564 (Line_2D *) Map_3_Track_Lines, 00565 (Line_2D *)Map_3_Track_Dotted_Lines, 00566 (Line_2D *)Map_3_Walls, 00567 (Sprite_2D *)Map_3_Sprites, 00568 (Triangle_2D *)Map_3_Boost_Plates, 00569 Map_3, 00570 car_model, 00571 Ghost.Play(ghost_enable, race_time), 00572 LCD 00573 ); //Draw the graphics 00574 Graphics.Draw_Laps(laps, LCD); //Draw the lap counter 00575 Graphics.Draw_Time(false, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the time 00576 LCD.refresh(); //Refresh the display 00577 wait((1 / float(game_fps))); //Wait for a short period of time 00578 race_time++; //Increase the race time 00579 } 00580 00581 LEDs.Clear(Device); //Turn off the LEDs 00582 LCD.clear(); 00583 Graphics.Draw_Map( 00584 translation, 00585 angle, 00586 squish, 00587 horizon_factor, 00588 (Line_2D *) Map_3_Track_Lines, 00589 (Line_2D *)Map_3_Track_Dotted_Lines, 00590 (Line_2D *)Map_3_Walls, 00591 (Sprite_2D *)Map_3_Sprites, 00592 (Triangle_2D *)Map_3_Boost_Plates, 00593 Map_3, 00594 car_model, 00595 Ghost.Play(false, race_time), 00596 LCD 00597 ); //Draw the graphics 00598 Graphics.Finish(LCD); //Display the finish logo 00599 Graphics.Draw_Time(true, Mechanics.Convert_To_Time(game_fps, race_time), LCD); //Draw the race time 00600 LCD.refresh(); //Refresh the display 00601 if(ghost_enable == true) { //If ghost mode enabled 00602 #ifdef DEBUG 00603 printf("RACE START\n"); 00604 #endif 00605 Ghost.Copy(race_time, Large, car_model, SD); //Copy over the ghost data to the SD card (if race time is quicker) 00606 } 00607 00608 //RESET ALL VARIABLES 00609 angle = 0; 00610 Point_2D test = {0,0}; 00611 translation = test; 00612 squish = 0.15; 00613 speed = 0; 00614 max_speed = 4; 00615 handling = 2; 00616 lap_gate = 0; 00617 laps = 1; 00618 race_time = 0; 00619 wait(3); 00620 00621 #ifdef DEBUG 00622 printf("LOOP\n"); 00623 #endif 00624 00625 } 00626 } 00627 } 00628
Generated on Wed Jul 20 2022 17:42:24 by
1.7.2