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