Laila Al Badwawi 200906179 SpaceInvaders I declare this my own independent work and understand the university rules on plagiarism.

Dependencies:   mbed

Revision:
12:45b1249b3d9a
Parent:
8:eec0d7fc365f
Child:
13:9d6ee753eca6
--- a/main.cpp	Mon Apr 15 12:04:25 2019 +0000
+++ b/main.cpp	Mon Apr 15 14:36:07 2019 +0000
@@ -6,6 +6,7 @@
 #include "SpaceInvadersEngine.h"
 
 
+
 #ifdef WITH_TESTING
 # include "tests.h"
 #endif
@@ -27,28 +28,15 @@
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
 
-//Direction _d;
-//SpaceEngine space;
+Direction _d;
+SpaceInvadersEngine space;
+
 ///////////// prototypes ///////////////
 void init();
 void update_game(UserInput input);
 void render();
 void welcome();
-/*
-const int run[12][10] =   {
-    { 0,0,0,0,0,1,1,1,0,0 },
-    { 0,0,0,0,0,1,1,1,0,0 },
-    { 0,0,0,1,1,1,1,0,0,0 },
-    { 0,0,1,1,1,1,0,1,0,0 },
-    { 0,1,1,0,1,1,0,1,1,1 },
-    { 1,0,0,0,1,1,0,0,0,0 },
-    { 0,0,0,1,1,1,1,0,0,0 },
-    { 0,0,1,1,0,0,1,1,0,0 },
-    { 1,1,1,1,0,0,0,1,1,0 },
-    { 0,0,0,0,0,0,1,1,0,0 },
-    { 0,0,0,0,0,1,1,0,0,0 },
-    { 0,0,0,0,1,1,0,0,0,0 },
-}; */
+
 const int bullet[4][10] =   {
     { 0,0,0,0,0,1,1,1,0,0 },
     { 0,0,0,0,0,1,1,1,0,0 },
@@ -70,21 +58,7 @@
     { 0,0,1,1,1,1,0,1,0,0 },
 
 };
-/* 
-const int space_ship[10][12]={
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-    {0,0,0,0,0,0,0,1,0,0,0,0},
-   
-    };
-*/
+
 
 
 
@@ -101,7 +75,12 @@
 {0,0,0,1,1,0,0,0,0,0,0,0},
 
 };
+
+
+
 Direction _d2;
+
+
 ///////////// functions ////////////////
 int main()
 {
@@ -117,7 +96,7 @@
     init();     // initialise and then display welcome screen...
     welcome();  // waiting for the user to start
     
-    //render();  // first draw the initial frame 
+    render();  // first draw the initial frame 
     wait(1.0f/fps);  // and wait for one frame period
     
     int y=0;
@@ -130,43 +109,46 @@
     int y_alien=10;
     int x_alien=70;
     int bullet_fired=0;
-     int x_space_ship=0;
+    int x_space_ship=0;
     int y_space_ship=40;
     // game loop - read input, update the game state and render the display
     while (1) {
-        lcd.clear();
-        //lcd.drawSprite(0,y,12,10,(int *)run);
-        lcd.drawSprite(0,y,12,10,(int *)space_ship);
+     space.read_input(pad);
+        space.update(pad);
+        render();
+        
+           
+        lcd.drawSprite(x_space_ship,y_space_ship,12,10,(int *)space_ship);
         lcd.drawSprite(x_alien,y_alien,12,10,(int *)alien);
         lcd.drawSprite(x_bullet,y_bullet,4,10,(int *)bullet);
-        //space.read_input(pad);
+        
+        space.read_input(pad);
         _d2 = pad.get_direction();
-        if(_d2==N)
-        //pong.update(pad);
-        //render();
-        y=y-2;
-        else
-        if(_d2==S)
-        y=y+2;
-        else if(_d2==E){
-        bullet_fired=1;
-        x_bullet=11;
-        y_bullet=y;
+        if(_d2==N) {
+
+            y=y-2 ;
+        }
+        else if(_d2==S) {                                                           
+            y=y+2;
         }
-        if(y>=40)
-        y=40;
-        else
-        if(y<=0)
-        y=0;
+        else if(_d2==E){
+            bullet_fired=1;
+            x_bullet=11;
+            y_bullet=y;
+        }
+        if(y>=40){ y=40;}
+        else if(y <= 0){ y = 0;}
         
-        if(bullet_fired==1)
+        if(bullet_fired==1){
             x_bullet+=4;
-            if(x_bullet>=x_alien && y_bullet >=y_alien && y_bullet <=y_alien+10)
+        }
+            if(x_bullet>=x_alien && y_bullet >=y_alien && y_bullet <=y_alien+10){
                 y=0;
-                if (x_bullet == x_alien && y_bullet ==y_alien)
-                score ++;
+            }
+                //if (x_bullet == x_alien && y_bullet ==y_alien)
+               // score ++;
         wait(1.0f/fps);
-        lcd.clear ();
+       
         lcd.refresh();
     }
 }
@@ -178,9 +160,7 @@
     lcd.init();
     pad.init();
      
-    // initialise the game with correct ball and paddle sizes
-    //pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
-
+    space.init(space_ship_width,space_ship_height,bullet_size,alien_size,speed);
 }
 
 // this function draws each frame on the LCD
@@ -188,13 +168,14 @@
 {
     // clear screen, re-draw and refresh
     lcd.clear();  
+    space.draw(lcd);
     lcd.refresh();
 }
 
 // simple splash screen displayed on start-up
 void welcome() {
     
-    lcd.printString("Space Invador!",0,1);  
+    lcd.printString("SpaceInvaders!",0,1);  
     lcd.printString("  Press Start ",0,4);
     
     lcd.refresh();