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