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

Dependencies:   microbit

Committer:
ahmeou
Date:
Thu Jul 09 06:04:17 2020 +0000
Revision:
3:a21366e3261e
Parent:
2:8f1130b99681

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ahmeou 0:17bdfb0e7069 1 #include "MicroBit.h"
ahmeou 0:17bdfb0e7069 2 #include "Screen.h"
ahmeou 1:25f13b341b11 3 #include "Bullet.h"
ahmeou 1:25f13b341b11 4 #include "Spacecraft.h"
ahmeou 3:a21366e3261e 5 #include "Asteroid.h"
ahmeou 0:17bdfb0e7069 6
ahmeou 0:17bdfb0e7069 7 MicroBit ubit;
ahmeou 2:8f1130b99681 8 MicroBitSerial serial(USBTX, USBRX);
ahmeou 0:17bdfb0e7069 9
ahmeou 0:17bdfb0e7069 10 int main(){
ahmeou 2:8f1130b99681 11 ubit.init();
ahmeou 0:17bdfb0e7069 12 Screen screen(&ubit);
ahmeou 0:17bdfb0e7069 13
ahmeou 0:17bdfb0e7069 14 Spacecraft spacecraft;
ahmeou 0:17bdfb0e7069 15 Bullet bullet;
ahmeou 3:a21366e3261e 16 Asteroid asteroid;
ahmeou 3:a21366e3261e 17
ahmeou 0:17bdfb0e7069 18 bullet.setX(spacecraft.getX());
ahmeou 0:17bdfb0e7069 19 bullet.setY(spacecraft.getY());
ahmeou 3:a21366e3261e 20
ahmeou 2:8f1130b99681 21 int rnd = 6;
ahmeou 3:a21366e3261e 22 rnd = ubit.random(5);
ahmeou 3:a21366e3261e 23
ahmeou 3:a21366e3261e 24 asteroid.setX(rnd);
ahmeou 3:a21366e3261e 25 asteroid.setY(0);
ahmeou 3:a21366e3261e 26
ahmeou 3:a21366e3261e 27 int dist;
ahmeou 2:8f1130b99681 28 while(true){
ahmeou 1:25f13b341b11 29
ahmeou 1:25f13b341b11 30 // move spacecraft
ahmeou 3:a21366e3261e 31 rnd = ubit.random(9);
ahmeou 3:a21366e3261e 32 if(rnd > 5)
ahmeou 2:8f1130b99681 33 spacecraft.moveRight();
ahmeou 3:a21366e3261e 34 else if(rnd < 3)
ahmeou 2:8f1130b99681 35 spacecraft.moveLeft();
ahmeou 2:8f1130b99681 36
ahmeou 1:25f13b341b11 37
ahmeou 1:25f13b341b11 38 // move bullet
ahmeou 1:25f13b341b11 39 for(int i = 0; i < 2; i++){
ahmeou 1:25f13b341b11 40 if(bullet.getY() == 0)
ahmeou 1:25f13b341b11 41 bullet.setX(spacecraft.getX());
ahmeou 1:25f13b341b11 42 bullet.move();
ahmeou 3:a21366e3261e 43
ahmeou 3:a21366e3261e 44 if(asteroid.hitBy(bullet)){
ahmeou 3:a21366e3261e 45 screen.draw(bullet);
ahmeou 3:a21366e3261e 46 screen.draw(spacecraft);
ahmeou 3:a21366e3261e 47 screen.draw(asteroid);
ahmeou 3:a21366e3261e 48 screen.refresh();
ahmeou 3:a21366e3261e 49 release_fiber();
ahmeou 3:a21366e3261e 50 }
ahmeou 3:a21366e3261e 51
ahmeou 3:a21366e3261e 52 if(asteroid.getY() == 4){
ahmeou 3:a21366e3261e 53 rnd = ubit.random(5);
ahmeou 3:a21366e3261e 54 asteroid.setX(rnd);
ahmeou 3:a21366e3261e 55 serial.send(asteroid.getX());
ahmeou 3:a21366e3261e 56 serial.send(',');
ahmeou 3:a21366e3261e 57 serial.send(rnd);
ahmeou 3:a21366e3261e 58 serial.send("\n\r");
ahmeou 3:a21366e3261e 59 }
ahmeou 3:a21366e3261e 60 asteroid.move();
ahmeou 3:a21366e3261e 61
ahmeou 3:a21366e3261e 62 if(asteroid.hitBy(bullet)){
ahmeou 3:a21366e3261e 63 screen.draw(bullet);
ahmeou 3:a21366e3261e 64 screen.draw(spacecraft);
ahmeou 3:a21366e3261e 65 screen.draw(asteroid);
ahmeou 3:a21366e3261e 66 screen.refresh();
ahmeou 3:a21366e3261e 67 release_fiber();
ahmeou 3:a21366e3261e 68 }
ahmeou 3:a21366e3261e 69
ahmeou 1:25f13b341b11 70 ubit.sleep(200);
ahmeou 2:8f1130b99681 71
ahmeou 2:8f1130b99681 72 screen.draw(bullet);
ahmeou 2:8f1130b99681 73 screen.draw(spacecraft);
ahmeou 3:a21366e3261e 74 screen.draw(asteroid);
ahmeou 2:8f1130b99681 75 screen.refresh();
ahmeou 1:25f13b341b11 76 }
ahmeou 1:25f13b341b11 77
ahmeou 1:25f13b341b11 78 }
ahmeou 1:25f13b341b11 79
ahmeou 0:17bdfb0e7069 80 release_fiber();
ahmeou 1:25f13b341b11 81 }