deemo1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
haoyan
Date:
Tue May 12 15:13:22 2020 +0000
Parent:
2:03cd3bb32511
Child:
4:9fa0c5edd1a1
Commit message:
finish2

Changed in this revision

Boss/Boss.cpp Show annotated file Show diff for this revision Revisions of this file
Boss/Boss.h Show annotated file Show diff for this revision Revisions of this file
StarcraftEngine/StarcraftEngine.cpp Show annotated file Show diff for this revision Revisions of this file
StarcraftEngine/StarcraftEngine.h Show annotated file Show diff for this revision Revisions of this file
mian.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Boss/Boss.cpp	Tue May 12 08:01:52 2020 +0000
+++ b/Boss/Boss.cpp	Tue May 12 15:13:22 2020 +0000
@@ -5,21 +5,19 @@
     
 }
 
-Swarm::~Swarm()
+Boss::~Boss()
 {
     
 }
 
-int Swarmm[6][6] = {
-    {0,0,1,1,0,0},
-    {0,0,1,1,0,0},
+int Bosss[4][6] = {
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,1,1,0,0},
     {0,0,1,1,0,0},
 };
 
-void Swarm::init(int height, int width, int speed)
+void Boss::init(int height, int width, int speed)
 {
      _height = height;
      _width = width;
@@ -32,36 +30,36 @@
      _velocity.y = speed;
 }    
      
-void Swarm::draw(N5110 &lcd)     
+void Boss::draw(N5110 &lcd)     
 {
-     lcd.drawSprite(_x,_y,_height,_width,(int*)Swarmm);  
+     lcd.drawSprite(_x,_y,_height,_width,(int*)Bosss);  
 }
 
-void Swarm::update()
+void Boss::update()
 {
     _x += _velocity.x;
     _y += _velocity.y;
 }   
      
-void Swarm::set_velocity(Vector2D v)
+void Boss::set_velocity(Vector2D v)
 {
     _velocity.x = v.x;
     _velocity.y = v.y;
 }
 
-Vector2D Swarm::get_velocity()
+Vector2D Boss::get_velocity()
 {
     Vector2D v = {_velocity.x,_velocity.y};
     return v;
 }
  
-Vector2D Swarm::get_pos()
+Vector2D Boss::get_pos()
 {
     Vector2D p = {_x,_y};
     return p;
 }
  
-void Swarm::set_pos(Vector2D p)
+void Boss::set_pos(Vector2D p)
 {
     _x = p.x;
     _y = p.y;
@@ -71,3 +69,8 @@
 
 
 
+
+
+
+
+ 
\ No newline at end of file
--- a/Boss/Boss.h	Tue May 12 08:01:52 2020 +0000
+++ b/Boss/Boss.h	Tue May 12 15:13:22 2020 +0000
@@ -0,0 +1,39 @@
+#ifndef BOSS_H
+#define BOSS_H
+ 
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Battleship.h"
+
+/** Boss Class  
+* @brief Boss 
+* @author Haoyan Zhang  
+* @date May, 2020 
+*/ 
+
+class Boss
+{
+public:
+       
+       Boss();
+       ~Boss();
+       void init(int height, int width, int speed);
+       void draw(N5110 &lcd);
+       void update();
+       void set_velocity(Vector2D v);
+       Vector2D get_velocity();
+       Vector2D get_pos();
+       void set_pos(Vector2D p);
+       
+private:
+
+        Vector2D _velocity;
+        int _height;
+        int _width;
+        int _x;
+        int _y;
+};
+#endif
+
+       
\ No newline at end of file
--- a/StarcraftEngine/StarcraftEngine.cpp	Tue May 12 08:01:52 2020 +0000
+++ b/StarcraftEngine/StarcraftEngine.cpp	Tue May 12 15:13:22 2020 +0000
@@ -10,7 +10,7 @@
     
 }
 
-void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width, int speed)
+void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width,int Boss_height, int Boss_width, int speed)
 {
      // initialise the game parameters
      _Battleship_height = Battleship_height;
@@ -19,6 +19,8 @@
      _Laser_width = Laser_width;
      _Swarm_height = Swarm_height;
      _Swarm_width = Swarm_width;
+     _Boss_height = Boss_height;
+     _Boss_width = Boss_width;
      _speed = speed;
      
      // x position on screen - WIDTH is defined in N5110.h
@@ -28,6 +30,7 @@
      _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width);
      _Laser.init(_Laser_height, _Laser_width, _speed);
      _Swarm.init(_Swarm_height, _Swarm_width, _speed);
+     _Boss.init(_Boss_height, _Boss_width, _speed);
      
 }
 
@@ -58,6 +61,8 @@
     _Battleship.draw(lcd);
     // Swarm
     _Swarm.draw(lcd);
+    // Boss
+    _Boss.draw(lcd);
     // Laser
     _Laser.draw(lcd); 
     
@@ -72,9 +77,11 @@
      
      _Battleship.update(_d,_mag);
      _Swarm.update();
+     _Boss.update();
      _Laser.update();
      
      check_Swarm_collisions(pad);
+     check_Boss_collisions(pad);
 }
 
 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad)
@@ -83,7 +90,7 @@
      Vector2D Swarm_pos = _Swarm.get_pos();
      Vector2D Swarm_velocity = _Swarm.get_velocity(); 
 
-     // check if swarm hit or over the battleship
+     // check if swarm over the battleship
      if (
           (Swarm_pos.y >= HEIGHT - 1) 
         )   {                 
@@ -97,14 +104,36 @@
     _Swarm.set_pos(Swarm_pos);
 } 
 
