
Zeyu Feng 201377605
Dependencies: mbed
On Minerva
main.cpp
- Committer:
- el19zf
- Date:
- 2020-05-18
- Revision:
- 16:cf2bfada3adf
- Parent:
- 15:3571beaaeed8
- Child:
- 17:ba4d9cd1e347
File content as of revision 16:cf2bfada3adf:
/* ELEC2645 Embedded Systems Project School of Electronic & Electrical Engineering University of Leeds 2019/20 Name:Zeyu Feng Username:el19zf Student ID Number:201377605 Date:11/3/2020 */ #include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "People.h" #include "PeopleEngine.h" #include "shot.h" #include "Collision.h" #include "Interface.h" #include "tests.h" // objects Gamepad pad; N5110 lcd; PeopleEngine engine; shot shot; Collision collision; Interface interface; Sound sound; //flag and triggers Ticker ticker;//gradually increase number of shots Timeout timeout; volatile int timer_flag = 0; volatile int timeout_flag = 0; volatile int count_flag = 18; volatile int paused_flag = 0; volatile int option_flag = 0; // prototypes void flip(){ timer_flag = 1; } void time_out(){ timeout_flag = 1; } void init(); void init_value(); void control_check(); void update(float, int); void init_timeout(); void main_game(float,int); void simp_game(); int main() { #ifdef WITH_TESTING int failures = run_all_tests(); #endif //initial init(); interface.Welcome(lcd,pad); //a infinite loop while(1) { init(); init_value(); int option_flag= interface.menu(lcd,pad,option_flag); switch(option_flag){ case 0: main_game(0.1,13); break; case 1: main_game(0.8,30); simp_game(); break; case 2: interface.exit(lcd,pad); break; } } } void main_game(float increment,int max){ ticker.attach(&flip,2);//set a ticker timeout.attach(&time_out,3);//set a timeout while((collision.get_health() > 0)&&(!collision.get_des())) { lcd.clear(); // increase shots with a ticker shot.gen_shot(timer_flag,increment,max); // shot update shot.draw(lcd); // set a count down and update lcd update(increment, max); pad.leds_on(); lcd.refresh(); //printf("shot refresh\n"); //printf("size = %d\n",shot._size); wait_ms(1000/6);//fps = 6 } //printf("END GAME\n"); } void init() { lcd.init(); engine.init(); pad.init(); shot.init(); collision.init(); lcd.refresh(); interface.init(); } void init_value() { timer_flag = 0; timeout_flag = 0; count_flag = 18; paused_flag = 0; option_flag = 0; } void control_check() { engine.read_input(pad); engine.update(); collision.set_pos(engine.get_pos()); // if people is shotted, health -1 and reset the game if(collision.check(lcd)) { pad.play_melody(2,sound_data_col,sound_dur_col,120,0); if(!collision.get_health()){ interface.game_over(lcd,pad); } else{ init_timeout(); } lcd.clear(); } } void update(float increment, int max) { if(timeout_flag){ control_check(); paused_flag = interface.check_pause(lcd,pad,paused_flag,increment,max); }else{ //if timeout_flag count down number and Joystick have been banned count_flag = interface.count_down(lcd,count_flag); pad.reset_buttons(); } //control people and check collision if(!collision.get_check_col()){ engine.draw(lcd,timeout_flag); collision.draw(lcd); }else{ collision.set_check_col(); } //printf("timeout_flag = %d\n",timeout_flag); //check destination if(collision.check_des(lcd)){ interface.victory(lcd,pad); lcd.clear(); } } //reset timeout after collision (health > 0) void init_timeout() { collision.draw_collision(lcd); engine.init(); shot.init(); //initialise time out,flag and bottons timeout.attach(&time_out,3); timeout_flag = 0; count_flag = 18; //count down, each duration 0.5s and 60 beats per minute(1s) sound.count_sound(pad); pad.reset_buttons(); } void simp_game() { if(!interface.get_victory_flag()){ interface.simple_game(lcd,pad); if(interface.get_sim_flag()){ init(); init_value(); main_game(0.4,20); } } interface.init(); }