I am learning OOP using c++ on a MicroBit by developing this simple game

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
ahmeou
Date:
Thu Jul 09 06:04:17 2020 +0000
Parent:
2:8f1130b99681
Commit message:

Changed in this revision

gameEngine/inc/Asteroid.h Show annotated file Show diff for this revision Revisions of this file
gameEngine/inc/Sprite.h Show annotated file Show diff for this revision Revisions of this file
gameEngine/module.json Show annotated file Show diff for this revision Revisions of this file
gameEngine/source/Spacecraft.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameEngine/inc/Asteroid.h	Thu Jul 09 06:04:17 2020 +0000
@@ -0,0 +1,8 @@
+#include "Sprite.h"
+
+class Asteroid: public Sprite{
+    public:
+    void move(){
+        m_y = (m_y + 1) % 5;
+    }
+};
\ No newline at end of file
--- a/gameEngine/inc/Sprite.h	Wed Jul 08 17:48:31 2020 +0000
+++ b/gameEngine/inc/Sprite.h	Thu Jul 09 06:04:17 2020 +0000
@@ -2,7 +2,7 @@
 #define SPRITE_H
 
 class Sprite{
-    private:
+    protected:
     int m_x;
     int m_y;
     
--- a/gameEngine/module.json	Wed Jul 08 17:48:31 2020 +0000
+++ b/gameEngine/module.json	Thu Jul 09 06:04:17 2020 +0000
@@ -1,5 +1,5 @@
 {
-  "name": "game engine",
+  "name": "game-engine",
   "version": "2.0.0",
   "description": "A simple game engine for the MicroBit",
   "license": "MIT",
--- a/gameEngine/source/Spacecraft.cpp	Wed Jul 08 17:48:31 2020 +0000
+++ b/gameEngine/source/Spacecraft.cpp	Thu Jul 09 06:04:17 2020 +0000
@@ -10,9 +10,7 @@
     setX(x);
 }
 void Spacecraft::moveRight(){
-    int x = getX() + 1;
-    if(x > 4) x = 0;
-    setX(x);
+    m_x = (m_x + 1) % 5;
 }
 bool Spacecraft::isBlinking(){
     return blinking;
--- a/main.cpp	Wed Jul 08 17:48:31 2020 +0000
+++ b/main.cpp	Thu Jul 09 06:04:17 2020 +0000
@@ -2,6 +2,7 @@
 #include "Screen.h"
 #include "Bullet.h"
 #include "Spacecraft.h"
+#include "Asteroid.h"
 
 MicroBit ubit;
 MicroBitSerial serial(USBTX, USBRX);
@@ -12,17 +13,25 @@
     
     Spacecraft spacecraft;
     Bullet bullet;
+    Asteroid asteroid;
+
     bullet.setX(spacecraft.getX());
     bullet.setY(spacecraft.getY());
+    
     int rnd = 6;
+    rnd = ubit.random(5);
+    
+    asteroid.setX(rnd);
+    asteroid.setY(0);    
+    
+    int dist;
     while(true){
         
         // move spacecraft
-        rnd = ubit.random(10);
-        serial.send(rnd);
-        if(rnd >= 5)
+        rnd = ubit.random(9);
+        if(rnd > 5)
             spacecraft.moveRight();
-        else
+        else if(rnd < 3) 
             spacecraft.moveLeft();
         
         
@@ -31,10 +40,38 @@
             if(bullet.getY() == 0)
                 bullet.setX(spacecraft.getX());
             bullet.move();
+            
+            if(asteroid.hitBy(bullet)){
+                screen.draw(bullet);
+                screen.draw(spacecraft);
+                screen.draw(asteroid);
+                screen.refresh();
+                release_fiber();
+            }
+            
+            if(asteroid.getY() == 4){
+                rnd = ubit.random(5);
+                asteroid.setX(rnd);
+                serial.send(asteroid.getX());
+                serial.send(',');
+                serial.send(rnd);
+                serial.send("\n\r");
+            }
+            asteroid.move();
+            
+            if(asteroid.hitBy(bullet)){
+                screen.draw(bullet);
+                screen.draw(spacecraft);
+                screen.draw(asteroid);
+                screen.refresh();
+                release_fiber();
+            }
+            
             ubit.sleep(200);
             
             screen.draw(bullet);
             screen.draw(spacecraft);
+            screen.draw(asteroid);
             screen.refresh();
         }