test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Committer:
kaku_jyoko
Date:
Tue Dec 13 14:05:32 2016 +0000
Revision:
47:50c54cb24316
Parent:
45:7d0a58fbaa8b
Child:
49:726ebd187f75
aaa

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 39:e8d6dd3c75c7 43 if(wall_height[j] > (LCD_Y - 1 - i)){
kaku_jyoko 37:f5d98f22b6bd 44 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
kaku_jyoko 40:ec5c1b305b9a 79 int xabs(double x,double y){
kaku_jyoko 40:ec5c1b305b9a 80 if(x>=y){
kaku_jyoko 40:ec5c1b305b9a 81 return(x-y);
kaku_jyoko 40:ec5c1b305b9a 82 }
kaku_jyoko 40:ec5c1b305b9a 83 else if(y>x){
kaku_jyoko 40:ec5c1b305b9a 84 return(y-x);
kaku_jyoko 40:ec5c1b305b9a 85 }
th_mbed 31:17b4ee41bdc5 86 }
th_mbed 31:17b4ee41bdc5 87
kaku_jyoko 40:ec5c1b305b9a 88 bool bullet_collision(point p_person, point p_bullet){
kaku_jyoko 40:ec5c1b305b9a 89 double person_center_x = p_person.x + PERSON_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 90 double person_center_y = p_person.y + PERSON_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 91
kaku_jyoko 40:ec5c1b305b9a 92 double bullet_center_x = p_bullet.x + BULLET_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 93 double bullet_center_y = p_bullet.y + BULLET_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 94
kaku_jyoko 40:ec5c1b305b9a 95 if(xabs(person_center_x, bullet_center_x) < (PERSON_SIZE / 2 + BULLET_SIZE / 2)){
kaku_jyoko 40:ec5c1b305b9a 96 if(xabs(person_center_y, bullet_center_y) < (PERSON_SIZE / 2 + (BULLET_SIZE - 2) / 2)){
kaku_jyoko 40:ec5c1b305b9a 97 return true;
kaku_jyoko 40:ec5c1b305b9a 98 }
kaku_jyoko 40:ec5c1b305b9a 99 }
kaku_jyoko 40:ec5c1b305b9a 100
kaku_jyoko 40:ec5c1b305b9a 101 return false;
kaku_jyoko 29:7784b098ef1d 102 }
kaku_jyoko 29:7784b098ef1d 103
kaku_jyoko 45:7d0a58fbaa8b 104 bool wall_collision(point p_person){
kaku_jyoko 45:7d0a58fbaa8b 105 int x = p_person.x + 5;
kaku_jyoko 45:7d0a58fbaa8b 106 int y = p_person.y + 6;
kaku_jyoko 47:50c54cb24316 107 printf("%d %d\n", wall_height[x - 1] , (LCD_Y - y));
kaku_jyoko 45:7d0a58fbaa8b 108 if(wall_height[x - 1] > (LCD_Y - y)){
kaku_jyoko 45:7d0a58fbaa8b 109 return true;
kaku_jyoko 45:7d0a58fbaa8b 110 }
kaku_jyoko 45:7d0a58fbaa8b 111 return false;
kaku_jyoko 45:7d0a58fbaa8b 112 }
kaku_jyoko 45:7d0a58fbaa8b 113
kaku_jyoko 5:651879f4f047 114 int main(){
kaku_jyoko 18:0f6f2aa2339d 115 point p_person, p_bullet;
kaku_jyoko 5:651879f4f047 116 printf("hello\n");
kaku_jyoko 18:0f6f2aa2339d 117 Thread jump_th(jump_receive);
kaku_jyoko 18:0f6f2aa2339d 118 Thread bullet_th(bullet_receive);
kaku_jyoko 29:7784b098ef1d 119 lcd.setmode(XOR);
kaku_jyoko 40:ec5c1b305b9a 120 bool gameover = false;
kaku_jyoko 40:ec5c1b305b9a 121 bool isDisplay = false;
kaku_jyoko 29:7784b098ef1d 122 srand((int)(aIn * 100));
th_mbed 36:b88fa8ff0be9 123 Converter converter;
kaku_jyoko 39:e8d6dd3c75c7 124 Bitmap picture;
kaku_jyoko 32:abb3086c210d 125
kaku_jyoko 5:651879f4f047 126 while(true){
kaku_jyoko 40:ec5c1b305b9a 127 if(!gameover){
kaku_jyoko 40:ec5c1b305b9a 128 update_mtx.lock();
kaku_jyoko 40:ec5c1b305b9a 129 reset_white_board();
kaku_jyoko 40:ec5c1b305b9a 130 make_wall();
kaku_jyoko 40:ec5c1b305b9a 131 p_person = h.update(wall_height[3]);
kaku_jyoko 40:ec5c1b305b9a 132 // p_person = h.update(1);
kaku_jyoko 40:ec5c1b305b9a 133 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 134 p_bullet = b->update();
kaku_jyoko 40:ec5c1b305b9a 135 }
kaku_jyoko 40:ec5c1b305b9a 136 //printf("bullet x: %d, y: %d \n", p_bullet.x,p_bullet.y);
kaku_jyoko 40:ec5c1b305b9a 137 //printf("person x: %d, y: %d \n", p_person.x,p_person.y);
kaku_jyoko 40:ec5c1b305b9a 138 gameover = bullet_collision(p_person, p_bullet);
kaku_jyoko 45:7d0a58fbaa8b 139 gameover = wall_collision(p_person);
kaku_jyoko 40:ec5c1b305b9a 140 picture = converter.convert(white_board);
kaku_jyoko 40:ec5c1b305b9a 141 update_mtx.unlock();
kaku_jyoko 40:ec5c1b305b9a 142
kaku_jyoko 40:ec5c1b305b9a 143 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 144 lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
kaku_jyoko 40:ec5c1b305b9a 145 lcd.print_bm(picture,0,0);
kaku_jyoko 40:ec5c1b305b9a 146 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 147 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 40:ec5c1b305b9a 148 }
kaku_jyoko 40:ec5c1b305b9a 149 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 150 wait(0.02);
kaku_jyoko 40:ec5c1b305b9a 151 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 152 lcd.print_bm(bitmPlayerB,p_person.x,p_person.y);
kaku_jyoko 40:ec5c1b305b9a 153 lcd.print_bm(picture,0,0);
kaku_jyoko 40:ec5c1b305b9a 154 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 155 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 40:ec5c1b305b9a 156 }
kaku_jyoko 40:ec5c1b305b9a 157 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 158 wait(0.02);
kaku_jyoko 40:ec5c1b305b9a 159 } else {
kaku_jyoko 40:ec5c1b305b9a 160 if(!isDisplay){
kaku_jyoko 40:ec5c1b305b9a 161 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 162 lcd.locate(5,10);
kaku_jyoko 40:ec5c1b305b9a 163 lcd.printf("Game Over.");
kaku_jyoko 40:ec5c1b305b9a 164 lcd.locate(5,22);
kaku_jyoko 40:ec5c1b305b9a 165 lcd.printf("Your record is %d.", stage.getLength());
kaku_jyoko 40:ec5c1b305b9a 166 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 167 isDisplay = true;
kaku_jyoko 40:ec5c1b305b9a 168 }
kaku_jyoko 40:ec5c1b305b9a 169
kaku_jyoko 18:0f6f2aa2339d 170 }
kaku_jyoko 5:651879f4f047 171 }
kaku_jyoko 6:e63641e13374 172 }