Space Invaders - Embedded Systems Project 15/16 - Avinash Patel 200860407

Dependencies:   Joystick N5110 SDFileSystem mbed

Revision:
1:b300d052d549
Parent:
0:427469992efe
Child:
2:d34c95990605
--- a/main.cpp	Sun Mar 13 01:00:23 2016 +0000
+++ b/main.cpp	Sun Mar 13 17:03:33 2016 +0000
@@ -1,3 +1,10 @@
+/*
+Space Invaders - Avinash Patel 200860407
+
+Week 19 - Set up joystick class
+Week 20 - Changed to space invaders as constrained too much by screen resolution
+        - Core cannon is drawn and can move, enemies are visible and they switch between states every second
+*/
 #include "mbed.h"
 #include "N5110.h"
 
@@ -158,10 +165,10 @@
 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
 
 //Ship bit-map and location
-int ship_xpos = 24;
-const int ship_ypos = 43;
-Ticker move_ship;
-const bool ship_bitmap[5][9] = {
+int cannon_xpos = 24;
+const int cannon_ypos = 43;
+Ticker move_cannon;
+const bool cannon_bitmap[5][9] = {
     {0, 0, 0, 0, 1, 0, 0, 0, 0},
     {0, 0, 0, 1, 1, 1, 0, 0, 0},
     {0, 1, 1, 1, 1, 1, 1, 1, 0},
@@ -171,15 +178,18 @@
 
 //Enemies
 Ticker move_enemies;
-
-//Small Invader struct and bitmap
-struct SmallInvader {
+short screen[84][48];
+bool enemies_in_state2 = true;
+//Struct to store enemy data
+struct Enemies {
     int x_pos;
     int y_pos;
     bool is_alive;
-} small_invader[6];
+} small_invader[6], medium_invader[6], large_invader[6];
 int no_of_small_invaders = 6;
-
+int no_of_medium_invaders = 6;
+int no_of_large_invaders = 6;
+//Bitmaps for small invaders
 const bool small_invader_bitmap_1[6][8] = {
     {0, 0, 0, 1, 1, 0, 0, 0},
     {0, 1, 1, 1, 1, 1, 1, 0},
@@ -197,8 +207,45 @@
     {1, 0, 0, 0, 0, 0, 0, 1},
     {0, 1, 0, 0, 0, 0, 1, 0}
 };
-bool small_invader_in_state2 = true;
-short screen[84][48];
+//Bitmaps for medium invaders
+const bool medium_invader_bitmap_1[6][10] = {
+    {1, 0, 0, 1, 0, 0, 1, 0, 0, 1},
+    {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
+    {1, 1, 1, 0, 1, 1, 0, 1, 1, 1},
+    {0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
+    {0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
+    {0, 1, 0, 0, 0, 0, 0, 0, 1, 0}
+};
+
+const bool medium_invader_bitmap_2[6][10] = {
+    {0, 0, 0, 1, 0, 0, 1, 0, 0, 0},
+    {0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
+    {0, 1, 1, 0, 1, 1, 0, 1, 1, 0},
+    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
+    {0, 0, 0, 1, 1, 1, 1, 0, 0, 0}
+};
+//Bitmaps for large invaders
+const bool large_invader_bitmap_1[6][12] = {
+    {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
+    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
+    {1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1},
+    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    {0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0},
+    {0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
+};
+
+const bool large_invader_bitmap_2[6][12] = {
+    {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
+    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
+    {1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1},
+    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    {0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0},
+    {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}
+};
+
+//int row_no = 6;
+//int col_no = 12;
 
 //ISR Flags
 volatile bool g_move_ship_flag = true;
@@ -215,13 +262,15 @@
 void MoveShip();
 void InitSmallInvaders();
 void DrawSmallInvaders();
+void InitMediumInvaders();
+void DrawMediumInvaders();
+void InitLargeInvaders();
+void DrawLargeInvaders();
 //ISR's
 void move_ship_isr();
 void move_enemies_isr();
 
 
-int row_no = 6;
-int col_no = 8;
 
 int main()
 {
@@ -231,14 +280,40 @@
     lcd.init();
     lcd.clear();
 
-    move_ship.attach(&move_ship_isr, 0.1);
+    move_cannon.attach(&move_ship_isr, 0.1);
     move_enemies.attach(&move_enemies_isr, 1);
 
     InitSmallInvaders();
+    InitMediumInvaders();
+    InitLargeInvaders();
+
+    int col = 0;
+    int row = 0;
+
+    for (int i = 0; i < no_of_large_invaders; i++) {
+        if (i == 0) {
+            for (col = 0; col < 12; col++) {
+                for (row = 0; row < 6; row++) {
+                    if(large_invader_bitmap_1[row][col]) {
+                        lcd.setPixel(large_invader[i].x_pos + col, large_invader[i].y_pos + row);
+                    }
+                }
+            }
+        } else {
+            for (col = 0; col < 12; col++) {
+                for (row = 0; row < 6; row++) {
+                    if(large_invader_bitmap_2[row][col]) {
+                        lcd.setPixel(large_invader[i].x_pos + col, large_invader[i].y_pos + row);
+                    }
+                }
+            }
+        }
+    }
 
     lcd.refresh();
 
     while (true) {
+        
         if (g_move_ship_flag) {
             g_move_ship_flag = false;
 
@@ -251,10 +326,14 @@
             g_move_enemies_flag = false;
 
             DrawSmallInvaders();
+            DrawMediumInvaders();
+            DrawLargeInvaders();
+
+            enemies_in_state2 = !enemies_in_state2;
 
             lcd.refresh();
         }
-
+        
         sleep();
     }
 }
@@ -299,30 +378,30 @@
     //Clears the ship
     for (int col = 0; col < 9; col++) {
         for (int row = 0; row < 5; row++) {
-            if(ship_bitmap[row][col]) {
-                lcd.clearPixel(ship_xpos+col, ship_ypos+row);
+            if(cannon_bitmap[row][col]) {
+                lcd.clearPixel(cannon_xpos+col, cannon_ypos+row);
             }
         }
     }
 
     //Changes the position of the ship when the joystick is moved, capping at 0 and 75 so it always fits on the screen
     if (joystick.GetXValue() < 0.25f) {
-        ship_xpos--;
-        if (ship_xpos < 0) {
-            ship_xpos = 0;
+        cannon_xpos--;
+        if (cannon_xpos < 0) {
+            cannon_xpos = 0;
         }
     } else if (joystick.GetXValue() > 0.75f) {
-        ship_xpos++;
-        if (ship_xpos > 75) {
-            ship_xpos = 75;
+        cannon_xpos++;
+        if (cannon_xpos > 75) {
+            cannon_xpos = 75;
         }
     }
 
     //Redraws the ship
     for (int col = 0; col < 9; col++) {
         for (int row = 0; row < 5; row++) {
-            if(ship_bitmap[row][col]) {
-                lcd.setPixel(ship_xpos+col, ship_ypos+row);
+            if(cannon_bitmap[row][col]) {
+                lcd.setPixel(cannon_xpos+col, cannon_ypos+row);
             }
         }
     }
@@ -337,7 +416,7 @@
 void InitSmallInvaders()
 {
     for (int i = 0; i < no_of_small_invaders; i++) {
-        small_invader[i].x_pos = 2+ (i*12);
+        small_invader[i].x_pos = 2+ (i*13);
         small_invader[i].y_pos = 7;
         small_invader[i].is_alive = true;
     }
@@ -356,17 +435,17 @@
             int col = 0;
             int row = 0;
             //Flips the bitmap everytime the function is called
-            if (small_invader_in_state2) {
-                for (col = 0; col < col_no; col++) {
-                    for (row = 0; row < row_no; row++) {
+            if (enemies_in_state2) {
+                for (col = 0; col < 8; col++) {
+                    for (row = 0; row < 6; row++) {
                         if(small_invader_bitmap_1[row][col]) {
                             lcd.setPixel(small_invader[i].x_pos + col, small_invader[i].y_pos + row);
                         }
                     }
                 }
             } else {
-                for (col = 0; col < col_no; col++) {
-                    for (row = 0; row < row_no; row++) {
+                for (col = 0; col < 8; col++) {
+                    for (row = 0; row < 6; row++) {
                         if(small_invader_bitmap_2[row][col]) {
                             lcd.setPixel(small_invader[i].x_pos + col, small_invader[i].y_pos + row);
                         }
@@ -375,6 +454,92 @@
             }
         }
     }
-    //Flips the flag so the bitmap can be switched
-    small_invader_in_state2 = !small_invader_in_state2;
+}
+
+//Sets the position and aliveness of the medium invaders
+void InitMediumInvaders()
+{
+    for (int i = 0; i < no_of_medium_invaders; i++) {
+        medium_invader[i].x_pos = 1 + (i*13);
+        medium_invader[i].y_pos = 15;
+        medium_invader[i].is_alive = true;
+    }
+}
+
+void DrawMediumInvaders()
+{
+    //For each small invader clear and redraws them if they're alive
+    for (int i = 0; i < no_of_medium_invaders; i++) {
+        //Clears the enemy position
+        lcd.drawRect(medium_invader[i].x_pos, medium_invader[i].y_pos, 10, 6, 2);
+
+        //Checks if the invader is alive
+        if (medium_invader[i].is_alive) {
+            //Reads off the bitmap and sets the allowed pixels
+            int col = 0;
+            int row = 0;
+            //Flips the bitmap everytime the function is called
+            if (enemies_in_state2) {
+                for (col = 0; col < 10; col++) {
+                    for (row = 0; row < 6; row++) {
+                        if(medium_invader_bitmap_1[row][col]) {
+                            lcd.setPixel(medium_invader[i].x_pos + col, medium_invader[i].y_pos + row);
+                        }
+                    }
+                }
+            } else {
+                for (col = 0; col < 10; col++) {
+                    for (row = 0; row < 6; row++) {
+                        if(medium_invader_bitmap_2[row][col]) {
+                            lcd.setPixel(medium_invader[i].x_pos + col, medium_invader[i].y_pos + row);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
+//Sets the position and aliveness of the large invaders
+void InitLargeInvaders()
+{
+    for (int i = 0; i < no_of_large_invaders; i++) {
+        large_invader[i].x_pos = 0 + (i*13);
+        large_invader[i].y_pos = 23;
+        large_invader[i].is_alive = true;
+    }
+}
+
+void DrawLargeInvaders()
+{
+    //For each small invader clear and redraws them if they're alive
+    for (int i = 0; i < no_of_large_invaders; i++) {
+        //Clears the enemy position
+        lcd.drawRect(large_invader[i].x_pos, large_invader[i].y_pos, 12, 6, 2);
+
+        //Checks if the invader is alive
+        if (large_invader[i].is_alive) {
+            //Reads off the bitmap and sets the allowed pixels
+            int col = 0;
+            int row = 0;
+            //Flips the bitmap everytime the function is called
+            if (enemies_in_state2) {
+                for (col = 0; col < 12; col++) {
+                    for (row = 0; row < 6; row++) {
+                        if(large_invader_bitmap_1[row][col]) {
+                            lcd.setPixel(large_invader[i].x_pos + col, large_invader[i].y_pos + row);
+                        }
+                    }
+                }
+            } else {
+                for (col = 0; col < 12; col++) {
+                    for (row = 0; row < 6; row++) {
+                        if(large_invader_bitmap_2[row][col]) {
+                            lcd.setPixel(large_invader[i].x_pos + col, large_invader[i].y_pos + row);
+                        }
+                    }
+                }
+            }
+        }
+    }
 }
\ No newline at end of file