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

Dependencies:   microbit

Committer:
ahmeou
Date:
Wed Jul 08 17:48:31 2020 +0000
Revision:
2:8f1130b99681
Parent:
1:25f13b341b11
Child:
3:a21366e3261e
for the random number generator function of MicroBit to work properly the instance of MicroBit need to be initialized like: ubit.init()

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 0:17bdfb0e7069 5
ahmeou 0:17bdfb0e7069 6 MicroBit ubit;
ahmeou 2:8f1130b99681 7 MicroBitSerial serial(USBTX, USBRX);
ahmeou 0:17bdfb0e7069 8
ahmeou 0:17bdfb0e7069 9 int main(){
ahmeou 2:8f1130b99681 10 ubit.init();
ahmeou 0:17bdfb0e7069 11 Screen screen(&ubit);
ahmeou 0:17bdfb0e7069 12
ahmeou 0:17bdfb0e7069 13 Spacecraft spacecraft;
ahmeou 0:17bdfb0e7069 14 Bullet bullet;
ahmeou 0:17bdfb0e7069 15 bullet.setX(spacecraft.getX());
ahmeou 0:17bdfb0e7069 16 bullet.setY(spacecraft.getY());
ahmeou 2:8f1130b99681 17 int rnd = 6;
ahmeou 2:8f1130b99681 18 while(true){
ahmeou 1:25f13b341b11 19
ahmeou 1:25f13b341b11 20 // move spacecraft
ahmeou 2:8f1130b99681 21 rnd = ubit.random(10);
ahmeou 2:8f1130b99681 22 serial.send(rnd);
ahmeou 2:8f1130b99681 23 if(rnd >= 5)
ahmeou 2:8f1130b99681 24 spacecraft.moveRight();
ahmeou 2:8f1130b99681 25 else
ahmeou 2:8f1130b99681 26 spacecraft.moveLeft();
ahmeou 2:8f1130b99681 27
ahmeou 1:25f13b341b11 28
ahmeou 1:25f13b341b11 29 // move bullet
ahmeou 1:25f13b341b11 30 for(int i = 0; i < 2; i++){
ahmeou 1:25f13b341b11 31 if(bullet.getY() == 0)
ahmeou 1:25f13b341b11 32 bullet.setX(spacecraft.getX());
ahmeou 1:25f13b341b11 33 bullet.move();
ahmeou 1:25f13b341b11 34 ubit.sleep(200);
ahmeou 2:8f1130b99681 35
ahmeou 2:8f1130b99681 36 screen.draw(bullet);
ahmeou 2:8f1130b99681 37 screen.draw(spacecraft);
ahmeou 2:8f1130b99681 38 screen.refresh();
ahmeou 1:25f13b341b11 39 }
ahmeou 1:25f13b341b11 40
ahmeou 1:25f13b341b11 41 }
ahmeou 1:25f13b341b11 42
ahmeou 0:17bdfb0e7069 43 release_fiber();
ahmeou 1:25f13b341b11 44 }