![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test avoid bulled game
Dependencies: C12832_lcd LCD_fonts mbed mbed-rtos
main.cpp@40:ec5c1b305b9a, 2016-12-13 (annotated)
- Committer:
- kaku_jyoko
- Date:
- Tue Dec 13 13:32:44 2016 +0000
- Revision:
- 40:ec5c1b305b9a
- Parent:
- 39:e8d6dd3c75c7
- Child:
- 45:7d0a58fbaa8b
fix something
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 | 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 | 5:651879f4f047 | 104 | int main(){ |
kaku_jyoko | 18:0f6f2aa2339d | 105 | point p_person, p_bullet; |
kaku_jyoko | 5:651879f4f047 | 106 | printf("hello\n"); |
kaku_jyoko | 18:0f6f2aa2339d | 107 | Thread jump_th(jump_receive); |
kaku_jyoko | 18:0f6f2aa2339d | 108 | Thread bullet_th(bullet_receive); |
kaku_jyoko | 29:7784b098ef1d | 109 | lcd.setmode(XOR); |
kaku_jyoko | 40:ec5c1b305b9a | 110 | bool gameover = false; |
kaku_jyoko | 40:ec5c1b305b9a | 111 | bool isDisplay = false; |
kaku_jyoko | 29:7784b098ef1d | 112 | srand((int)(aIn * 100)); |
th_mbed | 36:b88fa8ff0be9 | 113 | Converter converter; |
kaku_jyoko | 39:e8d6dd3c75c7 | 114 | Bitmap picture; |
kaku_jyoko | 32:abb3086c210d | 115 | |
kaku_jyoko | 5:651879f4f047 | 116 | while(true){ |
kaku_jyoko | 40:ec5c1b305b9a | 117 | if(!gameover){ |
kaku_jyoko | 40:ec5c1b305b9a | 118 | update_mtx.lock(); |
kaku_jyoko | 40:ec5c1b305b9a | 119 | reset_white_board(); |
kaku_jyoko | 40:ec5c1b305b9a | 120 | make_wall(); |
kaku_jyoko | 40:ec5c1b305b9a | 121 | p_person = h.update(wall_height[3]); |
kaku_jyoko | 40:ec5c1b305b9a | 122 | // p_person = h.update(1); |
kaku_jyoko | 40:ec5c1b305b9a | 123 | if(b != NULL){ |
kaku_jyoko | 40:ec5c1b305b9a | 124 | p_bullet = b->update(); |
kaku_jyoko | 40:ec5c1b305b9a | 125 | } |
kaku_jyoko | 40:ec5c1b305b9a | 126 | //printf("bullet x: %d, y: %d \n", p_bullet.x,p_bullet.y); |
kaku_jyoko | 40:ec5c1b305b9a | 127 | //printf("person x: %d, y: %d \n", p_person.x,p_person.y); |
kaku_jyoko | 40:ec5c1b305b9a | 128 | gameover = bullet_collision(p_person, p_bullet); |
kaku_jyoko | 40:ec5c1b305b9a | 129 | picture = converter.convert(white_board); |
kaku_jyoko | 40:ec5c1b305b9a | 130 | update_mtx.unlock(); |
kaku_jyoko | 40:ec5c1b305b9a | 131 | |
kaku_jyoko | 40:ec5c1b305b9a | 132 | lcd.cls(); |
kaku_jyoko | 40:ec5c1b305b9a | 133 | lcd.print_bm(bitmPlayer,p_person.x,p_person.y); |
kaku_jyoko | 40:ec5c1b305b9a | 134 | lcd.print_bm(picture,0,0); |
kaku_jyoko | 40:ec5c1b305b9a | 135 | if(b != NULL){ |
kaku_jyoko | 40:ec5c1b305b9a | 136 | lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y); |
kaku_jyoko | 40:ec5c1b305b9a | 137 | } |
kaku_jyoko | 40:ec5c1b305b9a | 138 | lcd.copy_to_lcd(); |
kaku_jyoko | 40:ec5c1b305b9a | 139 | wait(0.02); |
kaku_jyoko | 40:ec5c1b305b9a | 140 | lcd.cls(); |
kaku_jyoko | 40:ec5c1b305b9a | 141 | lcd.print_bm(bitmPlayerB,p_person.x,p_person.y); |
kaku_jyoko | 40:ec5c1b305b9a | 142 | lcd.print_bm(picture,0,0); |
kaku_jyoko | 40:ec5c1b305b9a | 143 | if(b != NULL){ |
kaku_jyoko | 40:ec5c1b305b9a | 144 | lcd.print_bm(bitmBullet_graphics, p_bullet.x, p_bullet.y); |
kaku_jyoko | 40:ec5c1b305b9a | 145 | } |
kaku_jyoko | 40:ec5c1b305b9a | 146 | lcd.copy_to_lcd(); |
kaku_jyoko | 40:ec5c1b305b9a | 147 | wait(0.02); |
kaku_jyoko | 40:ec5c1b305b9a | 148 | } else { |
kaku_jyoko | 40:ec5c1b305b9a | 149 | if(!isDisplay){ |
kaku_jyoko | 40:ec5c1b305b9a | 150 | lcd.cls(); |
kaku_jyoko | 40:ec5c1b305b9a | 151 | lcd.locate(5,10); |
kaku_jyoko | 40:ec5c1b305b9a | 152 | lcd.printf("Game Over."); |
kaku_jyoko | 40:ec5c1b305b9a | 153 | lcd.locate(5,22); |
kaku_jyoko | 40:ec5c1b305b9a | 154 | lcd.printf("Your record is %d.", stage.getLength()); |
kaku_jyoko | 40:ec5c1b305b9a | 155 | lcd.copy_to_lcd(); |
kaku_jyoko | 40:ec5c1b305b9a | 156 | isDisplay = true; |
kaku_jyoko | 40:ec5c1b305b9a | 157 | } |
kaku_jyoko | 40:ec5c1b305b9a | 158 | |
kaku_jyoko | 18:0f6f2aa2339d | 159 | } |
kaku_jyoko | 5:651879f4f047 | 160 | } |
kaku_jyoko | 6:e63641e13374 | 161 | } |