test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Committer:
kaku_jyoko
Date:
Wed Dec 14 03:03:29 2016 +0000
Revision:
56:2ab95bb6fbcc
Parent:
55:e4e2274e5ec0
Child:
57:873b89862cfb
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 6:e63641e13374 21 Person h;
kaku_jyoko 29:7784b098ef1d 22 Stage stage;
kaku_jyoko 18:0f6f2aa2339d 23 Serial pc(USBTX, USBRX); // tx, rx
kaku_jyoko 18:0f6f2aa2339d 24 Bullet* b = NULL;
kaku_jyoko 19:55f3114f3996 25 Timer t;
kaku_jyoko 29:7784b098ef1d 26 int white_board[LCD_Y][LCD_X];
kaku_jyoko 29:7784b098ef1d 27 int* wall_height;
kaku_jyoko 18:0f6f2aa2339d 28
kaku_jyoko 55:e4e2274e5ec0 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 55:e4e2274e5ec0 50 void call_jump(){
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 55:e4e2274e5ec0 57 button.rise(&call_jump);
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 55:e4e2274e5ec0 64 //bullet option
kaku_jyoko 55:e4e2274e5ec0 65 int fast = 4;
kaku_jyoko 55:e4e2274e5ec0 66 int normal = 3;
kaku_jyoko 55:e4e2274e5ec0 67 int slow = 2;
kaku_jyoko 55:e4e2274e5ec0 68 int high = 17;
kaku_jyoko 55:e4e2274e5ec0 69 int middle = 12;
kaku_jyoko 55:e4e2274e5ec0 70 int low = 7;
kaku_jyoko 55:e4e2274e5ec0 71
kaku_jyoko 18:0f6f2aa2339d 72 point start;
kaku_jyoko 18:0f6f2aa2339d 73 start.x = 127;
kaku_jyoko 18:0f6f2aa2339d 74 char c;
kaku_jyoko 18:0f6f2aa2339d 75 while(true){
kaku_jyoko 19:55f3114f3996 76 t.start();
kaku_jyoko 18:0f6f2aa2339d 77 c = pc.getc();
kaku_jyoko 55:e4e2274e5ec0 78 if(c >= '1' && c <= '9' && t.read() > 5.0){
kaku_jyoko 19:55f3114f3996 79 b = NULL;
kaku_jyoko 55:e4e2274e5ec0 80 switch(c){
kaku_jyoko 55:e4e2274e5ec0 81 case '1':
kaku_jyoko 55:e4e2274e5ec0 82 start.y = LCD_Y - high;
kaku_jyoko 55:e4e2274e5ec0 83 b = &Bullet(start, slow);
kaku_jyoko 55:e4e2274e5ec0 84 break;
kaku_jyoko 55:e4e2274e5ec0 85 case '2':
kaku_jyoko 55:e4e2274e5ec0 86 start.y = LCD_Y - high;
kaku_jyoko 55:e4e2274e5ec0 87 b = &Bullet(start, normal);
kaku_jyoko 55:e4e2274e5ec0 88 break;
kaku_jyoko 55:e4e2274e5ec0 89 case '3':
kaku_jyoko 55:e4e2274e5ec0 90 start.y = LCD_Y - high;
kaku_jyoko 55:e4e2274e5ec0 91 b = &Bullet(start, fast);
kaku_jyoko 55:e4e2274e5ec0 92 break;
kaku_jyoko 55:e4e2274e5ec0 93 case '4':
kaku_jyoko 55:e4e2274e5ec0 94 start.y = LCD_Y - middle;
kaku_jyoko 55:e4e2274e5ec0 95 b = &Bullet(start, slow);
kaku_jyoko 55:e4e2274e5ec0 96 break;
kaku_jyoko 55:e4e2274e5ec0 97 case '5':
kaku_jyoko 55:e4e2274e5ec0 98 start.y = LCD_Y - middle;
kaku_jyoko 55:e4e2274e5ec0 99 b = &Bullet(start, normal);
kaku_jyoko 55:e4e2274e5ec0 100 break;
kaku_jyoko 55:e4e2274e5ec0 101 case '6':
kaku_jyoko 55:e4e2274e5ec0 102 start.y = LCD_Y - middle;
kaku_jyoko 55:e4e2274e5ec0 103 b = &Bullet(start, fast);
kaku_jyoko 55:e4e2274e5ec0 104 break;
kaku_jyoko 55:e4e2274e5ec0 105 case '7':
kaku_jyoko 55:e4e2274e5ec0 106 start.y = LCD_Y - low;
kaku_jyoko 55:e4e2274e5ec0 107 b = &Bullet(start, slow);
kaku_jyoko 55:e4e2274e5ec0 108 break;
kaku_jyoko 55:e4e2274e5ec0 109 case '8':
kaku_jyoko 55:e4e2274e5ec0 110 start.y = LCD_Y - low;
kaku_jyoko 55:e4e2274e5ec0 111 b = &Bullet(start, normal);
kaku_jyoko 55:e4e2274e5ec0 112 break;
kaku_jyoko 55:e4e2274e5ec0 113 case '9':
kaku_jyoko 55:e4e2274e5ec0 114 start.y = LCD_Y - low;
kaku_jyoko 55:e4e2274e5ec0 115 b = &Bullet(start, fast);
kaku_jyoko 55:e4e2274e5ec0 116 break;
kaku_jyoko 55:e4e2274e5ec0 117 default:
kaku_jyoko 55:e4e2274e5ec0 118 break;
kaku_jyoko 55:e4e2274e5ec0 119 }
kaku_jyoko 19:55f3114f3996 120 t.reset();
kaku_jyoko 18:0f6f2aa2339d 121 }
kaku_jyoko 18:0f6f2aa2339d 122 }
kaku_jyoko 18:0f6f2aa2339d 123 }
kaku_jyoko 18:0f6f2aa2339d 124
kaku_jyoko 40:ec5c1b305b9a 125 int xabs(double x,double y){
kaku_jyoko 40:ec5c1b305b9a 126 if(x>=y){
kaku_jyoko 40:ec5c1b305b9a 127 return(x-y);
kaku_jyoko 40:ec5c1b305b9a 128 }
kaku_jyoko 40:ec5c1b305b9a 129 else if(y>x){
kaku_jyoko 40:ec5c1b305b9a 130 return(y-x);
kaku_jyoko 40:ec5c1b305b9a 131 }
kaku_jyoko 55:e4e2274e5ec0 132 return 0;
th_mbed 31:17b4ee41bdc5 133 }
th_mbed 31:17b4ee41bdc5 134
kaku_jyoko 40:ec5c1b305b9a 135 bool bullet_collision(point p_person, point p_bullet){
kaku_jyoko 54:64c0bdcc94ae 136 //彈当たり判定
kaku_jyoko 40:ec5c1b305b9a 137 double person_center_x = p_person.x + PERSON_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 138 double person_center_y = p_person.y + PERSON_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 139
kaku_jyoko 40:ec5c1b305b9a 140 double bullet_center_x = p_bullet.x + BULLET_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 141 double bullet_center_y = p_bullet.y + BULLET_SIZE / 2;
kaku_jyoko 40:ec5c1b305b9a 142
kaku_jyoko 40:ec5c1b305b9a 143 if(xabs(person_center_x, bullet_center_x) < (PERSON_SIZE / 2 + BULLET_SIZE / 2)){
kaku_jyoko 40:ec5c1b305b9a 144 if(xabs(person_center_y, bullet_center_y) < (PERSON_SIZE / 2 + (BULLET_SIZE - 2) / 2)){
kaku_jyoko 40:ec5c1b305b9a 145 return true;
kaku_jyoko 40:ec5c1b305b9a 146 }
kaku_jyoko 40:ec5c1b305b9a 147 }
kaku_jyoko 40:ec5c1b305b9a 148
kaku_jyoko 40:ec5c1b305b9a 149 return false;
kaku_jyoko 29:7784b098ef1d 150 }
kaku_jyoko 29:7784b098ef1d 151
kaku_jyoko 45:7d0a58fbaa8b 152 bool wall_collision(point p_person){
kaku_jyoko 54:64c0bdcc94ae 153 //壁衝突判定と穴落ち判定
kaku_jyoko 45:7d0a58fbaa8b 154 int x = p_person.x + 5;
kaku_jyoko 45:7d0a58fbaa8b 155 int y = p_person.y + 6;
kaku_jyoko 45:7d0a58fbaa8b 156 if(wall_height[x - 1] > (LCD_Y - y)){
kaku_jyoko 45:7d0a58fbaa8b 157 return true;
kaku_jyoko 45:7d0a58fbaa8b 158 }
kaku_jyoko 45:7d0a58fbaa8b 159 return false;
kaku_jyoko 45:7d0a58fbaa8b 160 }
kaku_jyoko 45:7d0a58fbaa8b 161
kaku_jyoko 5:651879f4f047 162 int main(){
kaku_jyoko 18:0f6f2aa2339d 163 point p_person, p_bullet;
kaku_jyoko 18:0f6f2aa2339d 164 Thread jump_th(jump_receive);
kaku_jyoko 18:0f6f2aa2339d 165 Thread bullet_th(bullet_receive);
kaku_jyoko 29:7784b098ef1d 166 lcd.setmode(XOR);
kaku_jyoko 40:ec5c1b305b9a 167 bool gameover = false;
kaku_jyoko 40:ec5c1b305b9a 168 bool isDisplay = false;
kaku_jyoko 29:7784b098ef1d 169 srand((int)(aIn * 100));
th_mbed 36:b88fa8ff0be9 170 Converter converter;
kaku_jyoko 39:e8d6dd3c75c7 171 Bitmap picture;
kaku_jyoko 55:e4e2274e5ec0 172 printf("Bullet Option\n");
kaku_jyoko 55:e4e2274e5ec0 173 printf(" slow normal fast\n");
kaku_jyoko 55:e4e2274e5ec0 174 printf("high 1 2 3\n");
kaku_jyoko 55:e4e2274e5ec0 175 printf("middle 4 5 6\n");
kaku_jyoko 55:e4e2274e5ec0 176 printf("low 7 8 9\n");
kaku_jyoko 32:abb3086c210d 177
kaku_jyoko 5:651879f4f047 178 while(true){
kaku_jyoko 40:ec5c1b305b9a 179 if(!gameover){
kaku_jyoko 40:ec5c1b305b9a 180 update_mtx.lock();
kaku_jyoko 40:ec5c1b305b9a 181 reset_white_board();
kaku_jyoko 40:ec5c1b305b9a 182 make_wall();
kaku_jyoko 40:ec5c1b305b9a 183 p_person = h.update(wall_height[3]);
kaku_jyoko 55:e4e2274e5ec0 184 //p_person = h.update(1);
kaku_jyoko 40:ec5c1b305b9a 185 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 186 p_bullet = b->update();
kaku_jyoko 40:ec5c1b305b9a 187 }
kaku_jyoko 40:ec5c1b305b9a 188 picture = converter.convert(white_board);
kaku_jyoko 55:e4e2274e5ec0 189
kaku_jyoko 55:e4e2274e5ec0 190 gameover |= bullet_collision(p_person, p_bullet);
kaku_jyoko 55:e4e2274e5ec0 191 gameover |= wall_collision(p_person);
kaku_jyoko 40:ec5c1b305b9a 192 update_mtx.unlock();
kaku_jyoko 40:ec5c1b305b9a 193
kaku_jyoko 40:ec5c1b305b9a 194 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 195 lcd.print_bm(bitmPlayer,p_person.x,p_person.y);
kaku_jyoko 40:ec5c1b305b9a 196 lcd.print_bm(picture,0,0);
kaku_jyoko 40:ec5c1b305b9a 197 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 198 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 40:ec5c1b305b9a 199 }
kaku_jyoko 40:ec5c1b305b9a 200 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 201 wait(0.02);
kaku_jyoko 40:ec5c1b305b9a 202 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 203 lcd.print_bm(bitmPlayerB,p_person.x,p_person.y);
kaku_jyoko 40:ec5c1b305b9a 204 lcd.print_bm(picture,0,0);
kaku_jyoko 40:ec5c1b305b9a 205 if(b != NULL){
kaku_jyoko 40:ec5c1b305b9a 206 lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y);
kaku_jyoko 40:ec5c1b305b9a 207 }
kaku_jyoko 40:ec5c1b305b9a 208 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 209 wait(0.02);
kaku_jyoko 40:ec5c1b305b9a 210 } else {
kaku_jyoko 40:ec5c1b305b9a 211 if(!isDisplay){
kaku_jyoko 40:ec5c1b305b9a 212 lcd.cls();
kaku_jyoko 40:ec5c1b305b9a 213 lcd.locate(5,10);
kaku_jyoko 40:ec5c1b305b9a 214 lcd.printf("Game Over.");
kaku_jyoko 40:ec5c1b305b9a 215 lcd.locate(5,22);
kaku_jyoko 40:ec5c1b305b9a 216 lcd.printf("Your record is %d.", stage.getLength());
kaku_jyoko 40:ec5c1b305b9a 217 lcd.copy_to_lcd();
kaku_jyoko 40:ec5c1b305b9a 218 isDisplay = true;
kaku_jyoko 40:ec5c1b305b9a 219 }
kaku_jyoko 40:ec5c1b305b9a 220
kaku_jyoko 18:0f6f2aa2339d 221 }
kaku_jyoko 5:651879f4f047 222 }
kaku_jyoko 6:e63641e13374 223 }