test avoid bulled game

Dependencies:   C12832_lcd LCD_fonts mbed mbed-rtos

Committer:
kaku_jyoko
Date:
Wed Dec 14 12:04:51 2016 +0000
Revision:
61:d44faf074743
Parent:
60:d04bc5987d53
Child:
62:5d3f6c919fbc
???

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