AsteroidDefender

Dependencies:   4DGL-uLCD-SE DebounceIn mbed

Files at this revision

API Documentation at this revision

Comitter:
rquinn7
Date:
Tue Oct 20 03:06:14 2015 +0000
Parent:
0:bbc2ad180020
Commit message:
Added 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
--- a/game.h	Tue Oct 20 02:49:21 2015 +0000
+++ b/game.h	Tue Oct 20 03:06:14 2015 +0000
@@ -6,13 +6,12 @@
     double y;  
 };
 
+//Class for the asteroids falling from the sky
 class Missile{
 private:
 
 public:
     bool moved;
-    bool interactive;         //collidable in physics
-    bool render;
     double x_change;
     double y_change;
     vec2 spawn;
@@ -21,20 +20,16 @@
     vec2 old_coord;
     vec2 size;
     int steps;
-    //vec2 velocity;
-    //Color color;
     
     vec2 prev_coord;
-    //vec2 prev_size;
   
-Missile(vec2 giv_spawn, vec2 giv_dest, int steps_in) :  moved(false), interactive(true), render(true)
+Missile(vec2 giv_spawn, vec2 giv_dest, int steps_in) :  moved(false)
 {
 
     spawn = giv_spawn;
     dest = giv_dest;
     coord = giv_spawn;
     old_coord = coord;
-    //color = RED;
     size.x = 6;
     size.y = 6;
     steps = steps_in;
@@ -43,9 +38,8 @@
     
 }
 
+//When called, moves the asteroid towards the bottom
 void move(){ 
-//    float x_change = 20;
-//    float y_change = 20;
     old_coord = coord;
     moved = true;
     coord.x = coord.x + x_change;
@@ -53,18 +47,17 @@
 }
 };
 
+//Handles all the fired projectiles
 class Projectile{
 private:
 
 public:
     bool moved;
-    bool interactive;         //collidable in physics
-    bool render;
     vec2 old_coord;
     vec2 coord;
     vec2 size;
     
-Projectile(vec2 coord_in) : moved(false), interactive(true), render(true)
+Projectile(vec2 coord_in) : moved(false)
 {
     coord.x = coord_in.x + 3;
     coord.y = coord_in.y + 6;
@@ -74,26 +67,23 @@
 }
 
 void move(){ 
-//    float x_change = 20;
-//    float y_change = 20;
     old_coord = coord;
     moved = true;
     coord.y = coord.y - 4;
 }
 };
 
+//Handles the object the user controls
 class Shooter{
 private:
 
 public:
     bool moved;
-    bool interactive;         //collidable in physics
-    bool render;
     vec2 coord;
     vec2 old_coord;
     vec2 size;
     
-Shooter() : moved(false), interactive(true), render(true)
+Shooter() : moved(false)
 {
     coord.x = 58;
     coord.y = 125;
--- 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);
     }
 }