2035
Dependencies: 4DGL-uLCD-SE mbed wave_player
Fork of missile_command by
player.cpp@3:7e33224a6f1d, 2016-11-09 (annotated)
- Committer:
- tianyeapply
- Date:
- Wed Nov 09 17:04:42 2016 +0000
- Revision:
- 3:7e33224a6f1d
- Parent:
- 0:532cb55d6136
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tianyeapply | 3:7e33224a6f1d | 1 | |
arvahsu | 0:532cb55d6136 | 2 | |
arvahsu | 0:532cb55d6136 | 3 | // Template of player implementation |
arvahsu | 0:532cb55d6136 | 4 | #include "mbed.h" |
arvahsu | 0:532cb55d6136 | 5 | #include "uLCD_4DGL.h" |
arvahsu | 0:532cb55d6136 | 6 | #include "globals.h" |
arvahsu | 0:532cb55d6136 | 7 | #include "player.h" |
tianyeapply | 3:7e33224a6f1d | 8 | #include "missile_public.h" |
tianyeapply | 3:7e33224a6f1d | 9 | #include "city_landscape_public.h" |
tianyeapply | 3:7e33224a6f1d | 10 | #define pi 3.14159265 |
arvahsu | 0:532cb55d6136 | 11 | void player_draw(int x, int y) { |
tianyeapply | 3:7e33224a6f1d | 12 | uLCD.circle(x+2,y,2,0x58C1FD); |
tianyeapply | 3:7e33224a6f1d | 13 | uLCD.circle(x+14,y,2,0x58C1FD); |
tianyeapply | 3:7e33224a6f1d | 14 | uLCD.rectangle(x+4, y-1.5, x+12, y+1.5, 0xECE9D8); |
tianyeapply | 3:7e33224a6f1d | 15 | uLCD.triangle(x+2,y-1.5, x+12, y-1.5,x+9,y-8.5,0xECE9D8); |
tianyeapply | 3:7e33224a6f1d | 16 | //uLCD.filled_rectangle(x+PLAYER_DELTA, y-PLAYER_DELTA, x+PLAYER_WIDTH-PLAYER_DELTA, y+PLAYER_HEIGHT, PLAYER_COLOR); |
arvahsu | 0:532cb55d6136 | 17 | } |
tianyeapply | 3:7e33224a6f1d | 18 | void player_clear(int x,int y){ |
tianyeapply | 3:7e33224a6f1d | 19 | uLCD.circle(x+2,y,2,BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 20 | uLCD.circle(x+14,y,2,BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 21 | uLCD.rectangle(x+4, y-1.5, x+12, y+1.5, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 22 | uLCD.triangle(x+2,y-1.5, x+12, y-1.5,x+9,y-8.5,BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 23 | //uLCD.filled_rectangle(x, y, x+PLAYER_WIDTH, y+PLAYER_HEIGHT, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 24 | //uLCD.filled_rectangle(x+PLAYER_DELTA, y-PLAYER_DELTA, x+PLAYER_WIDTH-PLAYER_DELTA, y+PLAYER_HEIGHT, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 25 | } |
arvahsu | 0:532cb55d6136 | 26 | // ... You need to implement your own functions for player ... |
arvahsu | 0:532cb55d6136 | 27 | |
arvahsu | 0:532cb55d6136 | 28 | |
tianyeapply | 3:7e33224a6f1d | 29 | |
tianyeapply | 3:7e33224a6f1d | 30 | |
tianyeapply | 3:7e33224a6f1d | 31 | |
tianyeapply | 3:7e33224a6f1d | 32 | |
tianyeapply | 3:7e33224a6f1d | 33 | |
tianyeapply | 3:7e33224a6f1d | 34 | |
tianyeapply | 3:7e33224a6f1d | 35 | |
tianyeapply | 3:7e33224a6f1d | 36 | |
tianyeapply | 3:7e33224a6f1d | 37 | |
tianyeapply | 3:7e33224a6f1d | 38 | MISSILE player_missile_record[MAX_NUM_PLAYER_MISSILE]; |
tianyeapply | 3:7e33224a6f1d | 39 | int player_missile_tick=1; |
tianyeapply | 3:7e33224a6f1d | 40 | int player_missile_interval = 17; |
tianyeapply | 3:7e33224a6f1d | 41 | int player_missile_speed = 8; |
tianyeapply | 3:7e33224a6f1d | 42 | int num_missile=0; |
tianyeapply | 3:7e33224a6f1d | 43 | void player_missile_generator(int x, int y){ |
tianyeapply | 3:7e33224a6f1d | 44 | player_missile_tick++; |
tianyeapply | 3:7e33224a6f1d | 45 | // only fire the missile at certain ticks |
tianyeapply | 3:7e33224a6f1d | 46 | if((player_missile_tick % player_missile_interval)==0 || player_missile_tick==1){ |
tianyeapply | 3:7e33224a6f1d | 47 | player_missile_create(x,y); |
tianyeapply | 3:7e33224a6f1d | 48 | } |
tianyeapply | 3:7e33224a6f1d | 49 | |
tianyeapply | 3:7e33224a6f1d | 50 | // update the missiles and draw them |
tianyeapply | 3:7e33224a6f1d | 51 | player_missile_update_position(); |
tianyeapply | 3:7e33224a6f1d | 52 | } |
tianyeapply | 3:7e33224a6f1d | 53 | |
tianyeapply | 3:7e33224a6f1d | 54 | void player_set_missile_speed(int speed){ |
tianyeapply | 3:7e33224a6f1d | 55 | ASSERT_P(player_missile_speed>=1 && player_missile_speed<=8,ERROR_MISSILE_SPEED); |
tianyeapply | 3:7e33224a6f1d | 56 | if(player_missile_speed>=1 && player_missile_speed<=8){ |
tianyeapply | 3:7e33224a6f1d | 57 | player_missile_speed = speed; |
tianyeapply | 3:7e33224a6f1d | 58 | } |
tianyeapply | 3:7e33224a6f1d | 59 | } |
tianyeapply | 3:7e33224a6f1d | 60 | |
tianyeapply | 3:7e33224a6f1d | 61 | // set missile interval (default interval is 10) |
tianyeapply | 3:7e33224a6f1d | 62 | void player_set_missile_interval(int interval){ |
tianyeapply | 3:7e33224a6f1d | 63 | ASSERT_P(player_missile_interval>=1 && player_missile_interval<=100,ERROR_MISSILE_INTERVAL); |
tianyeapply | 3:7e33224a6f1d | 64 | if(player_missile_interval>=1 && interval<=100){ |
tianyeapply | 3:7e33224a6f1d | 65 | player_missile_interval = interval; |
tianyeapply | 3:7e33224a6f1d | 66 | } |
tianyeapply | 3:7e33224a6f1d | 67 | } |
tianyeapply | 3:7e33224a6f1d | 68 | |
tianyeapply | 3:7e33224a6f1d | 69 | |
tianyeapply | 3:7e33224a6f1d | 70 | |
tianyeapply | 3:7e33224a6f1d | 71 | void player_missile_create(int current_x, int intcurrent_y){ |
tianyeapply | 3:7e33224a6f1d | 72 | int i; |
tianyeapply | 3:7e33224a6f1d | 73 | for(i=0;i<MAX_NUM_PLAYER_MISSILE;i++){ |
tianyeapply | 3:7e33224a6f1d | 74 | if(player_missile_record[i].status == MISSILE_DEACTIVE){ |
tianyeapply | 3:7e33224a6f1d | 75 | |
tianyeapply | 3:7e33224a6f1d | 76 | player_missile_record[i].y = 97; |
tianyeapply | 3:7e33224a6f1d | 77 | //each missile has its own tick |
tianyeapply | 3:7e33224a6f1d | 78 | player_missile_record[i].tick = 0; |
tianyeapply | 3:7e33224a6f1d | 79 | //missile_record[i].tick = missile_tick; |
tianyeapply | 3:7e33224a6f1d | 80 | //set a random source for the missile |
tianyeapply | 3:7e33224a6f1d | 81 | player_missile_record[i].source_x = current_x; |
tianyeapply | 3:7e33224a6f1d | 82 | player_missile_record[i].source_y = 97; |
tianyeapply | 3:7e33224a6f1d | 83 | //set a random target for the missile |
tianyeapply | 3:7e33224a6f1d | 84 | player_missile_record[i].target_y = 0; |
tianyeapply | 3:7e33224a6f1d | 85 | player_missile_record[i].target_x = current_x; |
tianyeapply | 3:7e33224a6f1d | 86 | //player_missile_record[i].y = player_missile_record[i].source_y; |
tianyeapply | 3:7e33224a6f1d | 87 | player_missile_record[i].status = MISSILE_ACTIVE; |
tianyeapply | 3:7e33224a6f1d | 88 | num_missile++; |
tianyeapply | 3:7e33224a6f1d | 89 | break; |
tianyeapply | 3:7e33224a6f1d | 90 | } |
tianyeapply | 3:7e33224a6f1d | 91 | } |
tianyeapply | 3:7e33224a6f1d | 92 | } |
tianyeapply | 3:7e33224a6f1d | 93 | |
tianyeapply | 3:7e33224a6f1d | 94 | |
tianyeapply | 3:7e33224a6f1d | 95 | |
tianyeapply | 3:7e33224a6f1d | 96 | |
tianyeapply | 3:7e33224a6f1d | 97 | |
tianyeapply | 3:7e33224a6f1d | 98 | |
tianyeapply | 3:7e33224a6f1d | 99 | |
tianyeapply | 3:7e33224a6f1d | 100 | |
tianyeapply | 3:7e33224a6f1d | 101 | |
tianyeapply | 3:7e33224a6f1d | 102 | |
tianyeapply | 3:7e33224a6f1d | 103 | |
tianyeapply | 3:7e33224a6f1d | 104 | void player_missile_update_position(void){ |
tianyeapply | 3:7e33224a6f1d | 105 | int i; |
tianyeapply | 3:7e33224a6f1d | 106 | player_missile_tick++; |
tianyeapply | 3:7e33224a6f1d | 107 | //controls how fast the missile will move |
tianyeapply | 3:7e33224a6f1d | 108 | //int rate = player_missile_speed * 25; |
tianyeapply | 3:7e33224a6f1d | 109 | int rate = player_missile_speed * 25; |
tianyeapply | 3:7e33224a6f1d | 110 | //delta_x and delta_y account for the slope of the missile |
tianyeapply | 3:7e33224a6f1d | 111 | double delta_y; |
tianyeapply | 3:7e33224a6f1d | 112 | for(i=0;i<MAX_NUM_PLAYER_MISSILE;i++){ |
tianyeapply | 3:7e33224a6f1d | 113 | if(player_missile_record[i].status == MISSILE_ACTIVE){ |
tianyeapply | 3:7e33224a6f1d | 114 | // update missile position |
tianyeapply | 3:7e33224a6f1d | 115 | //delta_x = (player_missile_record[i].target_x - player_missile_record[i].source_x)/rate; |
tianyeapply | 3:7e33224a6f1d | 116 | delta_y = -200/rate; |
tianyeapply | 3:7e33224a6f1d | 117 | player_missile_draw(player_missile_record[i], BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 118 | //player_missile_record[i].x = player_missile_record[i].source_x; |
tianyeapply | 3:7e33224a6f1d | 119 | player_missile_record[i].y = (int)(player_missile_record[i].source_y + delta_y*(player_missile_record[i].tick%rate)); |
tianyeapply | 3:7e33224a6f1d | 120 | // player_missile_record[i].y = (int)(delta_y*(player_missile_record[i].tick%rate)); |
tianyeapply | 3:7e33224a6f1d | 121 | player_missile_record[i].x = (int)(player_missile_record[i].source_x); |
tianyeapply | 3:7e33224a6f1d | 122 | |
tianyeapply | 3:7e33224a6f1d | 123 | // draw missile |
tianyeapply | 3:7e33224a6f1d | 124 | player_missile_draw(player_missile_record[i], 0x508ADF); |
tianyeapply | 3:7e33224a6f1d | 125 | player_missile_record[i].tick++; |
tianyeapply | 3:7e33224a6f1d | 126 | //draw_landscape(); |
tianyeapply | 3:7e33224a6f1d | 127 | //update missile's internal tick |
tianyeapply | 3:7e33224a6f1d | 128 | if (player_missile_record[i].tick==rate){ |
tianyeapply | 3:7e33224a6f1d | 129 | player_missile_record[i].status= MISSILE_DEACTIVE; |
tianyeapply | 3:7e33224a6f1d | 130 | |
tianyeapply | 3:7e33224a6f1d | 131 | player_missile_record[i].tick = 0; |
tianyeapply | 3:7e33224a6f1d | 132 | num_missile--; |
tianyeapply | 3:7e33224a6f1d | 133 | } |
tianyeapply | 3:7e33224a6f1d | 134 | //player_missile_record[i].tick++; |
tianyeapply | 3:7e33224a6f1d | 135 | } |
tianyeapply | 3:7e33224a6f1d | 136 | else if(player_missile_record[i].status == MISSILE_EXPLODED){ |
tianyeapply | 3:7e33224a6f1d | 137 | // clear the missile on the screen |
tianyeapply | 3:7e33224a6f1d | 138 | |
tianyeapply | 3:7e33224a6f1d | 139 | player_missile_draw(player_missile_record[i], BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 140 | |
tianyeapply | 3:7e33224a6f1d | 141 | // we have done with this missile, remove it from record |
tianyeapply | 3:7e33224a6f1d | 142 | player_missile_record[i].status = MISSILE_DEACTIVE; |
tianyeapply | 3:7e33224a6f1d | 143 | //resets the missile's internal tick |
tianyeapply | 3:7e33224a6f1d | 144 | player_missile_record[i].tick = 0; |
tianyeapply | 3:7e33224a6f1d | 145 | num_missile--; |
tianyeapply | 3:7e33224a6f1d | 146 | } |
tianyeapply | 3:7e33224a6f1d | 147 | |
tianyeapply | 3:7e33224a6f1d | 148 | } |
tianyeapply | 3:7e33224a6f1d | 149 | } |
tianyeapply | 3:7e33224a6f1d | 150 | |
tianyeapply | 3:7e33224a6f1d | 151 | MISSILE player_missile_get_info(int index){ |
tianyeapply | 3:7e33224a6f1d | 152 | // All interface for user should have error checking |
tianyeapply | 3:7e33224a6f1d | 153 | ASSERT_P(index<MAX_NUM_PLAYER_MISSILE,ERROR_MISSILE_INDEX_GET_INFO); |
tianyeapply | 3:7e33224a6f1d | 154 | |
tianyeapply | 3:7e33224a6f1d | 155 | return player_missile_record[index]; |
tianyeapply | 3:7e33224a6f1d | 156 | } |
tianyeapply | 3:7e33224a6f1d | 157 | |
tianyeapply | 3:7e33224a6f1d | 158 | |
tianyeapply | 3:7e33224a6f1d | 159 | void player_missile_set_exploded(int index){ |
tianyeapply | 3:7e33224a6f1d | 160 | // All interface for user should have error checking |
tianyeapply | 3:7e33224a6f1d | 161 | ASSERT_P(index<MAX_NUM_PLAYER_MISSILE,ERROR_MISSILE_INDEX_UPDATE_STATUS); |
tianyeapply | 3:7e33224a6f1d | 162 | |
tianyeapply | 3:7e33224a6f1d | 163 | player_missile_record[index].status = MISSILE_EXPLODED; |
tianyeapply | 3:7e33224a6f1d | 164 | } |
tianyeapply | 3:7e33224a6f1d | 165 | |
tianyeapply | 3:7e33224a6f1d | 166 | |
tianyeapply | 3:7e33224a6f1d | 167 | void player_missile_draw(MISSILE player_missile, int color){ |
tianyeapply | 3:7e33224a6f1d | 168 | int init_x,init_y,current_x,current_y; |
tianyeapply | 3:7e33224a6f1d | 169 | |
tianyeapply | 3:7e33224a6f1d | 170 | init_x = player_missile.source_x; |
tianyeapply | 3:7e33224a6f1d | 171 | init_y = 100; |
tianyeapply | 3:7e33224a6f1d | 172 | current_x = player_missile.x; |
tianyeapply | 3:7e33224a6f1d | 173 | current_y = player_missile.y; |
tianyeapply | 3:7e33224a6f1d | 174 | uLCD.line(init_x, init_y, current_x, current_y, color); |
tianyeapply | 3:7e33224a6f1d | 175 | } |
tianyeapply | 3:7e33224a6f1d | 176 | |
tianyeapply | 3:7e33224a6f1d | 177 | void life_reduce(int x){ |
tianyeapply | 3:7e33224a6f1d | 178 | uLCD.circle(x , 5 , 3, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 179 | } |
tianyeapply | 3:7e33224a6f1d | 180 | |
tianyeapply | 3:7e33224a6f1d | 181 | int life_generate(int life){ |
tianyeapply | 3:7e33224a6f1d | 182 | int i;int x_=0;; |
tianyeapply | 3:7e33224a6f1d | 183 | for(i=0;i<life;i++){ |
tianyeapply | 3:7e33224a6f1d | 184 | x_=5+7*i; |
tianyeapply | 3:7e33224a6f1d | 185 | uLCD.circle( x_, 5 , 3, 0x508ADF); |
tianyeapply | 3:7e33224a6f1d | 186 | } |
tianyeapply | 3:7e33224a6f1d | 187 | |
tianyeapply | 3:7e33224a6f1d | 188 | return x_; |
tianyeapply | 3:7e33224a6f1d | 189 | } |
tianyeapply | 3:7e33224a6f1d | 190 | int num_missile_avi(int x){ |
tianyeapply | 3:7e33224a6f1d | 191 | x= num_missile; |
tianyeapply | 3:7e33224a6f1d | 192 | return x; |
tianyeapply | 3:7e33224a6f1d | 193 | } |
tianyeapply | 3:7e33224a6f1d | 194 | void city_destory(){ |
tianyeapply | 3:7e33224a6f1d | 195 | uLCD.text_char('-', 13, 6, 0xCCAA00); |
tianyeapply | 3:7e33224a6f1d | 196 | uLCD.text_char('1', 14, 6, 0xCCAA00); |
tianyeapply | 3:7e33224a6f1d | 197 | uLCD.text_char('0',15, 6, 0xCCAA00); |
tianyeapply | 3:7e33224a6f1d | 198 | |
tianyeapply | 3:7e33224a6f1d | 199 | wait(0.3); |
tianyeapply | 3:7e33224a6f1d | 200 | uLCD.text_char('-', 13, 6, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 201 | uLCD.text_char('1', 14, 6, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 202 | uLCD.text_char('0', 15, 6, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 203 | } |
tianyeapply | 3:7e33224a6f1d | 204 | void point_get(){ |
tianyeapply | 3:7e33224a6f1d | 205 | uLCD.text_char('+', 13, 6, 0xCCAA00); |
tianyeapply | 3:7e33224a6f1d | 206 | uLCD.text_char('1', 14, 6, 0xCCAA00); |
tianyeapply | 3:7e33224a6f1d | 207 | wait(0.3); |
tianyeapply | 3:7e33224a6f1d | 208 | uLCD.text_char('-', 13, 6, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 209 | uLCD.text_char('1', 14, 6, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 210 | } |
tianyeapply | 3:7e33224a6f1d | 211 | void display_score(int x){ |
tianyeapply | 3:7e33224a6f1d | 212 | char str[8]; |
tianyeapply | 3:7e33224a6f1d | 213 | sprintf(str, "%d", x); |
tianyeapply | 3:7e33224a6f1d | 214 | uLCD.text_string(str,15,1,3,0xFF9900); |
tianyeapply | 3:7e33224a6f1d | 215 | } |
tianyeapply | 3:7e33224a6f1d | 216 | void advanced_level(int speed, int interval){ |
tianyeapply | 3:7e33224a6f1d | 217 | set_missile_interval(interval); |
tianyeapply | 3:7e33224a6f1d | 218 | set_missile_speed( speed); |
tianyeapply | 3:7e33224a6f1d | 219 | } |
tianyeapply | 3:7e33224a6f1d | 220 | void display_level(int x){ |
tianyeapply | 3:7e33224a6f1d | 221 | char str[2]; |
tianyeapply | 3:7e33224a6f1d | 222 | sprintf(str, "%d", x); |
tianyeapply | 3:7e33224a6f1d | 223 | uLCD.text_string(str,0,3,3,0xFF9900); |
tianyeapply | 3:7e33224a6f1d | 224 | } |
tianyeapply | 3:7e33224a6f1d | 225 | void circle_explosion(int x,int y){ |
tianyeapply | 3:7e33224a6f1d | 226 | int angle=0; |
tianyeapply | 3:7e33224a6f1d | 227 | for(angle=0;angle<=360;angle=angle+20){ |
tianyeapply | 3:7e33224a6f1d | 228 | int delta_x=4*cos(angle*pi/180); |
tianyeapply | 3:7e33224a6f1d | 229 | int delta_y=4*sin(angle*pi/180); |
tianyeapply | 3:7e33224a6f1d | 230 | uLCD.filled_circle(x+delta_x-2 , y+delta_y-2 , 2, 0xECE9D8); |
tianyeapply | 3:7e33224a6f1d | 231 | wait(0.15); |
tianyeapply | 3:7e33224a6f1d | 232 | uLCD.filled_circle(x+delta_x-2 , y+delta_y-2 , 2, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 233 | } |
tianyeapply | 3:7e33224a6f1d | 234 | } |
tianyeapply | 3:7e33224a6f1d | 235 | |
tianyeapply | 3:7e33224a6f1d | 236 | void use_fourth_pb(){ |
tianyeapply | 3:7e33224a6f1d | 237 | char str[10]; |
tianyeapply | 3:7e33224a6f1d | 238 | sprintf(str, "%s", "super bomb"); |
tianyeapply | 3:7e33224a6f1d | 239 | uLCD.text_string(str,5,3,3,0xECE9D8); |
tianyeapply | 3:7e33224a6f1d | 240 | wait(0.25); |
tianyeapply | 3:7e33224a6f1d | 241 | uLCD.text_string(str,5,3,3,BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 242 | int i; |
tianyeapply | 3:7e33224a6f1d | 243 | MISSILE missile; |
tianyeapply | 3:7e33224a6f1d | 244 | for(i=0;i<5;i++){ |
tianyeapply | 3:7e33224a6f1d | 245 | |
tianyeapply | 3:7e33224a6f1d | 246 | missile=missile_get_info(i); |
tianyeapply | 3:7e33224a6f1d | 247 | if(missile.status == MISSILE_ACTIVE){ |
tianyeapply | 3:7e33224a6f1d | 248 | |
tianyeapply | 3:7e33224a6f1d | 249 | uLCD.filled_circle(missile.x ,missile.y, 2, 0xECE9D8); |
tianyeapply | 3:7e33224a6f1d | 250 | wait(0.2); |
tianyeapply | 3:7e33224a6f1d | 251 | uLCD.filled_circle(missile.x ,missile.y, 2, BACKGROUND_COLOR); |
tianyeapply | 3:7e33224a6f1d | 252 | missile_set_exploded(i); |
tianyeapply | 3:7e33224a6f1d | 253 | } |
tianyeapply | 3:7e33224a6f1d | 254 | } |
tianyeapply | 3:7e33224a6f1d | 255 | } |