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

Revision:
9:49f4b9dcab22
Parent:
7:ff87ab834590
Child:
10:6bb7504e0f5a
--- a/main.cpp	Sat Mar 12 18:51:28 2016 +0000
+++ b/main.cpp	Sat Mar 12 20:57:33 2016 +0000
@@ -3,20 +3,24 @@
 #include "uLCD_4DGL.h"
 #include <math.h>
 #include <list>
+#include <time.h> 
 #include "SongPlayer.h"
 using namespace std;
 
 //constants
 #define ACCEL .5
-#define ROTTA .2
+#define ROTTA .3
 #define MAXSP 7
 #define BULSP 10
+#define ASTSP 3
+#define PI 3.14159265359
 
 //devices
 //DigitalOut myled(LED1);
 uLCD_4DGL ulcd(p28,p27,p29);
 BusOut myled(LED1,LED2,LED3,LED4);
 Serial blue(p13,p14);
+SongPlayer myspkr(p26);
 
 //Globals
 int game_speed = 25;
@@ -25,7 +29,9 @@
 bool right = 0;
 bool stop = 0;
 bool four = 0;
-
+bool colsound = 0;
+int lives = 3;
+int round = 1;
 
 class Movement
 {
@@ -102,6 +108,19 @@
 }
 
 //Threads
+//Speaker
+void crash(void const *args)
+{
+    while(1) {
+        if(colsound) {
+            float note = 1568.0;
+            float durration = .5;
+            myspkr.PlaySong(&note, &durration);
+            colsound = 0;
+        }
+    }
+}
+
 //Controls
 int read_getc(Serial &blue)
 {
@@ -201,7 +220,17 @@
         }
     }
 }
+//game_over
+void game_over()
+{
+    ulcd.cls();
+    ulcd.printf("GAME OVER");
+    wait(3);
+    ulcd.cls();
+    lives = 3;
+    ulcd.printf("%d",lives);
 
+}
 //move
 void move_all(Movement &player, list<Movement> &asteroids, list<Movement> &bullets)
 {
@@ -241,6 +270,14 @@
     return dist < rad;
 }
 
+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);
+    Movement temp(x, y, a_x_vel, a_y_vel, 0, size, 1);
+    asteroids.push_back(temp);
+}
+
 void calc_collisions(Movement &player, list<Movement> &asteroids, list<Movement> &bullets)
 {
     bool hit = 0;
@@ -250,13 +287,21 @@
     list<Movement>::iterator itbcpy;
     for (it = asteroids.begin(); it != asteroids.end(); ++it) {
         if(collision(player,*it)) {
-            ulcd.printf("crash");
+            //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);
         }
     }
     for (it = asteroids.begin(); it != asteroids.end(); ++it) {
         for (it2 = bullets.begin(); it2 != bullets.end(); ++it2) {
             if(collision(*it,*it2)) {
-                ulcd.printf("hit");
+                //ulcd.printf("hit");
                 hit = 1;
                 itacpy = it;
                 itbcpy = it2;
@@ -264,75 +309,107 @@
         }
     }
     if(hit) {
+        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){
+            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);
-        }
     }
+}
 
 //drawing
-    void draw_triangle(Movement p) {
-        int x1 = p.x_pos + p.hit_rad * cos(p.angle);
-        int y1 = p.y_pos + p.hit_rad * sin(p.angle);
-        int x2 = p.x_pos + p.hit_rad * cos(p.angle + 2.5);
-        int y2 = p.y_pos + p.hit_rad * sin(p.angle + 2.5);
-        int x3 = p.x_pos + p.hit_rad * cos(p.angle - 2.5);
-        int y3 = p.y_pos + p.hit_rad * sin(p.angle - 2.5);
-        ulcd.triangle(x1,y1,x2,y2,x3,y3,BLUE);
-    }
+void draw_triangle(Movement p, int color)
+{
+    int x1 = p.x_pos + p.hit_rad * cos(p.angle);
+    int y1 = p.y_pos + p.hit_rad * sin(p.angle);
+    int x2 = p.x_pos + p.hit_rad * cos(p.angle + 2.5);
+    int y2 = p.y_pos + p.hit_rad * sin(p.angle + 2.5);
+    int x3 = p.x_pos + p.hit_rad * cos(p.angle - 2.5);
+    int y3 = p.y_pos + p.hit_rad * sin(p.angle - 2.5);
+    ulcd.triangle(x1,y1,x2,y2,x3,y3,color);
+}
 
