Basic game using accelerometer and LCD screen. You can move and dodge asteroids coming from both left and right. There is no scoring at the moment.
Dependencies: C12832 FXOS8700Q mbed-rtos mbed
Diff: Game.cpp
- Revision:
- 2:9e0c826103d7
- Parent:
- 1:c6734b909bf0
--- a/Game.cpp Tue Feb 16 22:25:42 2016 +0000 +++ b/Game.cpp Wed Feb 17 10:14:33 2016 +0000 @@ -1,3 +1,8 @@ +/** + * Basic game using accelerometer. You must avoid Asteroids coming on your ship. + * Won't work because C++'s lists memory management sucks. :( + **/ + #include "Game.h" #if defined _DEBUG @@ -74,7 +79,7 @@ { this->factory(); #if defined _DEBUG - host.printf("Currently %d Asteroid(s).\r\n", this->asteroids.size()); + // host.printf("Currently %d Asteroid(s).\r\n", this->asteroids.size()); #endif this->move(); } @@ -83,12 +88,9 @@ { ++this->delta; #if defined _DEBUG - host.printf("=> [%c] next asteroid in %d/%d cycle(s).\r\n", this->delta >= this->next ? '1' : '0', this->delta, this->next); + // host.printf("=> [%c] next asteroid in %d/%d cycle(s).\r\n", this->delta >= this->next ? '1' : '0', this->delta, this->next); #endif if (this->delta >= this->next && (MAX_ASTEROID_NBS != 0 ? this->asteroids.size() <= MAX_ASTEROID_NBS : true)) { -#if defined _DEBUG - host.printf("\t***** GENERATION !!!! *****\r\n"); -#endif this->generate(); this->next = rand() % GENERATION_CYCLES + 1; this->delta = 0; @@ -97,17 +99,20 @@ void Game::generate() { - this->asteroids.push_back(new Asteroid()); + this->asteroids.push_front(new Asteroid()); } void Game::move() { this->mutex.lock(100); - std::list<Asteroid*>::iterator it; - for (it = this->asteroids.begin(); it != this->asteroids.end(); it++) { + std::list<Asteroid*>::iterator it = this->asteroids.begin(); + while (it != this->asteroids.end()) { if (!(*it)->move(this->player)) { - delete *it; - it = this->asteroids.erase(it); + delete *this->asteroids.end(); + this->asteroids.pop_back(); + break; + } else { + ++it; } } this->mutex.unlock();