Skip to content
Snippets Groups Projects
Verified Commit 1cb0ff4f authored by ja3-saxby's avatar ja3-saxby
Browse files

Back to our safe initial starting point

parent 099f7063
Branches
Tags
No related merge requests found
......@@ -28,125 +28,12 @@
MicroBit UBIT;
/**
* @brief Main class responsible for the game loop and logic
*/
class Game {
public:
/**
* @brief Default Constructor
*/
Game();
/**
* @brief Starts a new run of the game
* @note method blocks until game over.
*/
void run();
/* begin event handler methods */
/**
* Handles Button A Press event
*/
void press_button_a(MicroBitEvent);
/**
* Handles Button B Press event
*/
void press_button_b(MicroBitEvent);
/* end event handler methods */
private:
bool on; // whether flasher should be on or off
/**
* @brief Draws whatever's in the screen buffer to display
*/
void draw();
// internal screen-buffer, which we draw to first before pasting to display
MicroBitImage screen_buffer;
};
Game::Game()
: screen_buffer(5, 5) // our screen buffer mirrors the display dimensions
{}
void Game::press_button_a(MicroBitEvent e) {
// XXX: debug
this->on = true;
}
void Game::press_button_b(MicroBitEvent e) {
// XXX: debug
this->on = false;
}
void Game::run() {
// UBIT.display.scroll("NEW GAME!");
// init screen display mode, to be sure it is in a known-state
// TODO: consider changing to greyscale to allow "mutli-coloured" blocks
UBIT.display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);
// register event-handlers
UBIT.messageBus.listen(
MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_CLICK,
this,
&Game::press_button_a
);
UBIT.messageBus.listen(
MICROBIT_ID_BUTTON_B,
MICROBIT_BUTTON_EVT_CLICK,
this,
&Game::press_button_b
);
while (true) { // TODO: change to use game-over condition
// sleep to conserve CPU cycles and allow other fibers a chance to run
UBIT.sleep(10); // 10ms sleep, or 100Hz tick rate
}
// TODO: add the rest of the game logic
UBIT.display.scroll("GAME OVER!");
// TODO: display final score
// TODO: de-register event-handlers
UBIT.messageBus.ignore(
MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_CLICK,
this,
&Game::press_button_a
);
UBIT.messageBus.ignore(
MICROBIT_ID_BUTTON_B,
MICROBIT_BUTTON_EVT_CLICK,
this,
&Game::press_button_b
);
}
void Game::draw() {
// XXX: debug drawing, fill the screen buffer with black
this->screen_buffer.clear();
if (this->on) {
this->screen_buffer.setPixelValue(2, 2, 255);
}
// render our screen buffer
UBIT.display.print(this->screen_buffer);
}
int main() {
// Initialise the micro:bit runtime.
UBIT.init();
UBIT.display.scroll("BLOCKS!");
Game game;
create_fiber(game.draw);
while (true) { // indefinitely start new games
game.run();
}
// TODO: potentially remove this call, if clarified that it is not required.
// If main exits, there may still be other fibers running or registered event handlers etc.
// Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment