ELEC2645 (2018/19) / Mbed 2 deprecated fy14lkaa

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
fy14lkaa
Date:
Fri Apr 19 17:33:57 2019 +0000
Parent:
12:45b1249b3d9a
Child:
14:6c4bce33c1a1
Commit message:
added a function to draw the alien (lcd.drawRect) by using N5110 and lcd libraries .

Changed in this revision

SpaceInvadersEngine/SpaceInvadersEngine.cpp Show annotated file Show diff for this revision Revisions of this file
SpaceInvadersEngine/SpaceInvadersEngine.h Show annotated file Show diff for this revision Revisions of this file
alien/alien.cpp 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/SpaceInvadersEngine/SpaceInvadersEngine.cpp	Mon Apr 15 14:36:07 2019 +0000
+++ b/SpaceInvadersEngine/SpaceInvadersEngine.cpp	Fri Apr 19 17:33:57 2019 +0000
@@ -7,44 +7,33 @@
 
 SpaceInvadersEngine::~SpaceInvadersEngine()
 {
-
+    
+    
 }
 
 
-    Direction _d;
-    //_mag;
 
 
-    void SpaceInvadersEngine::init(int space_ship_width,int space_ship_height,int bullet_size, int alien_size, int speed)
+void SpaceInvadersEngine::init(int space_ship_width,int space_ship_height,int bullet_size, int alien_size, int speed)
     {
      // initialise the game parameters
     
-    int _space_ship_width= space_ship_width;
-    int _space_ship_height= space_ship_height;
-    int _bullet_size= bullet_size;
-    int _alien_size= alien_size;
-    int _speed= speed;
-    
+    _space_ship_width= space_ship_width;
+    _space_ship_height= space_ship_height;
+    _bullet_size= bullet_size;
+    _alien_size= alien_size;
+    _speed= speed;
     
-   /* // x position on screen - WIDTH is defined in N5110.h
-    _p1x = GAP;
-    _p2x = WIDTH - GAP - _paddle_width;
-
-    // puts paddles and ball in middle
-    _p1.init(_p1x,_paddle_height,_paddle_width);
-    _p2.init(_p2x,_paddle_height,_paddle_width);
-    _ball.init(_ball_size,_speed);
-    
-    */
+ 
 }
 
 void  SpaceInvadersEngine::read_input(Gamepad &pad)
 {
     _d = pad.get_direction();
-    //_mag = pad.get_mag();
+    _mag = pad.get_mag();
 }
 
-void  SpaceInvadersEngine::draw(N5110 &lcd)
+/*void  SpaceInvadersEngine::draw(N5110 &lcd)
 {
     // draw the elements in the LCD buffer
     // pitch
@@ -52,6 +41,7 @@
     lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
     //score
     print_scores(lcd);
+    
   
    
-}
+}*/
--- a/SpaceInvadersEngine/SpaceInvadersEngine.h	Mon Apr 15 14:36:07 2019 +0000
+++ b/SpaceInvadersEngine/SpaceInvadersEngine.h	Fri Apr 19 17:33:57 2019 +0000
@@ -9,11 +9,7 @@
 
 // gap from edge of screen
 #define GAP 2
-extern int space_ship_width;
-extern int space_ship_height;
-extern int bullet_size;
-extern int alien_size;
-extern int speed;
+
 
 class SpaceInvadersEngine
 {
@@ -22,7 +18,7 @@
    SpaceInvadersEngine();
     ~SpaceInvadersEngine();
 
-    void init(int space_ship_width ,int space_ship_height,int bullet_size, int alien_size, int speed);
+    void init(int space_ship_width,int space_ship_height,int bullet_size, int alien_size,int speed);
     void read_input(Gamepad &pad);
     void update(Gamepad &pad);
     void draw(N5110 &lcd);
@@ -31,13 +27,13 @@
     
     private:
 
+
     void check_wall_collision(Gamepad &pad);
     void space_ship_collisions(Gamepad &pad);
     void check_goal(Gamepad &pad);
     void print_scores(N5110 &lcd);
     
-    //Paddle _p1;
-    //Paddle _p2;
+    
      
     int _space_ship_width;
     int _space_ship_height;
@@ -45,17 +41,10 @@
     int _alien_size;
     int _speed;
     
-    
-    
-    /*   // x positions of the paddles
-    int _p1x;
-    int _p2x;
-    
-    Ball _ball;
-    
     Direction _d;
     float _mag;
-    */
+    
+
 
 };
 
--- a/alien/alien.cpp	Mon Apr 15 14:36:07 2019 +0000
+++ b/alien/alien.cpp	Fri Apr 19 17:33:57 2019 +0000
@@ -17,10 +17,7 @@
     _size = size;
     
     
-      /*
-       _x = WIDTH/2 -  _size/2;
-    _y = HEIGHT/2 - _size/2;
-    */
+   
     
     
     srand(time(NULL));
@@ -43,5 +40,9 @@
 }
    
    
+   void Ball::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+}
+ 
     
-    
--- a/main.cpp	Mon Apr 15 14:36:07 2019 +0000
+++ b/main.cpp	Fri Apr 19 17:33:57 2019 +0000
@@ -1,4 +1,3 @@
-
 ///////// pre-processor directives ////////
 #include "mbed.h"
 #include "Gamepad.h"