-    void draw_objs(Movement player, list<Movement> asteroids, list<Movement> bullets) {
-        ulcd.cls();
-        draw_triangle(player);
-        ulcd.circle(player.x_pos,player.y_pos,player.hit_rad,BLUE);
-        list<Movement>::const_iterator iterator;
-        for (iterator = asteroids.begin(); iterator != asteroids.end(); ++iterator) {
-            ulcd.circle(iterator->x_pos,iterator->y_pos,iterator->hit_rad,RED);
-        }
-        for (iterator = bullets.begin(); iterator != bullets.end(); ++iterator) {
-            ulcd.filled_circle(iterator->x_pos,iterator->y_pos,iterator->hit_rad,WHITE);
+void draw_objs(Movement player, list<Movement> asteroids, list<Movement> bullets, int color)
+{
+    //ulcd.cls();
+    draw_triangle(player,color);
+    ulcd.circle(player.x_pos,player.y_pos,player.hit_rad,color);
+    list<Movement>::const_iterator iterator;
+    for (iterator = asteroids.begin(); iterator != asteroids.end(); ++iterator) {
+        ulcd.circle(iterator->x_pos,iterator->y_pos,iterator->hit_rad,color);
+    }
+    for (iterator = bullets.begin(); iterator != bullets.end(); ++iterator) {
+        ulcd.filled_circle(iterator->x_pos,iterator->y_pos,iterator->hit_rad,color);
+    }
+}
+
+//round check
+void round_check(list<Movement> &asteroids){
+    if(asteroids.empty()){
+        round++;
+        for(int i = 0; i < round; i++){
+            new_asteroid(asteroids, 140*(i%2), 140*((i%2)+1),10);
         }
     }
+}
 
-    int main() {
-        Thread buttons(controls);
-        Movement temp(100, 0, 0, 5, 0, 10, 1);
-        Movement player(50, 100, 0, -5, 0, 10, 1);
-        list<Movement> asteroids;
-        list<Movement> bullets;
-        asteroids.push_back(temp);
-        temp.assign(20, 0, 0, 5, 0, 10, 1);
-        asteroids.push_back(temp);
-        while(1) {
-            //controls
-            if(up) {
-                player.accelerate();
-            }
-            if(left) {
-                player.turn_left();
-            }
-            if(right) {
-                player.turn_right();
-            }
-            if(stop) {
-                player.x_vel = 0;
-                player.y_vel = 0;
-            }
-            if(four) {
-                temp.x_pos = player.x_pos + player.hit_rad * cos(player.angle);
-                temp.y_pos = player.y_pos + player.hit_rad * sin(player.angle);
-                temp.x_vel = BULSP * cos(player.angle);
-                temp.y_vel = BULSP * sin(player.angle);
-                temp.angle = player.angle;
-                temp.hit_rad = 3;
-                temp.wrap = 0;
-                bullets.push_back(temp);
-            }
+int main()
+{
+    Movement temp(0,0,0,0,0,0,0);
+    srand(time(NULL));
+    lives = 3;
+    ulcd.printf("%d",lives);
+    ulcd.baudrate(BAUD_3000000);
+    Thread sound(crash);
+    Thread buttons(controls);
+    Movement player(50, 100, 0, -5, 0, 10, 1);
+    list<Movement> asteroids;
+    list<Movement> bullets;
+    Movement p_player(50, 100, 0, -5, 0, 10, 1);
+    list<Movement> p_asteroids;
+    list<Movement> p_bullets;
+    while(1) {
+        //controls
+        if(up) {
+            player.accelerate();
+        }
+        if(left) {
+            player.turn_left();
+        }
+        if(right) {
+            player.turn_right();
+        }
+        if(stop) {
+            player.x_vel = 0;
+            player.y_vel = 0;
+        }
+        if(four) {
+            temp.x_pos = player.x_pos + player.hit_rad * cos(player.angle);
+            temp.y_pos = player.y_pos + player.hit_rad * sin(player.angle);
+            temp.x_vel = BULSP * cos(player.angle);
+            temp.y_vel = BULSP * sin(player.angle);
+            temp.angle = player.angle;
+            temp.hit_rad = 1;
+            temp.wrap = 0;
+            bullets.push_back(temp);
+        }
 
-            //move
-            move_all(player, asteroids, bullets);
-            garbage(bullets);
-            calc_collisions(player, asteroids, bullets);
-            draw_objs(player, asteroids, bullets);
-            Thread::wait(game_speed);
-        }
-    }
\ No newline at end of file
+        //move
+        round_check(asteroids);
+        move_all(player, asteroids, bullets);
+        garbage(bullets);
+        calc_collisions(player, asteroids, bullets);
+        draw_objs(p_player, p_asteroids, p_bullets,BLACK);
+        draw_objs(player, asteroids, bullets,WHITE);
+        p_player = player;
+        p_asteroids = asteroids;
+        p_bullets = bullets;
+        Thread::wait(game_speed);
+    }
+}
\ No newline at end of file