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

Files at this revision

API Documentation at this revision

Comitter:
co838_gtvl2
Date:
Tue Feb 16 22:25:42 2016 +0000
Parent:
0:9ec880239b3c
Child:
2:9e0c826103d7
Commit message:
Added boost by pressing the joystick on the shield.; Added mutex to have a better stability.; Still a bug when the game stays for too long. :(

Changed in this revision

Asteroid.cpp Show annotated file Show diff for this revision Revisions of this file
Asteroid.h Show annotated file Show diff for this revision Revisions of this file
Game.cpp Show annotated file Show diff for this revision Revisions of this file
Game.h Show annotated file Show diff for this revision Revisions of this file
Player.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
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/Asteroid.cpp	Mon Feb 15 18:18:16 2016 +0000
+++ b/Asteroid.cpp	Tue Feb 16 22:25:42 2016 +0000
@@ -1,8 +1,7 @@
 #include "Asteroid.h"
 
-Asteroid::Asteroid(const int &nb)
+Asteroid::Asteroid()
 {
-    this->number = nb;
     this->a = static_cast<float>(rand()) / (static_cast<float>(RAND_MAX)) - 0.5f;
     this->b = rand() % DISPLAY_Y;
     if (rand() % 2 == 1) {
@@ -12,14 +11,24 @@
         this->x = DISPLAY_X;
         this->inc = -1;
     }
+#if defined _DEBUG
+    host.printf("New Asteroid !\r\n");
+#endif
+}
+
+Asteroid::~Asteroid()
+{
+#if defined _DEBUG
+    host.printf("Deleting Asteroid !\r\n");
+#endif
 }
 
 bool    Asteroid::move(Player *player)
 {
     this->x += this->inc;
-    if ((this->getX() == player->getX() || this->getX() == player->getX() + 1) && 
-        (this->getY() == player->getY() || this->getY() == player->getY() + 1)) {
-        player->die();   
+    if ((this->getX() == player->getX() || this->getX() == player->getX() + 1) &&
+            (this->getY() == player->getY() || this->getY() == player->getY() + 1)) {
+        player->die();
     }
     if (this->inc == 1 && this->x == DISPLAY_X + 1) {
         return false;
@@ -39,9 +48,3 @@
 {
     return static_cast<int>(this->a * this->x + this->b);
 }
-
-int     Asteroid::getNumber() const
-{
-    return this->number;    
-}
-
--- a/Asteroid.h	Mon Feb 15 18:18:16 2016 +0000
+++ b/Asteroid.h	Tue Feb 16 22:25:42 2016 +0000
@@ -7,13 +7,12 @@
 class Asteroid
 {
 public:
-    Asteroid(const int&);
+    Asteroid();
+    ~Asteroid();
     int         getX() const;
     int         getY() const;
-    int         getNumber() const;
     bool        move(Player*);
 private:
-    int         number;
     int         inc;    // -1 / +1
     int         x;      // 127=>0 / 0=>127
     float       a;      // [A] * x + b
--- a/Game.cpp	Mon Feb 15 18:18:16 2016 +0000
+++ b/Game.cpp	Tue Feb 16 22:25:42 2016 +0000
@@ -5,7 +5,9 @@
 #endif
 
 Game::Game()
-    : _thread(&Game::threader, this, osPriorityNormal,1024)
+    :   thread(&Game::threader, this, osPriorityNormal,1024),
+        mutex(),
+        boost(D4)
 {
 #if defined _DEBUG
     host.baud(38400);
@@ -13,22 +15,26 @@
     this->player = new Player();
     this->lcd = new C12832(D11, D13, D12, D7, D10);
     this->lcd->set_auto_up(0);
-
+    this->score = 0;
+    this->next = 0;
+    this->delta = 0;
     this->accel = new FXOS8700Q_acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
     this->accel->enable();
     this->accel->getY(&this->steadyPosition);
     this->asteroids = std::list<Asteroid*>();
-    this->_thread.signal_set(START_THREAD);
+    this->thread.signal_set(START_THREAD);
+    srand(time(NULL));
 }
 
 Game::~Game()
 {
+    this->thread.terminate();
     this->accel->disable();
-    this->_thread.terminate();
+    this->display();
     this->lcd->rect(this->player->getX() - 2, this->player->getY() - 2, this->player->getX() + 3, this->player->getY() + 3, 1);
+    this->lcd->locate(0, 0);
+    this->lcd->printf("GAME OVER\r\nScore: %d\r\n", this->score);
     this->lcd->copy_to_lcd();
-    this->lcd->locate(0, 0);
-    this->lcd->printf("GAME OVER");
     delete this->player;
     delete this->accel;
     delete this->lcd;
@@ -45,52 +51,66 @@
     while (this->player->isAlive()) {
         this->process();
         this->display();
+        ++this->score;
         Thread::wait(DELTA_TIME);
     }
 }
 
 void    Game::watchAccel()
 {
-    this->_thread.signal_wait(START_THREAD);
+    this->thread.signal_wait(START_THREAD);
     while (true) {
         int16_t raX, raY;
         this->accel->getX(&raX);
         this->accel->getY(&raY);
+        this->mutex.lock(100);
         this->player->move(-raX, raY - this->steadyPosition);
-        Thread::wait(DELTA_TIME);
+        this->mutex.unlock();
+        Thread::wait(this->boost ? DELTA_TIME / 2 : DELTA_TIME);
     }
 }
 
 void    Game::process()
 {
-    static int delta = 0;
-    static int next = rand() % 6;
-    if (delta >= next) {
+    this->factory();
+#if defined _DEBUG
+    host.printf("Currently %d Asteroid(s).\r\n", this->asteroids.size());
+#endif
+    this->move();
+}
+
+void    Game::factory()
+{
+    ++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);
+#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();
-        next = rand() % 6;
-        delta = 0;
-    }
-    ++delta;
-    
-    std::list<Asteroid*>::iterator it;
-    
-    for (it = this->asteroids.begin(); it != this->asteroids.end(); it++) {
-        if (!(*it)->move(this->player)) {
-            #if defined _DEBUG
-            host.printf("Erasing Asteroid #%d !\r\n", (*it)->getNumber());
-            #endif 
-            it = this->asteroids.erase(it);
-        }
+        this->next = rand() % GENERATION_CYCLES + 1;
+        this->delta = 0;
     }
 }
 
 void    Game::generate()
 {
-    static int current = 0;
-    this->asteroids.push_back(new Asteroid(++current));
-    #if defined _DEBUG
-        host.printf("New Asteroid #%d !\r\n", current);
-    #endif
+    this->asteroids.push_back(new Asteroid());
+}
+
+void    Game::move()
+{
+    this->mutex.lock(100);
+    std::list<Asteroid*>::iterator it;
+    for (it = this->asteroids.begin(); it != this->asteroids.end(); it++) {
+        if (!(*it)->move(this->player)) {
+            delete *it;
+            it = this->asteroids.erase(it);
+        }
+    }
+    this->mutex.unlock();
 }
 
 void    Game::display()
@@ -98,8 +118,8 @@
     this->lcd->cls();
     std::list<Asteroid*>::iterator it;
     for (it = this->asteroids.begin(); it != this->asteroids.end(); it++) {
-        this->lcd->pixel((*it)->getX(), (*it)->getY(), 1);    
-    } 
+        this->lcd->pixel((*it)->getX(), (*it)->getY(), 1);
+    }
     this->lcd->rect(this->player->getX(), this->player->getY(), this->player->getX() + 1, this->player->getY() + 1, 1);
     this->lcd->copy_to_lcd();
 }
\ No newline at end of file
--- a/Game.h	Mon Feb 15 18:18:16 2016 +0000
+++ b/Game.h	Tue Feb 16 22:25:42 2016 +0000
@@ -18,13 +18,20 @@
     void                    watchAccel();
     void                    display();
     void                    process();
+    void                    move();
+    void                    factory();
     void                    generate();
 private:
     Player                  *player;
-    Thread                  _thread;
+    Thread                  thread;
+    Mutex                   mutex;
     C12832                  *lcd;
     FXOS8700Q_acc           *accel;
+    DigitalIn               boost;
     int16_t                 steadyPosition;
+    unsigned int            score;
+    int                     delta;
+    int                     next;
     std::list<Asteroid*>    asteroids;
 };
 
--- a/Player.cpp	Mon Feb 15 18:18:16 2016 +0000
+++ b/Player.cpp	Tue Feb 16 22:25:42 2016 +0000
@@ -38,7 +38,7 @@
 
 bool    Player::isAlive() const
 {
-    return this->alive;   
+    return this->alive;
 }
 
 int     Player::getX() const
--- a/main.cpp	Mon Feb 15 18:18:16 2016 +0000
+++ b/main.cpp	Tue Feb 16 22:25:42 2016 +0000
@@ -3,6 +3,6 @@
 int main(void)
 {
     Game *g = new Game();
-    g->loop(); 
+    g->loop();
     delete g;
 }
\ No newline at end of file
--- a/main.h	Mon Feb 15 18:18:16 2016 +0000
+++ b/main.h	Tue Feb 16 22:25:42 2016 +0000
@@ -11,8 +11,14 @@
 # define DELTA_MOVE 1000.0f
 # define DELTA_TIME 100
 # define START_THREAD 999
+# define MAX_ASTEROID_NBS 50
+# define GENERATION_CYCLES 5
 
 // # define _DEBUG
 # undef _DEBUG
 
+# if defined _DEBUG
+extern Serial host;
+# endif
+
 #endif /* _MAIN_H_ */
\ No newline at end of file