Asteroids for MBED game with Bluetooth LE UART Friend Controller, uLCD, speaker, and uSD.

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Asteroids for MBED game with Bluetooth LE UART Friend Controller

Files at this revision

API Documentation at this revision

Comitter:
agamemaker
Date:
Sat Mar 12 22:23:59 2016 +0000
Parent:
10:6bb7504e0f5a
Commit message:
Complete

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6bb7504e0f5a -r d707cebe6eff main.cpp
--- a/main.cpp	Sat Mar 12 21:11:19 2016 +0000
+++ b/main.cpp	Sat Mar 12 22:23:59 2016 +0000
@@ -3,11 +3,12 @@
 #include "uLCD_4DGL.h"
 #include <math.h>
 #include <list>
-#include <time.h> 
+#include <time.h>
 #include "SongPlayer.h"
 using namespace std;
 
 //constants
+#define GAMES 144
 #define ACCEL .5
 #define ROTTA .3
 #define MAXSP 7
@@ -38,12 +39,12 @@
 {
 public:
     double x_pos;   // x position of object
-    double y_pos;   //
+    double y_pos;   // y position of object
     double x_vel;   // x velocity of object
-    double y_vel;   //
+    double y_vel;   // y velocity of object
     double angle;   // angle of object
-    int hit_rad;     // hit box radius of object
-    bool wrap;
+    int hit_rad;    // hit box radius of object
+    bool wrap;      // does the object wrap
     Movement(double xp,double yp,double xv,double yv,double a,double r,double w);
     void assign(double xp,double yp,double xv,double yv,double a,double r,double w);
     //void move();
@@ -75,19 +76,6 @@
     wrap = w;
 }
 
-/*void Movement::move()
-{
-    x_pos += x_vel;
-    y_pos += y_vel;
-    if(x_pos < 0 || x_pos > 144 || y_pos < 0 || y_pos > 144) {
-        if(wrap) {
-            x_pos += 144*(1 - ceil(x_pos/144));
-            y_pos += 144*(1 - ceil(y_pos/144));
-        } else {
-        }
-    }
-}*/
-
 void Movement::accelerate()
 {
     x_vel += ACCEL * cos(angle);
@@ -128,12 +116,14 @@
     while(!blue.readable()) {
         Thread::yield();
     }
-    return blue.getc();
+    stdio_mutex.lock();
+    int temp = blue.getc();
+    stdio_mutex.unlock();
+    return temp;
 }
 
 void controls(void const *args)
 {
-    stdio_mutex.lock();
     char bnum=0;
     char bhit=0;
     while(1) {
@@ -208,27 +198,20 @@
             }
         }
     }
-    stdio_mutex.unlock();
 }
 
-void move(Movement &p)
-{
-    p.x_pos += p.x_vel;
-    p.y_pos += p.y_vel;
-    if(p.x_pos < 0 || p.x_pos > 144 || p.y_pos < 0 || p.y_pos > 144) {
-        if(p.wrap) {
-            p.x_pos += 144*(1 - ceil(p.x_pos/144));
-            p.y_pos += 144*(1 - ceil(p.y_pos/144));
-        } else {
-        }
-    }
-}
 //game_over
-void game_over()
+void game_over(list<Movement> &asteroids, list<Movement> &bullets)
 {
     stdio_mutex.lock();
     ulcd.cls();
-    ulcd.printf("GAME OVER");
+    asteroids.clear();
+    bullets.clear();
+    round = 0;
+    //ulcd.printf("GAME OVER");
+    ulcd.media_init();
+    ulcd.set_sector_address(0x001D, 0x4C01);
+    ulcd.display_image(0,0);
     wait(3);
     ulcd.cls();
     lives = 3;
@@ -236,7 +219,21 @@
     stdio_mutex.unlock();
 
 }
+
 //move
