Zeyu Feng 201377605

Dependencies:   mbed

On Minerva

Revision:
10:02ab3324be6c
Parent:
9:62d6559f0d50
Child:
11:494cc44777fe
--- a/main.cpp	Sat May 09 08:27:07 2020 +0000
+++ b/main.cpp	Sun May 10 05:16:51 2020 +0000
@@ -48,36 +48,32 @@
 void draw();
 void shot_update();
 void count_down();
+void reset_leds();
+void welcome();
+void init_timeout();
+void game_over();
 
 int main()
 {
     //initial
     init();
     //set a ticker
+    welcome();
     ticker.attach(&flip,2);
     timeout.attach(&time_out,5);
 
     //a infinite loop to control position of the people, update the game state
     while(1) {
-        if(timer_flag == 1) {
-            timer_flag = 0;
-            if(shot.get_size() < 30){
-                int size = shot.get_size()+ 4;
-                shot.set_size(size);
-            }
-        }
+        //   increase shots with a ticker
+        shot.gen_shot(timer_flag);    
         lcd.clear();
         //   shot update
         shot_update();
         
-        //   control people and check collision
-        if(timeout_flag){
-            control_check();
-        } 
+        //   lose control before timeout
         draw();
-        if(!timeout_flag){
-            count_down();
-        }
+        reset_leds();
+        
         lcd.refresh();
         //printf("shot refresh\n");
         //printf("size = %d\n",shot._size);
@@ -102,24 +98,37 @@
     engine.read_input(pad);
     engine.update();
     collision.set_pos(engine.get_pos());
+    // if people is shotted, health -1 and init the game
     if(collision.check(lcd)) {
-        engine.init();
-        lcd.clear();
-        shot.init();
+        if(!collision.get_health()){
+            game_over();
+        } else{
+            engine.init();
+            lcd.clear();
+            shot.init();
+            init_timeout();
+        }
     }
 }
 
 void draw()
 {
+    if(timeout_flag)
+        control_check();
+    //control people and check collision
     engine.draw(lcd);
     collision.draw(lcd);
+    if(!timeout_flag)
+        count_down();
 }
 
 void shot_update()
 {
     shot.update();
+    // delete invalid shots
     shot.delete_shot();
-    shot.gen_shot();
+    //generate shot to keep constant number of shots
+    shot.init_shot();
     shot.draw(lcd);
 }
 
@@ -130,4 +139,36 @@
     lcd.printString(buffer,40,2);
     printf("count: %d\n",count_flag);
     count_flag--;
-}
\ No newline at end of file
+}
+void reset_leds()
+{
+    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);
+    }
+}
+
+void init_timeout(){
+    timeout.attach(&time_out,5);
+    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