ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ELEC2645 Embedded Systems Project
00003 School of Electronic & Electrical Engineering
00004 University of Leeds
00005 2019/20
00006 
00007 Name:Zeyu Feng
00008 Username:el19zf
00009 Student ID Number:201377605
00010 Date:11/3/2020
00011 */
00012 
00013 #include "mbed.h"
00014 #include "Gamepad.h"
00015 #include "N5110.h"
00016 #include "People.h"
00017 #include "PeopleEngine.h"
00018 #include "shot.h"
00019 #include "Collision.h"
00020 #include "Interface.h"
00021 #include "Instruction.h"
00022 #include "tests.h"
00023 
00024 // objects
00025 Gamepad pad;
00026 N5110 lcd;
00027 PeopleEngine engine;
00028 shot shot;
00029 Collision collision;
00030 Interface interface;
00031 Sound sound;
00032 Instruction instruction;
00033 
00034 //flag and triggers
00035 Ticker ticker;//gradually increase number of shots
00036 Timeout timeout;
00037 volatile int timer_flag = 0;
00038 volatile int timeout_flag = 0;
00039 volatile int paused_flag = 0;
00040 
00041 //    prototypes
00042 void flip(){    timer_flag = 1; }
00043 void time_out(){    timeout_flag = 1;   }
00044 void init();
00045 void init_value();
00046 void control_check();
00047 void update(float, int);
00048 void init_timeout();
00049 void main_game(float,int);
00050 void simp_game();
00051 void undefined();
00052 
00053 int main()
00054 {
00055 #ifdef WITH_TESTING
00056     int failures = run_all_tests();
00057 #endif
00058     //initial
00059     init();
00060     
00061     interface.Welcome(lcd,pad);
00062     
00063     //a infinite loop 
00064     while(1) {
00065         init();
00066         init_value();
00067         //select option by Joystick
00068         interface.menu(lcd,pad);
00069         switch(interface.get_menu_flag()){
00070             case 0:
00071                 main_game(0.15,13);
00072                 break;
00073             case 1:
00074                 main_game(1,35);
00075                 simp_game();
00076                 break;
00077             case 2:
00078             //define game parmeter by yourself
00079                 undefined();
00080                 break;
00081             case 3:
00082                 instruction.draw(lcd,pad);
00083                 break;
00084             case 4:
00085                 interface.record(lcd,pad);
00086                 break;
00087         }
00088         #ifdef DEBUG
00089             printf("END LOOP\n");
00090         #endif
00091     }
00092 }
00093 
00094 void main_game(float increment,int max){
00095     
00096     ticker.attach(&flip,2);//set a ticker
00097     timeout.attach(&time_out,3);//set a timeout
00098     
00099     while((collision.get_health() > 0)&&(!collision.get_des()))
00100     {   
00101         lcd.clear();
00102         //   increase shots with a ticker
00103         shot.gen_shot(timer_flag,increment,max);    
00104         //   shot update
00105         shot.draw(lcd);
00106         
00107         //   set a count down || generate and move the shots (shots won't move until count down to 0)
00108         //   check any collisions or victory
00109         update(increment, max);
00110         
00111         pad.leds_on();
00112         
00113         lcd.refresh();
00114         //printf("shot refresh\n");
00115         //printf("size = %d\n",shot._size);
00116         wait_ms(1000/6);//fps = 6
00117     }
00118     #ifdef DEBUG
00119         printf("END GAME\n");
00120     #endif
00121 }
00122 
00123 void init()
00124 {
00125     lcd.init();
00126     engine.init();
00127     pad.init();
00128     shot.init();
00129     collision.init();
00130     lcd.refresh();
00131     interface.init();
00132 }
00133 
00134 void init_value()
00135 {
00136     timer_flag = 0;
00137     timeout_flag = 0;
00138     paused_flag = 0;
00139 }   
00140 
00141 void control_check()
00142 {
00143     //control the people by Joystick
00144     engine.read_input(pad);
00145     engine.update();
00146     collision.set_pos(engine.get_pos());
00147     // if people is shotted, health -1 and reset the game
00148     if(collision.check(lcd)) {
00149         pad.play_melody(2,sound_data_col,sound_dur_col,120,0);
00150         if(!collision.get_health()){
00151             interface.game_over(lcd,pad);
00152         } else{
00153             init_timeout();
00154         }
00155         lcd.clear();
00156     }
00157 }
00158 
00159 void update(float increment, int max)
00160 {
00161     //if timeout_flag count down number and Joystick have been banned 
00162     if(timeout_flag){
00163         control_check();
00164         paused_flag = interface.check_pause(lcd,pad,paused_flag,increment,max);
00165     }else{
00166         interface.count_down(lcd);
00167         pad.reset_buttons();
00168     }
00169     //control people and check collision
00170     if(!collision.get_check_col()){
00171         engine.draw(lcd,timeout_flag);
00172         collision.draw(lcd);
00173     }else{
00174         collision.reset_check_col();
00175     }
00176     //check destination
00177     if(collision.check_des(lcd)){
00178         interface.victory(lcd,pad);
00179         interface.update_record(increment,max);
00180         lcd.clear();
00181     }
00182 }
00183 
00184 //reset timeout after collision (health > 0)
00185 void init_timeout()
00186 {   
00187     collision.draw_collision(lcd);
00188     engine.init();
00189     shot.init();
00190     //initialise timeout,flag and bottons
00191     timeout.attach(&time_out,3);
00192     timeout_flag = 0;
00193     interface.set_count_down(18);
00194     //count down, each duration 0.5s and 60 beats per minute(1s)
00195     sound.count_sound(pad);
00196     pad.reset_buttons();
00197     #ifdef DEBUG
00198         printf("initalises the timeout trigger\n");
00199     #endif
00200 }
00201 
00202 void simp_game()
00203 {
00204     if(!interface.get_victory_flag()){
00205         
00206         interface.simple_game(lcd,pad);
00207         if(interface.get_sim_flag()){
00208             init();
00209             init_value();
00210             main_game(0.3,20);
00211         }
00212     }
00213     interface.init();
00214 }
00215 
00216 void undefined()
00217 {
00218     //define shots rate and maximum
00219     interface.undefined_rate(lcd,pad);
00220     interface.undefined_max(lcd,pad);
00221     //   0   1   2   3   4  flag
00222     //  0.3 0.4 0.5 0.6 0.7 shots/frame
00223     //   15  20  25  30  35  Maximum shots
00224     main_game(0.1*(interface.get_rate_flag() + 3),5*(interface.get_max_flag()+ 3));
00225 }
00226