ECE 2035 AgarIO MiniProject

Dependencies:   4DGL-uLCD-SE ECE2035_Agar_Shell EthernetInterface Game_Synchronizer MMA8452 SDFileSystem Sound USBDevice mbed-rtos mbed wave_player

Fork of ECE2035_Agar_Shell by ECE2035 Spring 2015 TA

This program is a MiniProject given in ECE 2035. This is to design and program the multiplayer ethernet based AGAR.IO mbed game. Starting code is given by the instructor to aid student to complete the project. It utilize uLCD, Accelerometer, pushbuttons, sdFileSystem, ethernetBreakoutBoard, speaker in mBED LPC1768. It uses the similar concept to the Game http://agar.io/

Files at this revision

API Documentation at this revision

Comitter:
jford38
Date:
Sun Mar 20 03:38:40 2016 +0000
Child:
1:e487713a5231
Commit message:
Shell code for Spring 2016 Game "Agar.gt"

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
Game_Synchronizer.lib Show annotated file Show diff for this revision Revisions of this file
MMA8452.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
blob.cpp Show annotated file Show diff for this revision Revisions of this file
blob.h 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
misc.h Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Game_Synchronizer.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#a421f644f454
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8452.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/ashleymills/code/MMA8452/#a92a632a0cc7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/neilt6/code/SDFileSystem/#c2c1f0b16380
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blob.cpp	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,44 @@
+#include "blob.h"
+#include "mbed.h"
+
+extern Serial pc;
+
+
+// Randomly initialize a blob's position, velocity, color, radius, etc.
+// Set the valid flag to true and the delete_now flag to false.
+// delete_now is basically the derivative of valid. It goes true for one
+// fram when the blob is deleted, and then it is reset to false in the next frame
+// when that blob is deleted.
+void BLOB_init(BLOB* b) {
+    // ***
+}
+
+
+// Take in a blob and determine whether it is inside the world.
+// If the blob has escaped the world, put it back on the edge
+// of the world and negate its velocity so that it bounces off
+// the boundary. Use WORLD_WIDTH and WORLD_HEIGHT defined in "misc.h"
+void BLOB_constrain2world(BLOB* b) {
+    // ***
+}
+
+// Randomly initialize a blob. Then set the radius to the provided value.
+void BLOB_init(BLOB* b, int rad) {
+    // ***
+}
+
+// Randomly initialize a blob. Then set the radius and color to the 
+// provided values.
+void BLOB_init(BLOB* b, int rad, int color) {
+    // ***
+}
+
+// For debug purposes, you can use this to print a blob's properties to your computer's serial monitor.
+void BLOB_print(BLOB b) {
+    pc.printf("(%f, %f) <%f, %f> Color: 0x%x\n", b.posx, b.posy, b.vx, b.vy, b.color);
+}
+
+// Return the square of the distance from b1 to b2
+float BLOB_dist2(BLOB b1, BLOB b2) {
+    // ***
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blob.h	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,19 @@
+#include "misc.h"
+
+typedef struct blob {
+    float old_x, old_y;
+    float posx, posy;
+    float vx, vy;
+    float rad;
+    int color;    
+    bool valid;
+    bool delete_now;
+} BLOB;
+
+void BLOB_init(BLOB* b);
+void BLOB_init(BLOB* b, int rad);
+void BLOB_init(BLOB* b, int rad, int color);
+
+void BLOB_constrain2world(BLOB* b);
+void BLOB_print(BLOB b);
+float BLOB_dist2(BLOB b1, BLOB b2);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,209 @@
+// Student Side Shell Code
+// For the baseline, anywhere you see ***, you have code to write.
+
+#include "mbed.h"
+
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include "game_synchronizer.h"
+#include "misc.h"
+#include "blob.h"
+
+#define NUM_BLOBS 22
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+DigitalIn pb_u(p21);                        // Up Button
+DigitalIn pb_r(p22);                        // Right Button
+DigitalIn pb_d(p23);                        // Down Button
+DigitalIn pb_l(p24);                        // Left Button
+
+Serial pc(USBTX, USBRX);                    // Serial connection to PC. Useful for debugging!
+MMA8452 acc(p28, p27, 100000);              // Accelerometer (SDA, SCL, Baudrate)
+uLCD_4DGL uLCD(p9,p10,p11);                 // LCD (tx, rx, reset)
+SDFileSystem sd(p5, p6, p7, p8, "sd");      // SD  (mosi, miso, sck, cs)
+AnalogOut DACout(p18);                      // speaker
+wave_player player(&DACout);                // wav player
+GSYNC game_synchronizer;                    // Game_Synchronizer
+GSYNC* sync = &game_synchronizer;           //
+Timer frame_timer;                          // Timer
+
+int score1 = 0;                             // Player 1's score.
+int score2 = 0;                             // Player 2's score.
+
+
+// ***
+// Display a pretty game menu on the player 1 mbed. 
+// Do a good job, and make it look nice. Give the player
+// an option to play in single- or multi-player mode. 
+// Use the buttons to allow them to choose.
+int game_menu(void) {
+    uLCD.background_color(BGRD_COL);
+    uLCD.textbackground_color(BGRD_COL);
+    uLCD.cls();
+    uLCD.locate(0,0);
+    uLCD.puts("AGAR SHELL");   
+    
+    // ***
+    // Spin until a button is pressed. Depending which button is pressed, 
+    // return either SINGLE_PLAYER or MULTI_PLAYER.
+    while(1) {
+        if(!pb_u) { break; }
+    }
+    
+    return SINGLE_PLAYER;
+    
+}
+
+// Initialize the game hardware. 
+// Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
+// Initialize the game synchronizer.
+void game_init(void) {
+    
+    led1 = 0; led2 = 0; led3 = 0; led4 = 0;
+    
+    pb_u.mode(PullUp);
+    pb_r.mode(PullUp); 
+    pb_d.mode(PullUp);    
+    pb_l.mode(PullUp);
+    
+    pc.printf("\033[2J\033[0;0H");              // Clear the terminal screen.
+    pc.printf("I'm alive! Player 1\n");         // Let us know you made it this far
+
+    // game_menu MUST return either SINGLE_PLAYER or MULTI_PLAYER
+    int num_players = game_menu();
+    
+    GS_init(sync, &uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, num_players, PLAYER1); // Connect to the other player.
+        
+    pc.printf("Initialized...\n");              // Let us know you finished initializing.
+    srand(time(NULL));                          // Seed the random number generator.
+
+    GS_cls(sync, SCREEN_BOTH);
+    GS_update(sync);
+}
+
+// Display who won!
+void game_over(int winner) {
+    // Pause forever.
+    while(1);
+}
+
+// Take in a pointer to the blobs array. Iterate over the array
+// and initialize each blob with BLOB_init(). Set the first blob to (for example) blue
+// and the second blob to (for example) red. Set the color(s) of the food blobs however you like.
+// Make the radius of the "player blobs" equal and larger than the radius of the "food blobs".
+void generate_blobs(BLOB* blobs) {
+    // ***
+}
+
+
+
+int main (void) {
+    
+    int* p1_buttons;
+    int* p2_buttons;
+    
+    float ax1, ay1, az1;
+    float ax2, ay2, az2;
+    
+    
+    // Ask the user to choose (via pushbuttons)
+    // to play in single- or multi-player mode.
+    // Use their choice to correctly initialize the game synchronizer.
+    game_init();  
+    
+    // Keep an array of blobs. Use blob 0 for player 1 and
+    // blob 1 for player 2.
+    BLOB blobs[NUM_BLOBS];
+        
+    // Pass in a pointer to the blobs array. Iterate over the array
+    // and initialize each blob with BLOB_init(). Set the radii and colors
+    // anyway you see fit.
+    generate_blobs(blobs);
+    
+
+    while(true) {
+        
+        
+        // In single-player, check to see if the player has eaten all other blobs.
+        // In multi-player mode, check to see if the players have tied by eating half the food each.
+        // ***
+        
+        
+        
+        // In both single- and multi-player modes, display the score(s) in an appropriate manner.
+        // ***
+        
+        
+        // Use the game synchronizer API to get the button values from both players' mbeds.
+        p1_buttons = GS_get_p1_buttons(sync);
+        p2_buttons = GS_get_p2_buttons(sync);
+        
+        // Use the game synchronizer API to get the accelerometer values from both players' mbeds.
+        GS_get_p1_accel_data(sync, &ax1, &ay1, &az1);
+        GS_get_p2_accel_data(sync, &ax2, &ay2, &az2);
+        
+        
+        // If the magnitude of the p1 x and/or y accelerometer values exceed ACC_THRESHOLD,
+        // set the blob 0 velocities to be proportional to the accelerometer values.
+        // ***
+        
+        // If in multi-player mode and the magnitude of the p2 x and/or y accelerometer values exceed ACC_THRESHOLD,
+        // set the blob 0 velocities to be proportional to the accelerometer values.
+        // ***
+        
+        float time_step = .01; 
+        
+        // Undraw the world bounding rectangle (use BGRD_COL).
+        // ***
+        
+        // Loop over all blobs
+        // ***
+            
+            // If the current blob is valid, or it was deleted in the last frame, (delete_now is true), then draw a background colored circle over
+            // the old position of the blob. (If delete_now is true, reset delete_now to false.)  
+            // ***
+            
+            // Use the blob positions and velocities, as well as the time_step to compute the new position of the blob.
+            // ***
+            
+            // If the current blob is blob 0, iterate over all blobs and check if they are close enough to eat or be eaten. 
+            // In multi-player mode, if the player 0 blob is eaten, player 1 wins and vise versa.        
+            // If blob 0 eats some food, increment score1.   
+            // ***
+            
+            // If the current blob is blob 1 and we are playing in multi-player mode, iterate over all blobs and check
+            // if they are close enough to eat or be eaten. In multi-player mode, if the player 1 blob is eaten, player 0 wins and vise versa.
+            // If blob1 eats some food, increment score 2.
+            // ***
+            
+            // You have to implement this function.
+            // It should take in a pointer to a blob and constrain that blob to the world.
+            // More details in blob.cpp
+            //BLOB_constrain2world(&blobs[i]);           
+            
+        
+        // Iterate over all blobs and draw them at their newly computed positions. Reference their positions to the player blobs.
+        // That is, on screen 1, center the world on blob 0 and reference all blobs' position to that of blob 0.
+        // On screen 2, center the world on blob 1 and reference all blobs' position tho that of blob 1.
+        // ***
+        
+        
+        // Redraw the world boundary rectangle.
+        // ***
+        
+        // Update the screens by calling GS_update.
+        GS_update(sync);
+        
+        // If a button on either side is pressed, the corresponding LED on the player 1 mbed will toggle.
+        // This is just for debug purposes, and to know that your button pushes are being registered.
+        led1 = p1_buttons[0] ^ p2_buttons[0];
+        led2 = p1_buttons[1] ^ p2_buttons[1];
+        led3 = p1_buttons[2] ^ p2_buttons[2];
+        led4 = p1_buttons[3] ^ p2_buttons[3];
+    } 
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#07314541bd12
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc.h	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,25 @@
+#ifndef MISC_H__
+#define MISC_H__
+
+#define U_BUTTON 0
+#define R_BUTTON 1
+#define D_BUTTON 2
+#define L_BUTTON 3
+
+#define WINNER_P1   0
+#define WINNER_P2   1
+#define WINNER_TIE  2
+
+
+// Feel free to adjust any of these values as you see fit.
+#define WORLD_WIDTH  256
+#define WORLD_HEIGHT 256
+
+#define ACC_THRESHOLD 0.20
+
+#define BGRD_COL   0x0F0066
+#define FOOD_COL   0xFFFFFF
+#define P1_COL     0xBB0CE8
+#define P2_COL     0xFF0030
+#define BORDER_COL 0xFFFFFF
+#endif //MISC_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Sun Mar 20 03:38:40 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/sravet/code/wave_player/#acc3e18e77ad