test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Committer:
kaku_jyoko
Date:
Mon Dec 12 11:50:02 2016 +0000
Revision:
33:442498e281b1
Parent:
32:abb3086c210d
Parent:
31:17b4ee41bdc5
Child:
36:b88fa8ff0be9
Child:
37:f5d98f22b6bd
change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
th_mbed 0:26e5e770f91b 1 #include "mbed.h"
th_mbed 0:26e5e770f91b 2 #include "C12832_lcd.h"
th_mbed 0:26e5e770f91b 3 #include "Arial_9.h"
th_mbed 0:26e5e770f91b 4 #include "Small_7.h"
th_mbed 0:26e5e770f91b 5 #include "graphics.h"
kaku_jyoko 4:0fe59e58def9 6 #include "models.h"
kaku_jyoko 4:0fe59e58def9 7 #include "rtos.h"
kaku_jyoko 5:651879f4f047 8 #include "point.h"
kaku_jyoko 29:7784b098ef1d 9 #include "stdlib.h"
kaku_jyoko 32:abb3086c210d 10 #include "convert.h"
th_mbed 0:26e5e770f91b 11
th_mbed 0:26e5e770f91b 12 C12832_LCD lcd;
th_mbed 0:26e5e770f91b 13
kaku_jyoko 4:0fe59e58def9 14 // down: p12, left: p13, center: p14, up: p15, right: p16
kaku_jyoko 5:651879f4f047 15 InterruptIn button(p14);
kaku_jyoko 29:7784b098ef1d 16 AnalogIn aIn(p20);
kaku_jyoko 5:651879f4f047 17 DigitalOut led1(LED1);
kaku_jyoko 4:0fe59e58def9 18
kaku_jyoko 5:651879f4f047 19 Mutex jump_mtx;
kaku_jyoko 5:651879f4f047 20 Mutex update_mtx;
kaku_jyoko 4:0fe59e58def9 21 int jump_flag = 0;
kaku_jyoko 6:e63641e13374 22 Person h;
kaku_jyoko 29:7784b098ef1d 23 Stage stage;
kaku_jyoko 18:0f6f2aa2339d 24 Serial pc(USBTX, USBRX); // tx, rx
kaku_jyoko 18:0f6f2aa2339d 25 Bullet* b = NULL;
kaku_jyoko 19:55f3114f3996 26 Timer t;
kaku_jyoko 29:7784b098ef1d 27 int white_board[LCD_Y][LCD_X];
kaku_jyoko 29:7784b098ef1d 28 int* wall_height;
kaku_jyoko 18:0f6f2aa2339d 29
kaku_jyoko 29:7784b098ef1d 30 void reset_white_board()
kaku_jyoko 29:7784b098ef1d 31 {
kaku_jyoko 29:7784b098ef1d 32 for(int i = 0; i < LCD_Y; i++){
kaku_jyoko 29:7784b098ef1d 33 for (int j = 0; j < LCD_X; j++){
kaku_jyoko 29:7784b098ef1d 34 white_board[i][j] = 0;
kaku_jyoko 29:7784b098ef1d 35 }
kaku_jyoko 29:7784b098ef1d 36 }
kaku_jyoko 29:7784b098ef1d 37 }
kaku_jyoko 29:7784b098ef1d 38
kaku_jyoko 29:7784b098ef1d 39 void make_wall(){
kaku_jyoko 29:7784b098ef1d 40 wall_height = stage.getStage();
kaku_jyoko 29:7784b098ef1d 41 for(int i = 0; i < LCD_Y; i++){
kaku_jyoko 29:7784b098ef1d 42 for(int j = 0; j < LCD_X ; j++){
kaku_jyoko 29:7784b098ef1d 43 if(wall_height[i] > (LCD_Y - 1 - j)){
kaku_jyoko 29:7784b098ef1d 44 white_board[i][j] = white_board[i][j] ^ 1;
kaku_jyoko 29:7784b098ef1d 45 }
kaku_jyoko 29:7784b098ef1d 46 }
kaku_jyoko 29:7784b098ef1d 47 }
kaku_jyoko 29:7784b098ef1d 48 }
kaku_jyoko 4:0fe59e58def9 49
kaku_jyoko 5:651879f4f047 50 void a(){
kaku_jyoko 6:e63641e13374 51 h.jump();
kaku_jyoko 4:0fe59e58def9 52 }
kaku_jyoko 4:0fe59e58def9 53
kaku_jyoko 18:0f6f2aa2339d 54 void jump_receive(void const *argument){
kaku_jyoko 4:0fe59e58def9 55 while(true){
kaku_jyoko 6:e63641e13374 56 jump_mtx.lock();
kaku_jyoko 6:e63641e13374 57 button.rise(&a);
kaku_jyoko 6:e63641e13374 58 jump_mtx.unlock();
kaku_jyoko 6:e63641e13374 59 //Thread::wait(0.1);
th_mbed 0:26e5e770f91b 60 }
kaku_jyoko 5:651879f4f047 61 }
kaku_jyoko 5:651879f4f047 62
kaku_jyoko 18:0f6f2aa2339d 63 void bullet_receive(void const *argument){
kaku_jyoko 18:0f6f2aa2339d 64 point start;
kaku_jyoko 18:0f6f2aa2339d 65 start.x = 127;
kaku_jyoko 18:0f6f2aa2339d 66 start.y = 27;
kaku_jyoko 18:0f6f2aa2339d 67 char c;
kaku_jyoko 18:0f6f2aa2339d 68 while(true){
kaku_jyoko 19:55f3114f3996 69 t.start();
kaku_jyoko 18:0f6f2aa2339d 70 c = pc.getc();
kaku_jyoko 19:55f3114f3996 71 if(c == 'a' && t.read() > 5.0){
kaku_jyoko 19:55f3114f3996 72 b = NULL;
kaku_jyoko 18:0f6f2aa2339d 73 b = &Bullet(start, 2);
kaku_jyoko 19:55f3114f3996 74 t.reset();
kaku_jyoko 18:0f6f2aa2339d 75 }
kaku_jyoko 18:0f6f2aa2339d 76 }
kaku_jyoko 18:0f6f2aa2339d 77 }
kaku_jyoko 18:0f6f2aa2339d 78
th_mbed 31:17b4ee41bdc5 79 int line_judge(int a, int b, int c, int d){
th_mbed 31:17b4ee41bdc5 80 //if corrision, return 1
th_mbed 31:17b4ee41bdc5 81 if(b < c){
th_mbed 31:17b4ee41bdc5 82 return 0;
th_mbed 31:17b4ee41bdc5 83 }else if(d < a){
th_mbed 31:17b4ee41bdc5 84 return 0;
th_mbed 31:17b4ee41bdc5 85 }else{
th_mbed 31:17b4ee41bdc5 86 return 1;
th_mbed 31:17b4ee41bdc5 87 }
th_mbed 31:17b4ee41bdc5 88 }
th_mbed 31:17b4ee41bdc5 89
kaku_jyoko 29:7784b098ef1d 90 int corrision_judge(point p_person, point p_bullet){
kaku_jyoko 29:7784b098ef1d 91 printf("bullet x: %d, y: %d \n", p_bullet.x,p_bullet.y);
kaku_jyoko 29:7784b098ef1d 92 printf("person x: %d, y: %d \n", p_person.x,p_person.y);
kaku_jyoko 33:442498e281b1 93
th_mbed 31:17b4ee41bdc5 94 return (line_judge(p_person.x, p_person.x + PERSON_SIZE, p_bullet.x, p_bullet.x + BULLET_SIZE)
th_mbed 31:17b4ee41bdc5 95 && line_judge(p_person.y, p_person.y + PERSON_SIZE, p_bullet.y - 1, p_bullet.x - 4));
kaku_jyoko 29:7784b098ef1d 96 }
kaku_jyoko 29:7784b098ef1d 97
kaku_jyoko 5:651879f4f047 98 int main(){
kaku_jyoko 18:0f6f2aa2339d 99 point p_person, p_bullet;
kaku_jyoko 5:651879f4f047 100 printf("hello\n");
kaku_jyoko 18:0f6f2aa2339d 101 Thread jump_th(jump_receive);
kaku_jyoko 18:0f6f2aa2339d 102 Thread bullet_th(bullet_receive);
kaku_jyoko 29:7784b098ef1d 103 lcd.setmode(XOR);
kaku_jyoko 29:7784b098ef1d 104 int gameOver = 0;
kaku_jyoko 29:7784b098ef1d 105 srand((int)(aIn * 100));
kaku_jyoko 32:abb3086c210d 106 Converter converter();
kaku_jyoko 32:abb3086c210d 107 // Bitmap picture;
kaku_jyoko 32:abb3086c210d 108
kaku_jyoko 5:651879f4f047 109 while(true){
kaku_jyoko 5:651879f4f047 110 update_mtx.lock();
kaku_jyoko 29:7784b098ef1d 111 reset_white_board();
kaku_jyoko 29:7784b098ef1d 112 make_wall();
kaku_jyoko 18:0f6f2aa2339d 113 p_person = h.update(1);
kaku_jyoko 18:0f6f2aa2339d 114 if(b != NULL){
kaku_jyoko 18:0f6f2aa2339d 115 p_bullet = b->update();
kaku_jyoko 18:0f6f2aa2339d 116 }
kaku_jyoko 29:7784b098ef1d 117 //printf("bullet x: %d, y: %d \n", p_bullet.x,p_bullet.y);
kaku_jyoko 29:7784b098ef1d 118 // printf("person x: %d, y: %d \n", p_person.x,p_person.y);
kaku_jyoko 29:7784b098ef1d 119 corrision_judge(p_person, p_bullet);
kaku_jyoko 32:abb3086c210d 120 Bitmap picture = converter.convert(white_board);
kaku_jyoko 5:651879f4f047 121 update_mtx.unlock();
kaku_jyoko 6:e63641e13374 122 lcd.cls();
kaku_jyoko 18:0f6f2aa2339d 123 lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
kaku_jyoko 18:0f6f2aa2339d 124 if(b != NULL){
kaku_jyoko 18:0f6f2aa2339d 125 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 18:0f6f2aa2339d 126 }
kaku_jyoko 5:651879f4f047 127 lcd.copy_to_lcd();
kaku_jyoko 6:e63641e13374 128 wait(0.02);
kaku_jyoko 6:e63641e13374 129 lcd.cls();
kaku_jyoko 18:0f6f2aa2339d 130 lcd.print_bm(bitmPlayerB,p_person.x,p_person.y);
kaku_jyoko 18:0f6f2aa2339d 131 if(b != NULL){
kaku_jyoko 18:0f6f2aa2339d 132 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 18:0f6f2aa2339d 133 }
kaku_jyoko 5:651879f4f047 134 lcd.copy_to_lcd();
kaku_jyoko 6:e63641e13374 135 wait(0.02);
kaku_jyoko 5:651879f4f047 136 }
kaku_jyoko 6:e63641e13374 137 }