Dependencies: 4DGL-uLCD-SE DebounceIn mbed
Fork of AsteroidDefender by
main.cpp@4:6fc77e25bbcf, 2015-10-28 (annotated)
- Committer:
- ndaniel7
- Date:
- Wed Oct 28 15:46:00 2015 +0000
- Revision:
- 4:6fc77e25bbcf
- Parent:
- 3:0b2c776da95d
Added some additional comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rquinn7 | 0:bbc2ad180020 | 1 | #include "mbed.h" |
rquinn7 | 0:bbc2ad180020 | 2 | #include "game.h" |
rquinn7 | 0:bbc2ad180020 | 3 | #include "uLCD_4DGL.h" |
rquinn7 | 0:bbc2ad180020 | 4 | #include <list> |
rquinn7 | 0:bbc2ad180020 | 5 | #include <stdlib.h> |
rquinn7 | 0:bbc2ad180020 | 6 | |
rquinn7 | 0:bbc2ad180020 | 7 | uLCD_4DGL lcd(p28,p27,p29); |
rquinn7 | 0:bbc2ad180020 | 8 | |
rquinn7 | 0:bbc2ad180020 | 9 | DigitalOut myled(LED1); |
ndaniel7 | 2:13ba45ceb03f | 10 | AnalogIn sliderv(p17); |
ndaniel7 | 2:13ba45ceb03f | 11 | AnalogIn sliderh(p19); |
ndaniel7 | 2:13ba45ceb03f | 12 | PwmOut red(p24); |
ndaniel7 | 2:13ba45ceb03f | 13 | PwmOut green(p25); |
rquinn7 | 0:bbc2ad180020 | 14 | DigitalIn pb1(p21); |
ndaniel7 | 4:6fc77e25bbcf | 15 | //if the pushbutton is down |
rquinn7 | 0:bbc2ad180020 | 16 | bool down = false; |
ndaniel7 | 4:6fc77e25bbcf | 17 | //lives remaining |
ndaniel7 | 2:13ba45ceb03f | 18 | int lives = 3; |
ndaniel7 | 2:13ba45ceb03f | 19 | |
ndaniel7 | 2:13ba45ceb03f | 20 | |
ndaniel7 | 2:13ba45ceb03f | 21 | std::list<Enemy> enemies; |
ndaniel7 | 2:13ba45ceb03f | 22 | std::list<Bullet> bullets; |
ndaniel7 | 2:13ba45ceb03f | 23 | Ship ship; |
rquinn7 | 0:bbc2ad180020 | 24 | |
ndaniel7 | 4:6fc77e25bbcf | 25 | //If the enemies have moved, cover their old spot with a black rectangle and redraw them |
ndaniel7 | 2:13ba45ceb03f | 26 | void render_enemies() { |
ndaniel7 | 2:13ba45ceb03f | 27 | for(std::list<Enemy>::iterator j = enemies.begin(); j != enemies.end(); j++){ |
ndaniel7 | 2:13ba45ceb03f | 28 | if(j->moved) { |
ndaniel7 | 2:13ba45ceb03f | 29 | lcd.filled_rectangle(j->old_coord.x-3, j->old_coord.y-3, j->old_coord.x + j->size.x+3, j->old_coord.y + j->size.y+3, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 30 | } |
ndaniel7 | 2:13ba45ceb03f | 31 | lcd.filled_rectangle(j->coord.x, j->coord.y, j->coord.x + j->size.x, j->coord.y + j->size.y, 0x00FFFF); |
ndaniel7 | 2:13ba45ceb03f | 32 | if (!j->moved) { |
ndaniel7 | 2:13ba45ceb03f | 33 | j->move(); |
ndaniel7 | 2:13ba45ceb03f | 34 | } |
ndaniel7 | 2:13ba45ceb03f | 35 | } |
ndaniel7 | 2:13ba45ceb03f | 36 | } |
ndaniel7 | 4:6fc77e25bbcf | 37 | //Set the led color |
ndaniel7 | 2:13ba45ceb03f | 38 | void loseLife(){ |
ndaniel7 | 2:13ba45ceb03f | 39 | lives--; |
ndaniel7 | 2:13ba45ceb03f | 40 | if (lives==2){ |
ndaniel7 | 2:13ba45ceb03f | 41 | red=.5; |
ndaniel7 | 2:13ba45ceb03f | 42 | green=.25; |
ndaniel7 | 2:13ba45ceb03f | 43 | } |
ndaniel7 | 2:13ba45ceb03f | 44 | else if (lives==1){ |
ndaniel7 | 2:13ba45ceb03f | 45 | red=.5; |
ndaniel7 | 2:13ba45ceb03f | 46 | green=0; |
ndaniel7 | 2:13ba45ceb03f | 47 | } |
ndaniel7 | 2:13ba45ceb03f | 48 | else{ |
ndaniel7 | 2:13ba45ceb03f | 49 | red=0; |
ndaniel7 | 2:13ba45ceb03f | 50 | } |
ndaniel7 | 2:13ba45ceb03f | 51 | } |
ndaniel7 | 2:13ba45ceb03f | 52 | |
ndaniel7 | 4:6fc77e25bbcf | 53 | //If the ship has moved, cover their old spot with a black rectangle and redraw them |
ndaniel7 | 2:13ba45ceb03f | 54 | void render_ship() { |
ndaniel7 | 2:13ba45ceb03f | 55 | if(ship.moved) { |
ndaniel7 | 2:13ba45ceb03f | 56 | lcd.filled_rectangle(ship.old_coord.x-5, ship.old_coord.y-5, ship.old_coord.x + ship.size.x + 5, ship.old_coord.y + ship.size.y + 5, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 57 | ship.moved = false; |
ndaniel7 | 2:13ba45ceb03f | 58 | } |
ndaniel7 | 2:13ba45ceb03f | 59 | lcd.filled_rectangle(ship.coord.x, ship.coord.y, ship.coord.x + ship.size.x, ship.coord.y + ship.size.y, 0x00FF00); |
ndaniel7 | 2:13ba45ceb03f | 60 | } |
ndaniel7 | 2:13ba45ceb03f | 61 | |
ndaniel7 | 4:6fc77e25bbcf | 62 | //If the bullets have moved, cover their old spot with a black rectangle and redraw them |
ndaniel7 | 2:13ba45ceb03f | 63 | void render_bullets() { |
ndaniel7 | 2:13ba45ceb03f | 64 | for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ |
rquinn7 | 0:bbc2ad180020 | 65 | if(j->moved) { |
rquinn7 | 0:bbc2ad180020 | 66 | lcd.filled_rectangle(j->old_coord.x, j->old_coord.y, j->old_coord.x + j->size.x, j->old_coord.y + j->size.y, 0x000000); |
rquinn7 | 0:bbc2ad180020 | 67 | } |
ndaniel7 | 2:13ba45ceb03f | 68 | lcd.filled_rectangle(j->coord.x, j->coord.y, j->coord.x + j->size.x, j->coord.y + j->size.y, 0xFFFF00); |
ndaniel7 | 2:13ba45ceb03f | 69 | if(j->coord.y < -4) { |
ndaniel7 | 2:13ba45ceb03f | 70 | j = bullets.erase(j); |
rquinn7 | 0:bbc2ad180020 | 71 | } |
rquinn7 | 0:bbc2ad180020 | 72 | else { |
rquinn7 | 0:bbc2ad180020 | 73 | j->move(); |
rquinn7 | 0:bbc2ad180020 | 74 | } |
rquinn7 | 0:bbc2ad180020 | 75 | } |
rquinn7 | 0:bbc2ad180020 | 76 | } |
rquinn7 | 0:bbc2ad180020 | 77 | |
ndaniel7 | 4:6fc77e25bbcf | 78 | //Collision checking based on coordinates |
rquinn7 | 0:bbc2ad180020 | 79 | void check_collisions() { |
ndaniel7 | 2:13ba45ceb03f | 80 | for(std::list<Enemy>::iterator k = enemies.begin(); k != enemies.end(); k++){ |
ndaniel7 | 2:13ba45ceb03f | 81 | for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ |
ndaniel7 | 2:13ba45ceb03f | 82 | if((j->old_coord.x >= k->old_coord.x && j->old_coord.x <= (k->old_coord.x + k->size.x))||((j->coord.x + j->size.x) >= k->old_coord.x && (j->old_coord.x + j->size.x) <= (k->old_coord.x + k->size.x))){ |
ndaniel7 | 2:13ba45ceb03f | 83 | |
ndaniel7 | 2:13ba45ceb03f | 84 | if( (j->old_coord.y >= k->old_coord.y && j->old_coord.y <= (k->old_coord.y + k->size.y))||((j->old_coord.y + j->size.y) >= k->old_coord.y && (j->old_coord.y + j->size.y) <= (k->old_coord.y + k->size.y)) ){ |
ndaniel7 | 2:13ba45ceb03f | 85 | |
ndaniel7 | 2:13ba45ceb03f | 86 | lcd.filled_rectangle(j->coord.x-4, j->coord.y-4, j->coord.x + j->size.x+4, j->coord.y + j->size.y+6, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 87 | lcd.filled_rectangle(k->coord.x-3, k->coord.y-3, k->coord.x + k->size.x+3, k->coord.y + k->size.y+3, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 88 | k = enemies.erase(k); |
ndaniel7 | 2:13ba45ceb03f | 89 | j = bullets.erase(j); |
ndaniel7 | 2:13ba45ceb03f | 90 | } |
ndaniel7 | 2:13ba45ceb03f | 91 | } |
rquinn7 | 0:bbc2ad180020 | 92 | } |
ndaniel7 | 2:13ba45ceb03f | 93 | //if an enemy collides with the ship, it blows up and takes away a life |
ndaniel7 | 2:13ba45ceb03f | 94 | if((ship.coord.x >= k->coord.x && ship.coord.x <= (k->coord.x + k->size.x))||((ship.coord.x + ship.size.x) >= k->coord.x && (ship.coord.x + ship.size.x) <= (k->coord.x + k->size.x))){ |
ndaniel7 | 2:13ba45ceb03f | 95 | |
ndaniel7 | 2:13ba45ceb03f | 96 | if( (ship.coord.y >= k->coord.y && ship.coord.y <= (k->coord.y + k->size.y))||((ship.coord.y + ship.size.y) >= k->coord.y && (ship.coord.y + ship.size.y) <= (k->coord.y + k->size.y))){ |
ndaniel7 | 2:13ba45ceb03f | 97 | lcd.filled_rectangle(k->coord.x, k->coord.y, k->coord.x + k->size.x, k->coord.y + k->size.y, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 98 | k = enemies.erase(k); |
ndaniel7 | 2:13ba45ceb03f | 99 | loseLife(); |
ndaniel7 | 2:13ba45ceb03f | 100 | } |
ndaniel7 | 2:13ba45ceb03f | 101 | } |
rquinn7 | 0:bbc2ad180020 | 102 | } |
rquinn7 | 0:bbc2ad180020 | 103 | } |
rquinn7 | 0:bbc2ad180020 | 104 | |
rquinn7 | 0:bbc2ad180020 | 105 | |
rquinn7 | 1:34bb7c386b9f | 106 | //If button is pressed, shoot only once |
rquinn7 | 0:bbc2ad180020 | 107 | void check_button() { |
rquinn7 | 0:bbc2ad180020 | 108 | if(pb1 == 0 && !down) { |
rquinn7 | 0:bbc2ad180020 | 109 | down = true; |
ndaniel7 | 2:13ba45ceb03f | 110 | if(bullets.size() < 3) { |
ndaniel7 | 2:13ba45ceb03f | 111 | Bullet bullet(ship.coord); |
ndaniel7 | 2:13ba45ceb03f | 112 | bullets.push_back(bullet); |
rquinn7 | 0:bbc2ad180020 | 113 | } |
rquinn7 | 0:bbc2ad180020 | 114 | } |
ndaniel7 | 2:13ba45ceb03f | 115 | else if(pb1 == 1) { |
rquinn7 | 0:bbc2ad180020 | 116 | down = false; |
rquinn7 | 0:bbc2ad180020 | 117 | } |
rquinn7 | 0:bbc2ad180020 | 118 | } |
rquinn7 | 1:34bb7c386b9f | 119 | |
ndaniel7 | 4:6fc77e25bbcf | 120 | //Calls render methods for all different objects |
rquinn7 | 0:bbc2ad180020 | 121 | void render() { |
ndaniel7 | 2:13ba45ceb03f | 122 | render_enemies(); |
ndaniel7 | 2:13ba45ceb03f | 123 | render_ship(); |
rquinn7 | 0:bbc2ad180020 | 124 | check_button(); |
ndaniel7 | 2:13ba45ceb03f | 125 | render_bullets(); |
rquinn7 | 0:bbc2ad180020 | 126 | check_collisions(); |
rquinn7 | 0:bbc2ad180020 | 127 | } |
rquinn7 | 0:bbc2ad180020 | 128 | |
ndaniel7 | 4:6fc77e25bbcf | 129 | //Analog movement for the ship based on the joystick |
ndaniel7 | 2:13ba45ceb03f | 130 | void ship_move() { |
ndaniel7 | 2:13ba45ceb03f | 131 | if(sliderh < 0.1) { |
ndaniel7 | 2:13ba45ceb03f | 132 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 133 | ship.coord.x += 5; |
ndaniel7 | 2:13ba45ceb03f | 134 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 135 | } |
ndaniel7 | 2:13ba45ceb03f | 136 | else if (sliderh < .2){ |
ndaniel7 | 2:13ba45ceb03f | 137 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 138 | ship.coord.x += 4; |
ndaniel7 | 2:13ba45ceb03f | 139 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 140 | } |
ndaniel7 | 2:13ba45ceb03f | 141 | else if (sliderh < .3){ |
ndaniel7 | 2:13ba45ceb03f | 142 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 143 | ship.coord.x += 3; |
ndaniel7 | 2:13ba45ceb03f | 144 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 145 | } |
ndaniel7 | 2:13ba45ceb03f | 146 | else if (sliderh < .4){ |
ndaniel7 | 2:13ba45ceb03f | 147 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 148 | ship.coord.x += 2; |
ndaniel7 | 2:13ba45ceb03f | 149 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 150 | } |
ndaniel7 | 2:13ba45ceb03f | 151 | if(sliderh > 0.9) { |
ndaniel7 | 2:13ba45ceb03f | 152 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 153 | ship.coord.x -= 5; |
ndaniel7 | 2:13ba45ceb03f | 154 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 155 | } |
ndaniel7 | 2:13ba45ceb03f | 156 | else if(sliderh > 0.8) { |
ndaniel7 | 2:13ba45ceb03f | 157 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 158 | ship.coord.x -= 4; |
ndaniel7 | 2:13ba45ceb03f | 159 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 160 | } |
ndaniel7 | 2:13ba45ceb03f | 161 | else if(sliderh > 0.7) { |
ndaniel7 | 2:13ba45ceb03f | 162 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 163 | ship.coord.x -= 3; |
ndaniel7 | 2:13ba45ceb03f | 164 | ship.moved = true; |
rquinn7 | 0:bbc2ad180020 | 165 | } |
rquinn7 | 0:bbc2ad180020 | 166 | else if(sliderh > 0.6) { |
ndaniel7 | 2:13ba45ceb03f | 167 | ship.old_coord.x = ship.coord.x; |
ndaniel7 | 2:13ba45ceb03f | 168 | ship.coord.x -= 2; |
ndaniel7 | 2:13ba45ceb03f | 169 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 170 | } |
ndaniel7 | 2:13ba45ceb03f | 171 | if (ship.coord.x < 0){ |
ndaniel7 | 2:13ba45ceb03f | 172 | ship.coord.x = 0; |
ndaniel7 | 2:13ba45ceb03f | 173 | } |
ndaniel7 | 2:13ba45ceb03f | 174 | else if (ship.coord.x > 128-ship.size.x){ |
ndaniel7 | 2:13ba45ceb03f | 175 | ship.coord.x = 128-ship.size.x; |
ndaniel7 | 2:13ba45ceb03f | 176 | } |
ndaniel7 | 2:13ba45ceb03f | 177 | if(sliderv < 0.1) { |
ndaniel7 | 2:13ba45ceb03f | 178 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 179 | ship.coord.y += 5; |
ndaniel7 | 2:13ba45ceb03f | 180 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 181 | } |
ndaniel7 | 2:13ba45ceb03f | 182 | else if (sliderv < .2){ |
ndaniel7 | 2:13ba45ceb03f | 183 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 184 | ship.coord.y += 4; |
ndaniel7 | 2:13ba45ceb03f | 185 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 186 | } |
ndaniel7 | 2:13ba45ceb03f | 187 | else if (sliderv < .3){ |
ndaniel7 | 2:13ba45ceb03f | 188 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 189 | ship.coord.y += 3; |
ndaniel7 | 2:13ba45ceb03f | 190 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 191 | } |
ndaniel7 | 2:13ba45ceb03f | 192 | else if (sliderv < .4){ |
ndaniel7 | 2:13ba45ceb03f | 193 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 194 | ship.coord.y += 2; |
ndaniel7 | 2:13ba45ceb03f | 195 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 196 | } |
ndaniel7 | 2:13ba45ceb03f | 197 | else if(sliderv> 0.9) { |
ndaniel7 | 2:13ba45ceb03f | 198 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 199 | ship.coord.y -= 5; |
ndaniel7 | 2:13ba45ceb03f | 200 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 201 | } |
ndaniel7 | 2:13ba45ceb03f | 202 | else if(sliderv> 0.8) { |
ndaniel7 | 2:13ba45ceb03f | 203 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 204 | ship.coord.y-= 4; |
ndaniel7 | 2:13ba45ceb03f | 205 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 206 | } |
ndaniel7 | 2:13ba45ceb03f | 207 | else if(sliderv> 0.7) { |
ndaniel7 | 2:13ba45ceb03f | 208 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 209 | ship.coord.y-= 3; |
ndaniel7 | 2:13ba45ceb03f | 210 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 211 | } |
ndaniel7 | 2:13ba45ceb03f | 212 | else if(sliderv> 0.6) { |
ndaniel7 | 2:13ba45ceb03f | 213 | ship.old_coord.y = ship.coord.y; |
ndaniel7 | 2:13ba45ceb03f | 214 | ship.coord.y-= 2; |
ndaniel7 | 2:13ba45ceb03f | 215 | ship.moved = true; |
ndaniel7 | 2:13ba45ceb03f | 216 | } |
ndaniel7 | 2:13ba45ceb03f | 217 | if (ship.coord.y < 0){ |
ndaniel7 | 2:13ba45ceb03f | 218 | ship.coord.y = 0; |
ndaniel7 | 2:13ba45ceb03f | 219 | } |
ndaniel7 | 2:13ba45ceb03f | 220 | else if (ship.coord.y > 128-ship.size.y){ |
ndaniel7 | 2:13ba45ceb03f | 221 | ship.coord.y= 128-ship.size.y; |
rquinn7 | 0:bbc2ad180020 | 222 | } |
rquinn7 | 0:bbc2ad180020 | 223 | } |
rquinn7 | 0:bbc2ad180020 | 224 | |
ndaniel7 | 2:13ba45ceb03f | 225 | void create_enemies(int number) { |
rquinn7 | 0:bbc2ad180020 | 226 | for(int i = 0; i<number; i++) { |
ndaniel7 | 2:13ba45ceb03f | 227 | Pair spawn,speed; |
ndaniel7 | 2:13ba45ceb03f | 228 | srand(1*i); |
rquinn7 | 0:bbc2ad180020 | 229 | spawn.x = rand() % 124 + 0; |
rquinn7 | 0:bbc2ad180020 | 230 | spawn.y = 0; |
ndaniel7 | 2:13ba45ceb03f | 231 | srand(2*i); |
ndaniel7 | 2:13ba45ceb03f | 232 | speed.x = rand() % (number) + 1; |
ndaniel7 | 2:13ba45ceb03f | 233 | srand(3*i); |
ndaniel7 | 2:13ba45ceb03f | 234 | if (rand()%3==0){ |
ndaniel7 | 2:13ba45ceb03f | 235 | speed.x=-speed.x; |
ndaniel7 | 2:13ba45ceb03f | 236 | } |
ndaniel7 | 2:13ba45ceb03f | 237 | srand(4*i); |
ndaniel7 | 2:13ba45ceb03f | 238 | speed.y = rand() % (number) + 1; |
ndaniel7 | 2:13ba45ceb03f | 239 | Enemy enemy(spawn, speed); |
ndaniel7 | 2:13ba45ceb03f | 240 | enemies.push_back(enemy); |
rquinn7 | 0:bbc2ad180020 | 241 | } |
rquinn7 | 0:bbc2ad180020 | 242 | } |
rquinn7 | 1:34bb7c386b9f | 243 | |
rquinn7 | 0:bbc2ad180020 | 244 | int main() { |
rquinn7 | 0:bbc2ad180020 | 245 | pb1.mode(PullUp); |
rquinn7 | 0:bbc2ad180020 | 246 | pc.baud(9600); |
rquinn7 | 0:bbc2ad180020 | 247 | lcd.baudrate(3000000); |
rquinn7 | 0:bbc2ad180020 | 248 | lcd.cls(); |
ndaniel7 | 2:13ba45ceb03f | 249 | int numEnemies = 1; |
ndaniel7 | 2:13ba45ceb03f | 250 | lcd.set_font_size(5,5); |
ndaniel7 | 2:13ba45ceb03f | 251 | lcd.locate(5,4); |
ndaniel7 | 2:13ba45ceb03f | 252 | lcd.printf("Welcome"); |
ndaniel7 | 2:13ba45ceb03f | 253 | lcd.locate(5,6); |
ndaniel7 | 2:13ba45ceb03f | 254 | lcd.printf(" to"); |
ndaniel7 | 2:13ba45ceb03f | 255 | lcd.locate(5,8); |
ndaniel7 | 2:13ba45ceb03f | 256 | lcd.printf("Galager"); |
ndaniel7 | 2:13ba45ceb03f | 257 | red=0; |
ndaniel7 | 2:13ba45ceb03f | 258 | green=.5; |
ndaniel7 | 2:13ba45ceb03f | 259 | wait(3); |
ndaniel7 | 2:13ba45ceb03f | 260 | lcd.cls(); |
rquinn7 | 1:34bb7c386b9f | 261 | while(1) { |
ndaniel7 | 4:6fc77e25bbcf | 262 | //Game over |
ndaniel7 | 2:13ba45ceb03f | 263 | if(lives==0) { |
rquinn7 | 0:bbc2ad180020 | 264 | break; |
rquinn7 | 0:bbc2ad180020 | 265 | } |
ndaniel7 | 4:6fc77e25bbcf | 266 | Create enemies based on round number |
ndaniel7 | 2:13ba45ceb03f | 267 | create_enemies(numEnemies); |
ndaniel7 | 2:13ba45ceb03f | 268 | pc.printf("%d",enemies.size()); |
ndaniel7 | 2:13ba45ceb03f | 269 | for (int x = 0; x<100000000; x++) { |
ndaniel7 | 2:13ba45ceb03f | 270 | ship_move(); |
ndaniel7 | 2:13ba45ceb03f | 271 | for(std::list<Enemy>::iterator j = enemies.begin(); j != enemies.end(); j++){ |
ndaniel7 | 2:13ba45ceb03f | 272 | j->move(); |
ndaniel7 | 4:6fc77e25bbcf | 273 | //If enemies get passed the bottom, delete them |
ndaniel7 | 2:13ba45ceb03f | 274 | if (j->coord.y>140){ |
ndaniel7 | 2:13ba45ceb03f | 275 | j = enemies.erase(j); |
ndaniel7 | 2:13ba45ceb03f | 276 | } |
ndaniel7 | 2:13ba45ceb03f | 277 | } |
ndaniel7 | 2:13ba45ceb03f | 278 | |
rquinn7 | 0:bbc2ad180020 | 279 | render(); |
ndaniel7 | 4:6fc77e25bbcf | 280 | //If no enemies, round is over |
ndaniel7 | 2:13ba45ceb03f | 281 | if(enemies.size() == 0) { |
ndaniel7 | 2:13ba45ceb03f | 282 | numEnemies+=1; |
ndaniel7 | 2:13ba45ceb03f | 283 | break; |
ndaniel7 | 2:13ba45ceb03f | 284 | } |
ndaniel7 | 4:6fc77e25bbcf | 285 | //If lose all lives, game over |
ndaniel7 | 2:13ba45ceb03f | 286 | if(lives==0) { |
rquinn7 | 0:bbc2ad180020 | 287 | break; |
rquinn7 | 0:bbc2ad180020 | 288 | } |
ndaniel7 | 2:13ba45ceb03f | 289 | wait(.05); |
ndaniel7 | 2:13ba45ceb03f | 290 | |
ndaniel7 | 2:13ba45ceb03f | 291 | } |
ndaniel7 | 4:6fc77e25bbcf | 292 | //Delete all the stray bullets after a round |
ndaniel7 | 2:13ba45ceb03f | 293 | for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ |
ndaniel7 | 2:13ba45ceb03f | 294 | lcd.filled_rectangle(j->coord.x-4, j->coord.y-4, j->coord.x + j->size.x+4, j->coord.y + j->size.y+4, 0x000000); |
ndaniel7 | 2:13ba45ceb03f | 295 | j = bullets.erase(j); |
rquinn7 | 0:bbc2ad180020 | 296 | } |
ndaniel7 | 2:13ba45ceb03f | 297 | lcd.cls(); |
ndaniel7 | 4:6fc77e25bbcf | 298 | //Time inbetween rounds |
ndaniel7 | 2:13ba45ceb03f | 299 | if (lives>0){ |
ndaniel7 | 2:13ba45ceb03f | 300 | for (int i=0;i<(1.5/.05);i++){ |
ndaniel7 | 2:13ba45ceb03f | 301 | ship_move(); |
ndaniel7 | 2:13ba45ceb03f | 302 | render_ship(); |
ndaniel7 | 2:13ba45ceb03f | 303 | wait(.05); |
ndaniel7 | 2:13ba45ceb03f | 304 | } |
ndaniel7 | 2:13ba45ceb03f | 305 | } |
rquinn7 | 0:bbc2ad180020 | 306 | } |
ndaniel7 | 4:6fc77e25bbcf | 307 | //Game is over |
ndaniel7 | 2:13ba45ceb03f | 308 | lcd.cls(); |
ndaniel7 | 2:13ba45ceb03f | 309 | lcd.color(0xFF0000); |
ndaniel7 | 2:13ba45ceb03f | 310 | lcd.set_font_size(5,5); |
ndaniel7 | 2:13ba45ceb03f | 311 | lcd.locate(5,4); |
ndaniel7 | 2:13ba45ceb03f | 312 | lcd.printf("Game Over"); |
ndaniel7 | 2:13ba45ceb03f | 313 | lcd.locate(2,7); |
ndaniel7 | 2:13ba45ceb03f | 314 | lcd.printf("You made it to"); |
ndaniel7 | 2:13ba45ceb03f | 315 | lcd.locate(5,9); |
ndaniel7 | 3:0b2c776da95d | 316 | lcd.printf(" Round %d!",numEnemies-1); |
rquinn7 | 0:bbc2ad180020 | 317 | } |