test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
kaku_jyoko
Date:
Mon Dec 12 11:50:02 2016 +0000
Parent:
32:abb3086c210d
Parent:
31:17b4ee41bdc5
Child:
34:a83cad29890c
Commit message:
change

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp.orig Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Dec 12 11:48:19 2016 +0000
+++ b/main.cpp	Mon Dec 12 11:50:02 2016 +0000
@@ -76,16 +76,23 @@
     }
 }
 
+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 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);
-    if((p_person.x + 2 >= p_bullet.x) && (p_person.x + 2 <= p_bullet.x + 5)){
-        printf("aaaaa\n");
-        if((p_person.y + 2 >= p_bullet.y + 1) && (p_person.y + 2 <= p_bullet.y + 4)){
-            printf("gameover\n");
-        }   
-    }
-    return 0;
+
+    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));
 }
 
 int main(){
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp.orig	Mon Dec 12 11:50:02 2016 +0000
@@ -0,0 +1,130 @@
+#include "mbed.h"
+#include "C12832_lcd.h"
+#include "Arial_9.h"
+#include "Small_7.h"
+#include "graphics.h"
+#include "models.h"
+#include "rtos.h"
+#include "point.h"
+#include "stdlib.h"
+#include "convert.h"
+
+C12832_LCD lcd;
+
+// down: p12, left: p13, center: p14, up: p15, right: p16
+InterruptIn button(p14);
+AnalogIn aIn(p20);
+DigitalOut led1(LED1);
+
+Mutex jump_mtx;
+Mutex update_mtx;
+int jump_flag = 0;
+Person h;
+Stage stage;
+Serial pc(USBTX, USBRX); // tx, rx
+Bullet* b = NULL;
+Timer t;
+int white_board[LCD_Y][LCD_X];
+int* wall_height;
+
+void reset_white_board()
+{
+    for(int i = 0; i < LCD_Y; i++){
+        for (int j = 0; j < LCD_X; j++){
+            white_board[i][j] = 0;
+        }
+    }
+}
+
+void make_wall(){
+    wall_height = stage.getStage();
+    for(int i = 0; i < LCD_Y; i++){
+        for(int j = 0; j < LCD_X ; j++){
+            if(wall_height[i] > (LCD_Y - 1 - j)){
+                white_board[i][j] = white_board[i][j] ^ 1;
+            }
+        }
+    }
+}
+
+void a(){
+    h.jump();
+}
+
+void jump_receive(void const *argument){
+    while(true){
+        jump_mtx.lock();
+        button.rise(&a);
+        jump_mtx.unlock();
+        //Thread::wait(0.1);
+    }
+}
+
+void bullet_receive(void const *argument){
+    point start;
+    start.x = 127;
+    start.y = 27;
+    char c;
+    while(true){
+        t.start();
+        c = pc.getc();
+        if(c == 'a' && t.read() > 5.0){
+            b = NULL;
+            b = &Bullet(start, 2);
+            t.reset();
+        }
+    }
+}
+
+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);
+    if((p_person.x + 2 >= p_bullet.x) && (p_person.x + 2 <= p_bullet.x + 5)){
+        printf("aaaaa\n");
+        if((p_person.y + 2 >= p_bullet.y + 1) && (p_person.y + 2 <= p_bullet.y + 4)){
+            printf("gameover\n");
+        }   
+    }
+    return 0;
+}
+
+int main(){
+    point p_person, p_bullet;
+    printf("hello\n");
+    Thread jump_th(jump_receive);
+    Thread bullet_th(bullet_receive);
+    lcd.setmode(XOR);
+    int gameOver = 0;
+    srand((int)(aIn * 100));
+    Converter converter();
+//    Bitmap picture;
+    
+    while(true){
+        update_mtx.lock();
+        reset_white_board();
+        make_wall();
+        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);
+        corrision_judge(p_person, p_bullet);
+        Bitmap picture = converter.convert(white_board);
+        update_mtx.unlock();
+        lcd.cls();
+        lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
+        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);
+        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
--- a/models.h	Mon Dec 12 11:48:19 2016 +0000
+++ b/models.h	Mon Dec 12 11:50:02 2016 +0000
@@ -23,6 +23,7 @@
 #define GAME_OVER -1
 
 //Bullet params
+#define BULLET_SIZE 5 //bullet is 5*5
 #define HIGH_SPEED 5
 #define NOMAL_SOEED 3
 #define LOW_SPEED 1