A simplified version of Galaga that can be run on an mbed
Dependencies: 4DGL-uLCD-SE DebounceIn mbed
Fork of AsteroidDefender by
Diff: main.cpp
- Revision:
- 1:34bb7c386b9f
- Parent:
- 0:bbc2ad180020
- Child:
- 2:13ba45ceb03f
diff -r bbc2ad180020 -r 34bb7c386b9f main.cpp --- a/main.cpp Tue Oct 20 02:49:21 2015 +0000 +++ b/main.cpp Tue Oct 20 03:06:14 2015 +0000 @@ -14,6 +14,8 @@ bool down = false; bool lost = false; int lives = 2; +//Create lists for missiles and projectiles, so that we can easily remove them when +//destroyed std::list<Missile> missiles; std::list<Projectile> projectiles; Shooter shooter; @@ -21,6 +23,8 @@ int speed = 128; +//Render all the missiles, and erase them when they fall off the screen +//Also write black to where the square used to be void render_missiles() { for(std::list<Missile>::iterator j = missiles.begin(); j != missiles.end(); j++){ if(j->moved) { @@ -39,6 +43,7 @@ } } +//Collision checking void check_collisions() { for(std::list<Projectile>::iterator j = projectiles.begin(); j != projectiles.end(); j++){ for(std::list<Missile>::iterator k = missiles.begin(); k != missiles.end(); k++){ @@ -82,6 +87,7 @@ } } +//If button is pressed, shoot only once void check_button() { if(pb1 == 0 && !down) { down = true; @@ -97,6 +103,10 @@ down = false; } } + +//Calls all the necessary render functions, +//but only calls render_missiles every 4 +//for speed purposes void render() { if(counter >= 4) { render_missiles(); @@ -109,6 +119,7 @@ counter++; } +//Handles moving the shooter void shooter_move() { if(sliderh < 0.4) { shooter.old_coord = shooter.coord; @@ -124,14 +135,11 @@ shooter.moved = true; } } -// if(sliderv < 0.4) { -// shooter.coord.x -= 0.2; -// } -// if(sliderv > 0.6) { -// shooter.coord.x += 0.2; -// } } +//Create rocks at the beggining of every level, +//randomly picking top and bottom locations for +//spawn and destination void create_rocks(int number, int speed) { for(int i = 0; i<number; i++) { vec2 spawn, dest; @@ -144,6 +152,7 @@ missiles.push_back(missile); } } + int main() { pb1.mode(PullUp); pc.baud(9600); @@ -151,36 +160,14 @@ lcd.cls(); int counter2 = 0; int rock_number = 5; - //lcd.printf("hello"); - //lcd.filled_rectangle(0, 0, 128, 64.5, 0xFF0000); - while(1) { - + + while(1) { if(lost) { lcd.filled_rectangle(0, 0, 128, 128, 0xFF0000); break; } - -// vec2 spawn, dest; -// spawn.x = rand() % 124 + 0; -// spawn.y = 0; -// dest.x = rand() % 124 + 0; -// dest.y = 129; -// int steps = 128; -// Missile yolo(spawn, dest, steps); -// missiles.push_back(yolo); -// spawn.x = rand() % 124 + 0; -// spawn.y = 0; -// dest.x = 0; -// dest.y = 129; -// Missile yolo2(spawn, dest, steps); -// missiles.push_back(yolo2); - - //CHANGE TO INCREASE OVER TIME create_rocks(rock_number, speed); - //render(); -// wait(.5); - //lcd.cls(); - //yolo.move(); + for (int x = 0; x<128000000; x++) { render(); shooter_move(); @@ -194,9 +181,6 @@ } wait(.041); } - //lcd.cls(); - //yolo.move(); -// render(); wait(2.5); } }