+void StarcraftEngine::check_Boss_collisions(Gamepad &pad)
+{
+     // read current Boss attributes
+     Vector2D Boss_pos = _Boss.get_pos();
+     Vector2D Boss_velocity = _Boss.get_velocity(); 
+
+     // check if Boss over the battleship
+     if (
+          (Boss_pos.y >= HEIGHT - 1) 
+        )   {                 
+         _Battleship.minus_life();
+         pad.tone(800.0,0.1);  //  Audio feedback 
+     }
+     
+     
+     // write new attributes
+    _Boss.set_velocity(Boss_velocity);
+    _Boss.set_pos(Boss_pos);
+} 
+
 void StarcraftEngine::check_goal(Gamepad &pad)    
 {
      Vector2D Swarm_pos = _Swarm.get_pos();
+     Vector2D Boss_pos = _Boss.get_pos();
      Vector2D Battleship_pos = _Battleship.get_pos();  
      Vector2D Laser_pos = _Laser.get_pos();
      
      Vector2D Laser_velocity = _Laser.get_velocity();
      Vector2D Swarm_velocity = _Swarm.get_velocity();
+     Vector2D Boss_velocity = _Boss.get_velocity();
      
      // Player get score
      if ((Laser_pos.x >= Swarm_pos.x)&&
@@ -121,6 +150,21 @@
          pad.tone(800.0,0.25);
          wait(0.1);
      }
+     
+     if ((Laser_pos.x >= Boss_pos.x)&&
+         (Laser_pos.x <= Boss_pos.x + 6)&&
+         (Laser_pos.y <= Boss_pos.y + 4)&&
+         (Laser_pos.y >= Boss_pos.y))
+     {
+         _Battleship.add_score();
+         Laser_pos.x = Battleship_pos.x + 3;    
+         Laser_pos.y = Battleship_pos.y - 1; 
+         Boss_pos.x = rand() % 64;
+         Boss_pos.y = 2;
+         
+         pad.tone(800.0,0.25);
+         wait(0.1);
+     }
       
      if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20))
      {
@@ -130,8 +174,10 @@
      
       _Laser.set_velocity(Laser_velocity);
       _Swarm.set_velocity(Swarm_velocity);
+      _Boss.set_velocity(Boss_velocity);
       _Laser.set_pos(Laser_pos); 
-      _Swarm.set_pos(Swarm_pos);      
+      _Swarm.set_pos(Swarm_pos);    
+      _Boss.set_pos(Boss_pos);  
 }              
        
 void StarcraftEngine::print_scores(N5110 &lcd)
--- a/StarcraftEngine/StarcraftEngine.h	Tue May 12 08:01:52 2020 +0000
+++ b/StarcraftEngine/StarcraftEngine.h	Tue May 12 15:13:22 2020 +0000
@@ -7,6 +7,7 @@
 #include "Battleship.h"
 #include "Laser.h"
 #include "Swarm.h"
+#include "Boss.h"
 
 // gap from edge of screen
 #define GAP 2
@@ -25,7 +26,7 @@
        StarcraftEngine();
        ~StarcraftEngine();
        
-       void init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width, int speed);
+       void init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width,int Boss_height, int Boss_width, int speed);
        void read_input(Gamepad &pad);
        int find_life();
        int find_score();
@@ -34,6 +35,7 @@
        
 private:
         void check_Swarm_collisions(Gamepad &pad);
+        void check_Boss_collisions(Gamepad &pad);
         void check_goal(Gamepad &pad);
         void print_scores(N5110 &lcd);
         
@@ -53,6 +55,10 @@
         int _Swarm_height;
         int _Swarm_width;
         
+        Boss _Boss;
+        int _Boss_height;
+        int _Boss_width;
+        
         Direction _d;
         float _mag;
 };
--- a/mian.cpp	Tue May 12 08:01:52 2020 +0000
+++ b/mian.cpp	Tue May 12 15:13:22 2020 +0000
@@ -21,6 +21,8 @@
 #define SWARM_HEIGHT 6
 #define SWARM_WIDTH 6 
 #define SWARM_SPEED 1
+#define BOSS_HEIGHT 4
+#define BOSS_WIDTH 6
 
 
 
@@ -88,7 +90,7 @@
      pad.init();
      
      // initialise the game with correct ball and paddle sizes
-     Starcraft.init(BATTLESHIP_HEIGHT, BATTLESHIP_WIDTH, LASER_HEIGHT, LASER_WIDTH,SWARM_HEIGHT, SWARM_WIDTH, SWARM_SPEED);
+     Starcraft.init(BATTLESHIP_HEIGHT, BATTLESHIP_WIDTH, LASER_HEIGHT, LASER_WIDTH,SWARM_HEIGHT, SWARM_WIDTH, BOSS_HEIGHT, BOSS_WIDTH, SWARM_SPEED);
 }
 
 // this function draws each frame on the LCD
@@ -174,13 +176,15 @@
 void victory()
 {
     lcd.clear();
-    lcd.printString(" Victory! ",2,2);
+    lcd.printString(" Victory! ",1,1);
+    lcd.printString(" You protect ",1,2);
+    lcd.printString(" the earth! ",1,3);
     lcd.refresh();
     wait(2);
     
     lcd.clear();
-    lcd.printString(" Press back ",1,3);
-    lcd.printString(" to play again ",1,4);
+    lcd.printString(" Press back ",1,1);
+    lcd.printString(" to play again ",1,3);
     lcd.refresh();
      
     while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) {