Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE DebounceIn mbed
Fork of AsteroidDefender by
Revision 4:6fc77e25bbcf, committed 2015-10-28
- Comitter:
- ndaniel7
- Date:
- Wed Oct 28 15:46:00 2015 +0000
- Parent:
- 3:0b2c776da95d
- Commit message:
- Added some additional comments
Changed in this revision
game.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0b2c776da95d -r 6fc77e25bbcf game.h --- a/game.h Wed Oct 21 17:00:49 2015 +0000 +++ b/game.h Wed Oct 28 15:46:00 2015 +0000 @@ -1,6 +1,7 @@ #include <vector> #include "mbed.h" +//used for x,y coordinates struct Pair { double x; double y; @@ -16,10 +17,10 @@ Pair coord; Pair old_coord; Pair size; - + +//Enemies take in a spawn locatio (x,y) and a speed(x,y) Enemy(Pair tSpawn, Pair tSpeed) : moved(false) { - spawn = tSpawn; speed = tSpeed; coord = tSpawn; @@ -28,6 +29,8 @@ size.y = 7; } +//Enemies move by updating their location based on their speed +//if they hit a left/right wall their x speed is reversed void move(){ old_coord = coord; moved = true; @@ -44,7 +47,7 @@ } }; - +//What the ship shoots class Bullet{ private: @@ -61,7 +64,7 @@ size.x = 3; size.y = 3; } - +//Can only move up void move(){ old_coord = coord; moved = true; @@ -69,7 +72,7 @@ } }; - +//What you control class Ship{ private: public: @@ -77,7 +80,7 @@ Pair coord; Pair old_coord; Pair size; - +//Always starts in the center of the screen at the bottom Ship() : moved(false) { size.x = 10;
diff -r 0b2c776da95d -r 6fc77e25bbcf main.cpp --- a/main.cpp Wed Oct 21 17:00:49 2015 +0000 +++ b/main.cpp Wed Oct 28 15:46:00 2015 +0000 @@ -7,21 +7,22 @@ uLCD_4DGL lcd(p28,p27,p29); DigitalOut myled(LED1); -Serial pc(USBTX, USBRX); AnalogIn sliderv(p17); AnalogIn sliderh(p19); PwmOut red(p24); PwmOut green(p25); DigitalIn pb1(p21); +//if the pushbutton is down bool down = false; +//lives remaining int lives = 3; std::list<Enemy> enemies; std::list<Bullet> bullets; Ship ship; -int counter = 0; +//If the enemies have moved, cover their old spot with a black rectangle and redraw them void render_enemies() { for(std::list<Enemy>::iterator j = enemies.begin(); j != enemies.end(); j++){ if(j->moved) { @@ -33,7 +34,7 @@ } } } - +//Set the led color void loseLife(){ lives--; if (lives==2){ @@ -49,6 +50,7 @@ } } +//If the ship has moved, cover their old spot with a black rectangle and redraw them void render_ship() { if(ship.moved) { 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); @@ -57,6 +59,7 @@ lcd.filled_rectangle(ship.coord.x, ship.coord.y, ship.coord.x + ship.size.x, ship.coord.y + ship.size.y, 0x00FF00); } +//If the bullets have moved, cover their old spot with a black rectangle and redraw them void render_bullets() { for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ if(j->moved) { @@ -72,7 +75,7 @@ } } -//Collision checking +//Collision checking based on coordinates void check_collisions() { for(std::list<Enemy>::iterator k = enemies.begin(); k != enemies.end(); k++){ for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ @@ -114,15 +117,16 @@ } } +//Calls render methods for all different objects void render() { render_enemies(); render_ship(); check_button(); render_bullets(); check_collisions(); - counter++; } +//Analog movement for the ship based on the joystick void ship_move() { if(sliderh < 0.1) { ship.old_coord.x = ship.coord.x; @@ -255,36 +259,43 @@ wait(3); lcd.cls(); while(1) { + //Game over if(lives==0) { break; } + Create enemies based on round number create_enemies(numEnemies); pc.printf("%d",enemies.size()); for (int x = 0; x<100000000; x++) { ship_move(); for(std::list<Enemy>::iterator j = enemies.begin(); j != enemies.end(); j++){ j->move(); + //If enemies get passed the bottom, delete them if (j->coord.y>140){ j = enemies.erase(j); } } render(); + //If no enemies, round is over if(enemies.size() == 0) { numEnemies+=1; break; } + //If lose all lives, game over if(lives==0) { break; } wait(.05); } + //Delete all the stray bullets after a round for(std::list<Bullet>::iterator j = bullets.begin(); j != bullets.end(); j++){ 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); j = bullets.erase(j); } lcd.cls(); + //Time inbetween rounds if (lives>0){ for (int i=0;i<(1.5/.05);i++){ ship_move(); @@ -293,6 +304,7 @@ } } } + //Game is over lcd.cls(); lcd.color(0xFF0000); lcd.set_font_size(5,5);