Zeyu Feng 201377605

Dependencies:   mbed

On Minerva

Revision:
13:eb60628db8bf
Parent:
12:009895f6b6e4
Child:
14:42b8a91e463c
--- a/main.cpp	Mon May 11 14:43:16 2020 +0000
+++ b/main.cpp	Thu May 14 16:45:20 2020 +0000
@@ -10,7 +10,6 @@
 Date:11/3/2020
 */
 
-// includes
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
@@ -19,7 +18,7 @@
 #include "shot.h"
 #include "Collision.h"
 #include "Interface.h"
-#include "tests.h"
+# include "tests.h"
 
 // objects
 Gamepad pad;
@@ -38,26 +37,27 @@
 volatile int paused_flag = 0;
 volatile int option_flag = 0;
 
-//    functions
+//    prototypes
 void flip(){    timer_flag = 1; }
 void time_out(){    timeout_flag = 1;   }
 void init();
+void init_value();
 void control_check();
 void update();
-void shot_update();
 void init_timeout();
 void main_game(float,int);
-void init_value();
 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();
@@ -65,10 +65,10 @@
         int option_flag= interface.menu(lcd,pad,option_flag);
         switch(option_flag){
             case 0:
-                main_game(0.7,25);
+                main_game(0.05,20);
                 break;
             case 1:
-                main_game(4,40);
+                main_game(1,40);
                 simp_game();
                 break;
             case 2:
@@ -84,11 +84,12 @@
     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();
+        //   increase shots with a ticker
+        shot.gen_shot(timer_flag,increment,max);    
         //   shot update
-        shot_update();
+        shot.update(lcd);
         
         //   set a count down and update lcd
         update();
@@ -107,75 +108,12 @@
 void init()
 {
     lcd.init();
-    lcd.setContrast(0.5);
     engine.init();
     pad.init();
     shot.init();
     collision.init();
     lcd.refresh();
-}
-
-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)) {
-        if(!collision.get_health()){
-            interface.game_over(lcd,pad);
-            lcd.clear();
-        } else{
-            init_timeout();
-            lcd.clear();
-        }
-    }
-}
-
-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_flag = interface.count_down(lcd,count_flag);
-}
-
-void shot_update()
-{
-    shot.update();
-    // delete invalid shots
-    shot.delete_shot();
-    //generate shot to keep constant number of shots
-    shot.init_shot();
-    shot.draw(lcd);
-}
-
-
-void init_timeout()
-{   
-    lcd.clear();
-    lcd.drawRect(1,1,(collision.get_health()+1)*2,3,FILL_BLACK);
-    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
-    lcd.refresh();
-    wait(0.5);
-    lcd.clear();
-    lcd.drawRect(1,1,collision.get_health()*2,3,FILL_BLACK);
-    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
-    lcd.refresh();
-    wait(0.5);
-    engine.init();
-    shot.init();
-    //initialise time out
-    timeout.attach(&time_out,3);
-    timeout_flag = 0;
-    count_flag = 18;
+    interface.init();
 }
 
 void init_value()
@@ -187,13 +125,65 @@
     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)) {
+        if(!collision.get_health()){
+            interface.game_over(lcd,pad);
+        } else{
+            init_timeout();
+        }
+        lcd.clear();
+    }
+}
+
+void update()
+{
+    if(timeout_flag){
+        control_check();
+    }else{
+        //if timeout_flag count down number and Joystick have been banned 
+        count_flag = interface.count_down(lcd,count_flag);
+    }
+    //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
+    timeout.attach(&time_out,3);
+    timeout_flag = 0;
+    count_flag = 18;
+}
+
 void simp_game()
 {
     if(!interface.get_victory_flag()){
+        
         interface.simple_game(lcd,pad);
         if(interface.get_sim_flag()){
             init();
-            main_game(2.5,30);
+            main_game(0.5,30);
         }
     }
     interface.init();