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:
Wed Feb 17 10:14:33 2016 +0000
Parent:
1:c6734b909bf0
Commit message:
Unrealisable because memory in cpp isn't that good :(

Changed in this revision

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
--- a/Asteroid.h	Tue Feb 16 22:25:42 2016 +0000
+++ b/Asteroid.h	Wed Feb 17 10:14:33 2016 +0000
@@ -9,14 +9,14 @@
 public:
     Asteroid();
     ~Asteroid();
-    int         getX() const;
-    int         getY() const;
-    bool        move(Player*);
+    int                 getX() const;
+    int                 getY() const;
+    bool                move(Player*);
 private:
-    int         inc;    // -1 / +1
-    int         x;      // 127=>0 / 0=>127
-    float       a;      // [A] * x + b
-    int         b;      // a * x + [B]
+    char                inc;    // -1 / +1
+    unsigned short int  x;      // 127 => 0 / 0 => 127
+    float               a;      // [A] * x + b
+    unsigned short int  b;      // a * x + [B]
 };
 
 #endif /* _ASTEROID_H_ */
\ No newline at end of file
--- 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();
--- a/Game.h	Tue Feb 16 22:25:42 2016 +0000
+++ b/Game.h	Wed Feb 17 10:14:33 2016 +0000
@@ -30,8 +30,8 @@
     DigitalIn               boost;
     int16_t                 steadyPosition;
     unsigned int            score;
-    int                     delta;
-    int                     next;
+    unsigned short int      delta;
+    unsigned short int      next;
     std::list<Asteroid*>    asteroids;
 };