
Zeyu Feng 201377605
Dependencies: mbed
On Minerva
Diff: main.cpp
- Revision:
- 11:494cc44777fe
- Parent:
- 10:02ab3324be6c
- Child:
- 12:009895f6b6e4
--- a/main.cpp Sun May 10 05:16:51 2020 +0000 +++ b/main.cpp Mon May 11 09:09:31 2020 +0000 @@ -18,7 +18,7 @@ #include "PeopleEngine.h" #include "shot.h" #include "Collision.h" - +#include "Interface.h" // objects Gamepad pad; @@ -26,62 +26,80 @@ PeopleEngine engine; shot shot; Collision collision; +Interface interface; //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 = 30; +volatile int count_flag = 18; +volatile int paused_flag = 0; +volatile int option_flag = 0; // functions -void flip() -{ - timer_flag = 1; -} -void time_out() -{ - timeout_flag = 1; -} +void flip(){ timer_flag = 1; } +void time_out(){ timeout_flag = 1; } void init(); void control_check(); -void draw(); +void update(); void shot_update(); -void count_down(); -void reset_leds(); -void welcome(); void init_timeout(); -void game_over(); +void main_game(float,int); +void init_value(); int main() { //initial init(); - //set a ticker - welcome(); - ticker.attach(&flip,2); - timeout.attach(&time_out,5); + + interface.Welcome(lcd,pad); - //a infinite loop to control position of the people, update the game state + //a infinite loop while(1) { - // increase shots with a ticker - shot.gen_shot(timer_flag); + init(); + init_value(); + int option_flag= interface.menu(lcd,pad,option_flag); + switch(option_flag){ + case 0: + main_game(0.7,25); + break; + case 1: + main_game(4,40); + 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())) + { // increase shots with a ticker + shot.gen_shot(timer_flag,increment,max); lcd.clear(); // shot update shot_update(); - // lose control before timeout - draw(); - reset_leds(); + // set a count down and update lcd + update(); + + pad.leds_on(); + paused_flag = interface.check_pause(lcd,pad,paused_flag); 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(); @@ -98,10 +116,11 @@ engine.read_input(pad); engine.update(); collision.set_pos(engine.get_pos()); - // if people is shotted, health -1 and init the game + // if people is shotted, health -1 and reset the game if(collision.check(lcd)) { if(!collision.get_health()){ - game_over(); + interface.game_over(lcd,pad); + lcd.clear(); } else{ engine.init(); lcd.clear(); @@ -111,15 +130,19 @@ } } -void draw() +void update() { if(timeout_flag) control_check(); //control people and check collision engine.draw(lcd); collision.draw(lcd); + if(collision.check_des(lcd)){ + interface.victory(lcd,pad); + lcd.clear(); + } if(!timeout_flag) - count_down(); + count_flag = interface.count_down(lcd,count_flag); } void shot_update() @@ -132,43 +155,19 @@ shot.draw(lcd); } -void count_down() -{ - char buffer[6]; - sprintf(buffer,"%d",(int)count_flag/6); - lcd.printString(buffer,40,2); - printf("count: %d\n",count_flag); - count_flag--; -} -void reset_leds() + +void init_timeout() { - pad.leds_on(); -} -void welcome(){ - lcd.printString(" Welcome! ",0,1); - lcd.printString(" Game by ",0,2); - lcd.printString(" Z. FENG ",0,3); - lcd.refresh(); - while (pad.A_pressed() ==false) { - pad.leds_on(); - wait(0.1); - pad.leds_off(); - wait(0.1); - } + timeout.attach(&time_out,3); + timeout_flag = 0; + count_flag = 18; } -void init_timeout(){ - timeout.attach(&time_out,5); +void init_value() +{ + timer_flag = 0; timeout_flag = 0; - count_flag = 30; -} - -void game_over(){ - while(pad.B_pressed()==false){ - lcd.clear(); - lcd.printString(" Game over ",0,2); - lcd.refresh(); - wait(0.1); - } -} - \ No newline at end of file + count_flag = 18; + paused_flag = 0; + option_flag = 0; +} \ No newline at end of file