test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Revision:
40:ec5c1b305b9a
Parent:
39:e8d6dd3c75c7
Child:
45:7d0a58fbaa8b
--- a/main.cpp	Tue Dec 13 11:55:17 2016 +0000
+++ b/main.cpp	Tue Dec 13 13:32:44 2016 +0000
@@ -45,16 +45,6 @@
             }
         }
     }
-    //print wall int
-//    for(int i = 0; i < LCD_X; i++){
-//        printf("line %d  height: %d  ",i, wall_height[i]);
-//        for(int j = 0; j < LCD_Y; j++){
-//            printf("%d ",white_board[j][i]);
-//        }
-//        printf("\n");
-//    }
-//    printf("\n");
-
 }
 
 void a(){
@@ -86,23 +76,29 @@
     }
 }
 
-int line_judge(int a, int b, int c, int d){
-    //if corrision, return 1
-    if(b < c){
-        return 0;
-    }else if(d < a){
-        return 0;
-    }else{
-        return 1;
-    }
+int xabs(double x,double y){
+  if(x>=y){
+    return(x-y);
+  }
+  else if(y>x){
+    return(y-x);
+  }
 }
 
-int corrision_judge(point p_person, point p_bullet){
-    //printf("bullet  x: %d,  y: %d \n", p_bullet.x,p_bullet.y);
-//    printf("person  x: %d,  y: %d \n", p_person.x,p_person.y);
-
-    return (line_judge(p_person.x, p_person.x + PERSON_SIZE, p_bullet.x, p_bullet.x + BULLET_SIZE)
-    && line_judge(p_person.y, p_person.y + PERSON_SIZE, p_bullet.y - 1, p_bullet.x - 4));
+bool bullet_collision(point p_person, point p_bullet){
+    double person_center_x = p_person.x + PERSON_SIZE / 2;
+    double person_center_y = p_person.y + PERSON_SIZE / 2;
+    
+    double bullet_center_x = p_bullet.x + BULLET_SIZE / 2;
+    double bullet_center_y = p_bullet.y + BULLET_SIZE / 2;
+    
+    if(xabs(person_center_x, bullet_center_x) < (PERSON_SIZE / 2 + BULLET_SIZE / 2)){
+        if(xabs(person_center_y, bullet_center_y) < (PERSON_SIZE / 2 + (BULLET_SIZE - 2) / 2)){
+            return true;
+        }
+    }
+    
+    return false;
 }
 
 int main(){
@@ -111,39 +107,55 @@
     Thread jump_th(jump_receive);
     Thread bullet_th(bullet_receive);
     lcd.setmode(XOR);
-    int gameOver = 0;
+    bool gameover = false;
+    bool isDisplay = false;
     srand((int)(aIn * 100));
     Converter converter;
     Bitmap picture;
     
     while(true){
-        update_mtx.lock();
-        reset_white_board();
-        make_wall();
-        p_person = h.update(wall_height[3]);
-        if(b != NULL){
-            p_bullet = b->update();
+        if(!gameover){
+            update_mtx.lock();
+            reset_white_board();
+            make_wall();
+            p_person = h.update(wall_height[3]);
+           // p_person = h.update(1);
+            if(b != NULL){
+                p_bullet = b->update();
+            }
+            //printf("bullet  x: %d,  y: %d \n", p_bullet.x,p_bullet.y);
+            //printf("person  x: %d,  y: %d \n", p_person.x,p_person.y);
+            gameover = bullet_collision(p_person, p_bullet);
+            picture = converter.convert(white_board);
+            update_mtx.unlock();
+            
+            lcd.cls();
+            lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
+            lcd.print_bm(picture,0,0);
+            if(b != NULL){
+                lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
+            }
+            lcd.copy_to_lcd();
+            wait(0.02);
+            lcd.cls();
+            lcd.print_bm(bitmPlayerB,p_person.x,p_person.y);
+            lcd.print_bm(picture,0,0);
+            if(b != NULL){
+                lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
+            }
+            lcd.copy_to_lcd();
+            wait(0.02);
+        } else {
+            if(!isDisplay){
+                lcd.cls();
+                lcd.locate(5,10);
+                lcd.printf("Game Over.");
+                lcd.locate(5,22);
+                lcd.printf("Your record is %d.", stage.getLength());
+                lcd.copy_to_lcd();
+                isDisplay = true;
+            }
+            
         }
-        //printf("bullet  x: %d,  y: %d \n", p_bullet.x,p_bullet.y);
-        //printf("person  x: %d,  y: %d \n", p_person.x,p_person.y);
-        corrision_judge(p_person, p_bullet);
-        picture = converter.convert(white_board);
-        update_mtx.unlock();
-        lcd.cls();
-        lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
-        lcd.print_bm(picture,0,0);
-        if(b != NULL){
-            lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
-        }
-        lcd.copy_to_lcd();
-        wait(0.02);
-        lcd.cls();
-        lcd.print_bm(bitmPlayerB,p_person.x,p_person.y);
-        lcd.print_bm(picture,0,0);
-        if(b != NULL){
-            lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
-        }
-        lcd.copy_to_lcd();
-        wait(0.02);
     }
 }
\ No newline at end of file