+void move(Movement &p)
+{
+    p.x_pos += p.x_vel;
+    p.y_pos += p.y_vel;
+    if(p.x_pos < 0 || p.x_pos > GAMES || p.y_pos < 0 || p.y_pos > GAMES) {
+        if(p.wrap) {
+            p.x_pos += GAMES*(1 - ceil(p.x_pos/GAMES));
+            p.y_pos += GAMES*(1 - ceil(p.y_pos/GAMES));
+        } else {
+        }
+    }
+}
+
 void move_all(Movement &player, list<Movement> &asteroids, list<Movement> &bullets)
 {
     move(player);
@@ -256,7 +253,7 @@
     list<Movement>::iterator iterator;
     list<Movement>::iterator itcpy;
     for (iterator = bullets.begin(); iterator != bullets.end(); ++iterator) {
-        if(iterator->x_pos < 0 || iterator->x_pos > 144 || iterator->y_pos < 0 || iterator->y_pos > 144) {
+        if(iterator->x_pos < 0 || iterator->x_pos > GAMES || iterator->y_pos < 0 || iterator->y_pos > GAMES) {
             //bullets.erase(iterator);
             del = 1;
             itcpy = iterator;
@@ -275,7 +272,8 @@
     return dist < rad;
 }
 
-void new_asteroid(list<Movement> &asteroids, int x, int y, int size){
+void new_asteroid(list<Movement> &asteroids, int x, int y, int size)
+{
     double ast_ang = rand()%1000 * 2*PI/1000;
     double a_x_vel = ASTSP * cos(ast_ang);
     double a_y_vel = ASTSP * sin(ast_ang);
@@ -285,7 +283,7 @@
 
 void calc_collisions(Movement &player, list<Movement> &asteroids, list<Movement> &bullets)
 {
-    bool hit = 0;
+    bool crash = 0, hit = 0;
     list<Movement>::iterator it;
     list<Movement>::iterator it2;
     list<Movement>::iterator itacpy;
@@ -294,13 +292,7 @@
         if(collision(player,*it)) {
             //ulcd.printf("crash");
             colsound = 1;
-            player.assign(70,70,0,0,0,10,1);
-            lives--;
-            if(!lives) {
-                game_over();
-            }
-            ulcd.cls();
-            ulcd.printf("%d",lives);
+            crash = 1;
         }
     }
     for (it = asteroids.begin(); it != asteroids.end(); ++it) {
@@ -313,16 +305,29 @@
             }
         }
     }
+
+    if(crash) {
+        player.assign(70,70,0,0,0,10,1);
+        lives--;
+        if(!lives) {
+            game_over(asteroids,bullets);
+        }
+        stdio_mutex.lock();
+        ulcd.cls();
+        ulcd.printf("%d",lives);
+        stdio_mutex.unlock();
+    }
+
     if(hit) {
-        if(itacpy->hit_rad == 10){
+        if(itacpy->hit_rad == 10) {
             new_asteroid(asteroids, itacpy->x_pos, itacpy->y_pos, 7);
             new_asteroid(asteroids, itacpy->x_pos, itacpy->y_pos, 7);
         }
-        if(itacpy->hit_rad == 7){
+        if(itacpy->hit_rad == 7) {
             new_asteroid(asteroids, itacpy->x_pos, itacpy->y_pos, 5);
             new_asteroid(asteroids, itacpy->x_pos, itacpy->y_pos, 5);
         }
-        
+
         asteroids.erase(itacpy);
         bullets.erase(itbcpy);
     }
@@ -355,10 +360,11 @@
 }
 
 //round check
-void round_check(list<Movement> &asteroids){
-    if(asteroids.empty()){
+void round_check(list<Movement> &asteroids)
+{
+    if(asteroids.empty()) {
         round++;
-        for(int i = 0; i < round && i < 4; i++){
+        for(int i = 0; i < round && i < 4; i++) {
             new_asteroid(asteroids, 140*(i%2), 140*((i%2)+1),10);
         }
     }