@@ -6,43 +5,44 @@
 #include "SpaceInvadersEngine.h"
 
 
-
-#ifdef WITH_TESTING
-# include "tests.h"
-#endif
-
-//#define PADDLE_WIDTH 2
-//#define PADDLE_HEIGHT 8
-//#define BALL_SIZE 2
-//#define BALL_SPEED 3
-
 // structs //
 struct UserInput {
     Direction d;
     float mag;
 };
 
+Direction _d2;
 
 
-/////////////// objects ///////////////
+int space_ship_width;
+int space_ship_height;
+int bullet_size;
+int alien_size;
+int speed;
+
+
+////////////// objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
 
-Direction _d;
 SpaceInvadersEngine space;
 
+
 ///////////// prototypes ///////////////
 void init();
 void update_game(UserInput input);
 void render();
 void welcome();
 
+
 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 },
     { 0,0,0,1,1,1,1,0,0,0 },
     { 0,0,1,1,1,1,0,1,0,0 },
 };
+
+
 const int alien[12][10] =   {
     { 0,0,0,0,0,1,1,1,0,0 },
     { 0,0,0,0,0,1,1,1,0,0 },
@@ -62,35 +62,25 @@
 
 
 
-const int space_ship[10][12]={
-{0,0,0,0,0,0,0,0,0,0,0,0},
-{0,0,0,1,1,0,0,0,0,0,0,0},
-{0,0,0,1,1,0,0,0,0,0,0,0},
-{0,1,1,1,1,1,1,1,0,0,0,0},
-{0,1,1,1,1,1,1,1,1,0,0,0},
-{0,1,1,1,1,1,1,1,1,1,1,0},
-{0,1,1,1,1,1,1,1,1,0,0,0},
-{0,1,1,1,1,1,1,1,0,0,0,0},
-{0,0,0,1,1,0,0,0,0,0,0,0},
-{0,0,0,1,1,0,0,0,0,0,0,0},
+  const int space_ship[10][12]={
+  {0,0,0,0,0,0,0,0,0,0,0,0},
+  {0,0,0,1,1,0,0,0,0,0,0,0},
+  {0,0,0,1,1,0,0,0,0,0,0,0},
+  {0,1,1,1,1,1,1,1,0,0,0,0},
+  {0,1,1,1,1,1,1,1,1,0,0,0},
+  {0,1,1,1,1,1,1,1,1,1,1,0},
+  {0,1,1,1,1,1,1,1,1,0,0,0},
+  {0,1,1,1,1,1,1,1,0,0,0,0},
+  {0,0,0,1,1,0,0,0,0,0,0,0},
+  {0,0,0,1,1,0,0,0,0,0,0,0},
 
 };
 
 
-
-Direction _d2;
+///////////// functions ////////////////
+int main(){
 
 
-///////////// functions ////////////////
-int main()
-{
-    /*
-#ifdef WITH_TESTING
-    int number_of_failures = run_all_tests();
-
-    if(number_of_failures > 0) return number_of_failures;
-#endif
-*/
     int fps = 8;  // frames per second
     
     init();     // initialise and then display welcome screen...
@@ -111,61 +101,68 @@
     int bullet_fired=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) {
      space.read_input(pad);
         space.update(pad);
-        render();
+       render();
+        _d2 = pad.get_direction();
+           
+        lcd.clear();
         
-           
-        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);
-        _d2 = pad.get_direction();
-        if(_d2==N) {
-
-            y=y-2 ;
-        }
-        else if(_d2==S) {                                                           
-            y=y+2;
-        }
-        else if(_d2==E){
+        lcd.drawSprite(x_space_ship,y_space_ship,10,12,(int *) space_ship);
+         
+        if (_d2==N){
+             y= y-2;
+             }
+             
+             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 (y>=40){
+                  y=40;  
+            }
+            
+            else if(y<=0){
+                  y =0;   
+            }
+            
+            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 ++;
-        wait(1.0f/fps);
+            
+             wait(1.0f/fps);
        
         lcd.refresh();
-    }
-}
-
-// initialies all classes and libraries
-void init()
-{
+        
+            
+      }
+  } 
+  
+  
+  // initialies all classes and libraries
+void init() {
     // need to initialise LCD and Gamepad 
     lcd.init();
-    pad.init();
-     
-    space.init(space_ship_width,space_ship_height,bullet_size,alien_size,speed);
+    pad.init(); 
+ space.init(space_ship_width,space_ship_height,bullet_size,alien_size,speed);
+    
 }
-
-// this function draws each frame on the LCD
-void render()
-{
+  
+  // this function draws each frame on the LCD
+void render(){
     // clear screen, re-draw and refresh
     lcd.clear();  
     space.draw(lcd);
@@ -176,8 +173,7 @@
 void welcome() {
     
     lcd.printString("SpaceInvaders!",0,1);  
-    lcd.printString("  Press Start ",0,4);
-    
+    lcd.printString("Press Start ",0,4);
     lcd.refresh();
      
     // wait flashing LEDs until start button is pressed 
@@ -188,4 +184,7 @@
         wait(0.1);
     }
  
+ 
+ 
+ 
 }
\ No newline at end of file