![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
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/
Diff: main.cpp
- Revision:
- 0:9d6ea88b6d14
- Child:
- 1:e487713a5231
--- /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