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/

Committer:
jford38
Date:
Sun Mar 20 03:38:40 2016 +0000
Revision:
0:9d6ea88b6d14
Child:
1:e487713a5231
Shell code for Spring 2016 Game "Agar.gt"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 0:9d6ea88b6d14 1 // Student Side Shell Code
jford38 0:9d6ea88b6d14 2 // For the baseline, anywhere you see ***, you have code to write.
jford38 0:9d6ea88b6d14 3
jford38 0:9d6ea88b6d14 4 #include "mbed.h"
jford38 0:9d6ea88b6d14 5
jford38 0:9d6ea88b6d14 6 #include "SDFileSystem.h"
jford38 0:9d6ea88b6d14 7 #include "wave_player.h"
jford38 0:9d6ea88b6d14 8 #include "game_synchronizer.h"
jford38 0:9d6ea88b6d14 9 #include "misc.h"
jford38 0:9d6ea88b6d14 10 #include "blob.h"
jford38 0:9d6ea88b6d14 11
jford38 0:9d6ea88b6d14 12 #define NUM_BLOBS 22
jford38 0:9d6ea88b6d14 13
jford38 0:9d6ea88b6d14 14 DigitalOut led1(LED1);
jford38 0:9d6ea88b6d14 15 DigitalOut led2(LED2);
jford38 0:9d6ea88b6d14 16 DigitalOut led3(LED3);
jford38 0:9d6ea88b6d14 17 DigitalOut led4(LED4);
jford38 0:9d6ea88b6d14 18
jford38 0:9d6ea88b6d14 19 DigitalIn pb_u(p21); // Up Button
jford38 0:9d6ea88b6d14 20 DigitalIn pb_r(p22); // Right Button
jford38 0:9d6ea88b6d14 21 DigitalIn pb_d(p23); // Down Button
jford38 0:9d6ea88b6d14 22 DigitalIn pb_l(p24); // Left Button
jford38 0:9d6ea88b6d14 23
jford38 0:9d6ea88b6d14 24 Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging!
jford38 0:9d6ea88b6d14 25 MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate)
jford38 0:9d6ea88b6d14 26 uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset)
jford38 0:9d6ea88b6d14 27 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs)
jford38 0:9d6ea88b6d14 28 AnalogOut DACout(p18); // speaker
jford38 0:9d6ea88b6d14 29 wave_player player(&DACout); // wav player
jford38 0:9d6ea88b6d14 30 GSYNC game_synchronizer; // Game_Synchronizer
jford38 0:9d6ea88b6d14 31 GSYNC* sync = &game_synchronizer; //
jford38 0:9d6ea88b6d14 32 Timer frame_timer; // Timer
jford38 0:9d6ea88b6d14 33
jford38 0:9d6ea88b6d14 34 int score1 = 0; // Player 1's score.
jford38 0:9d6ea88b6d14 35 int score2 = 0; // Player 2's score.
jford38 0:9d6ea88b6d14 36
jford38 0:9d6ea88b6d14 37
jford38 0:9d6ea88b6d14 38 // ***
jford38 0:9d6ea88b6d14 39 // Display a pretty game menu on the player 1 mbed.
jford38 0:9d6ea88b6d14 40 // Do a good job, and make it look nice. Give the player
jford38 0:9d6ea88b6d14 41 // an option to play in single- or multi-player mode.
jford38 0:9d6ea88b6d14 42 // Use the buttons to allow them to choose.
jford38 0:9d6ea88b6d14 43 int game_menu(void) {
jford38 0:9d6ea88b6d14 44 uLCD.background_color(BGRD_COL);
jford38 0:9d6ea88b6d14 45 uLCD.textbackground_color(BGRD_COL);
jford38 0:9d6ea88b6d14 46 uLCD.cls();
jford38 0:9d6ea88b6d14 47 uLCD.locate(0,0);
jford38 0:9d6ea88b6d14 48 uLCD.puts("AGAR SHELL");
jford38 0:9d6ea88b6d14 49
jford38 0:9d6ea88b6d14 50 // ***
jford38 0:9d6ea88b6d14 51 // Spin until a button is pressed. Depending which button is pressed,
jford38 0:9d6ea88b6d14 52 // return either SINGLE_PLAYER or MULTI_PLAYER.
jford38 0:9d6ea88b6d14 53 while(1) {
jford38 0:9d6ea88b6d14 54 if(!pb_u) { break; }
jford38 0:9d6ea88b6d14 55 }
jford38 0:9d6ea88b6d14 56
jford38 0:9d6ea88b6d14 57 return SINGLE_PLAYER;
jford38 0:9d6ea88b6d14 58
jford38 0:9d6ea88b6d14 59 }
jford38 0:9d6ea88b6d14 60
jford38 0:9d6ea88b6d14 61 // Initialize the game hardware.
jford38 0:9d6ea88b6d14 62 // Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
jford38 0:9d6ea88b6d14 63 // Initialize the game synchronizer.
jford38 0:9d6ea88b6d14 64 void game_init(void) {
jford38 0:9d6ea88b6d14 65
jford38 0:9d6ea88b6d14 66 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 0:9d6ea88b6d14 67
jford38 0:9d6ea88b6d14 68 pb_u.mode(PullUp);
jford38 0:9d6ea88b6d14 69 pb_r.mode(PullUp);
jford38 0:9d6ea88b6d14 70 pb_d.mode(PullUp);
jford38 0:9d6ea88b6d14 71 pb_l.mode(PullUp);
jford38 0:9d6ea88b6d14 72
jford38 0:9d6ea88b6d14 73 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 0:9d6ea88b6d14 74 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far
jford38 0:9d6ea88b6d14 75
jford38 0:9d6ea88b6d14 76 // game_menu MUST return either SINGLE_PLAYER or MULTI_PLAYER
jford38 0:9d6ea88b6d14 77 int num_players = game_menu();
jford38 0:9d6ea88b6d14 78
jford38 0:9d6ea88b6d14 79 GS_init(sync, &uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, num_players, PLAYER1); // Connect to the other player.
jford38 0:9d6ea88b6d14 80
jford38 0:9d6ea88b6d14 81 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 0:9d6ea88b6d14 82 srand(time(NULL)); // Seed the random number generator.
jford38 0:9d6ea88b6d14 83
jford38 0:9d6ea88b6d14 84 GS_cls(sync, SCREEN_BOTH);
jford38 0:9d6ea88b6d14 85 GS_update(sync);
jford38 0:9d6ea88b6d14 86 }
jford38 0:9d6ea88b6d14 87
jford38 0:9d6ea88b6d14 88 // Display who won!
jford38 0:9d6ea88b6d14 89 void game_over(int winner) {
jford38 0:9d6ea88b6d14 90 // Pause forever.
jford38 0:9d6ea88b6d14 91 while(1);
jford38 0:9d6ea88b6d14 92 }
jford38 0:9d6ea88b6d14 93
jford38 0:9d6ea88b6d14 94 // Take in a pointer to the blobs array. Iterate over the array
jford38 0:9d6ea88b6d14 95 // and initialize each blob with BLOB_init(). Set the first blob to (for example) blue
jford38 0:9d6ea88b6d14 96 // and the second blob to (for example) red. Set the color(s) of the food blobs however you like.
jford38 0:9d6ea88b6d14 97 // Make the radius of the "player blobs" equal and larger than the radius of the "food blobs".
jford38 0:9d6ea88b6d14 98 void generate_blobs(BLOB* blobs) {
jford38 0:9d6ea88b6d14 99 // ***
jford38 0:9d6ea88b6d14 100 }
jford38 0:9d6ea88b6d14 101
jford38 0:9d6ea88b6d14 102
jford38 0:9d6ea88b6d14 103
jford38 0:9d6ea88b6d14 104 int main (void) {
jford38 0:9d6ea88b6d14 105
jford38 0:9d6ea88b6d14 106 int* p1_buttons;
jford38 0:9d6ea88b6d14 107 int* p2_buttons;
jford38 0:9d6ea88b6d14 108
jford38 0:9d6ea88b6d14 109 float ax1, ay1, az1;
jford38 0:9d6ea88b6d14 110 float ax2, ay2, az2;
jford38 0:9d6ea88b6d14 111
jford38 0:9d6ea88b6d14 112
jford38 0:9d6ea88b6d14 113 // Ask the user to choose (via pushbuttons)
jford38 0:9d6ea88b6d14 114 // to play in single- or multi-player mode.
jford38 0:9d6ea88b6d14 115 // Use their choice to correctly initialize the game synchronizer.
jford38 0:9d6ea88b6d14 116 game_init();
jford38 0:9d6ea88b6d14 117
jford38 0:9d6ea88b6d14 118 // Keep an array of blobs. Use blob 0 for player 1 and
jford38 0:9d6ea88b6d14 119 // blob 1 for player 2.
jford38 0:9d6ea88b6d14 120 BLOB blobs[NUM_BLOBS];
jford38 0:9d6ea88b6d14 121
jford38 0:9d6ea88b6d14 122 // Pass in a pointer to the blobs array. Iterate over the array
jford38 0:9d6ea88b6d14 123 // and initialize each blob with BLOB_init(). Set the radii and colors
jford38 0:9d6ea88b6d14 124 // anyway you see fit.
jford38 0:9d6ea88b6d14 125 generate_blobs(blobs);
jford38 0:9d6ea88b6d14 126
jford38 0:9d6ea88b6d14 127
jford38 0:9d6ea88b6d14 128 while(true) {
jford38 0:9d6ea88b6d14 129
jford38 0:9d6ea88b6d14 130
jford38 0:9d6ea88b6d14 131 // In single-player, check to see if the player has eaten all other blobs.
jford38 0:9d6ea88b6d14 132 // In multi-player mode, check to see if the players have tied by eating half the food each.
jford38 0:9d6ea88b6d14 133 // ***
jford38 0:9d6ea88b6d14 134
jford38 0:9d6ea88b6d14 135
jford38 0:9d6ea88b6d14 136
jford38 0:9d6ea88b6d14 137 // In both single- and multi-player modes, display the score(s) in an appropriate manner.
jford38 0:9d6ea88b6d14 138 // ***
jford38 0:9d6ea88b6d14 139
jford38 0:9d6ea88b6d14 140
jford38 0:9d6ea88b6d14 141 // Use the game synchronizer API to get the button values from both players' mbeds.
jford38 0:9d6ea88b6d14 142 p1_buttons = GS_get_p1_buttons(sync);
jford38 0:9d6ea88b6d14 143 p2_buttons = GS_get_p2_buttons(sync);
jford38 0:9d6ea88b6d14 144
jford38 0:9d6ea88b6d14 145 // Use the game synchronizer API to get the accelerometer values from both players' mbeds.
jford38 0:9d6ea88b6d14 146 GS_get_p1_accel_data(sync, &ax1, &ay1, &az1);
jford38 0:9d6ea88b6d14 147 GS_get_p2_accel_data(sync, &ax2, &ay2, &az2);
jford38 0:9d6ea88b6d14 148
jford38 0:9d6ea88b6d14 149
jford38 0:9d6ea88b6d14 150 // If the magnitude of the p1 x and/or y accelerometer values exceed ACC_THRESHOLD,
jford38 0:9d6ea88b6d14 151 // set the blob 0 velocities to be proportional to the accelerometer values.
jford38 0:9d6ea88b6d14 152 // ***
jford38 0:9d6ea88b6d14 153
jford38 0:9d6ea88b6d14 154 // If in multi-player mode and the magnitude of the p2 x and/or y accelerometer values exceed ACC_THRESHOLD,
jford38 0:9d6ea88b6d14 155 // set the blob 0 velocities to be proportional to the accelerometer values.
jford38 0:9d6ea88b6d14 156 // ***
jford38 0:9d6ea88b6d14 157
jford38 0:9d6ea88b6d14 158 float time_step = .01;
jford38 0:9d6ea88b6d14 159
jford38 0:9d6ea88b6d14 160 // Undraw the world bounding rectangle (use BGRD_COL).
jford38 0:9d6ea88b6d14 161 // ***
jford38 0:9d6ea88b6d14 162
jford38 0:9d6ea88b6d14 163 // Loop over all blobs
jford38 0:9d6ea88b6d14 164 // ***
jford38 0:9d6ea88b6d14 165
jford38 0:9d6ea88b6d14 166 // 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
jford38 0:9d6ea88b6d14 167 // the old position of the blob. (If delete_now is true, reset delete_now to false.)
jford38 0:9d6ea88b6d14 168 // ***
jford38 0:9d6ea88b6d14 169
jford38 0:9d6ea88b6d14 170 // Use the blob positions and velocities, as well as the time_step to compute the new position of the blob.
jford38 0:9d6ea88b6d14 171 // ***
jford38 0:9d6ea88b6d14 172
jford38 0:9d6ea88b6d14 173 // If the current blob is blob 0, iterate over all blobs and check if they are close enough to eat or be eaten.
jford38 0:9d6ea88b6d14 174 // In multi-player mode, if the player 0 blob is eaten, player 1 wins and vise versa.
jford38 0:9d6ea88b6d14 175 // If blob 0 eats some food, increment score1.
jford38 0:9d6ea88b6d14 176 // ***
jford38 0:9d6ea88b6d14 177
jford38 0:9d6ea88b6d14 178 // If the current blob is blob 1 and we are playing in multi-player mode, iterate over all blobs and check
jford38 0:9d6ea88b6d14 179 // 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.
jford38 0:9d6ea88b6d14 180 // If blob1 eats some food, increment score 2.
jford38 0:9d6ea88b6d14 181 // ***
jford38 0:9d6ea88b6d14 182
jford38 0:9d6ea88b6d14 183 // You have to implement this function.
jford38 0:9d6ea88b6d14 184 // It should take in a pointer to a blob and constrain that blob to the world.
jford38 0:9d6ea88b6d14 185 // More details in blob.cpp
jford38 0:9d6ea88b6d14 186 //BLOB_constrain2world(&blobs[i]);
jford38 0:9d6ea88b6d14 187
jford38 0:9d6ea88b6d14 188
jford38 0:9d6ea88b6d14 189 // Iterate over all blobs and draw them at their newly computed positions. Reference their positions to the player blobs.
jford38 0:9d6ea88b6d14 190 // That is, on screen 1, center the world on blob 0 and reference all blobs' position to that of blob 0.
jford38 0:9d6ea88b6d14 191 // On screen 2, center the world on blob 1 and reference all blobs' position tho that of blob 1.
jford38 0:9d6ea88b6d14 192 // ***
jford38 0:9d6ea88b6d14 193
jford38 0:9d6ea88b6d14 194
jford38 0:9d6ea88b6d14 195 // Redraw the world boundary rectangle.
jford38 0:9d6ea88b6d14 196 // ***
jford38 0:9d6ea88b6d14 197
jford38 0:9d6ea88b6d14 198 // Update the screens by calling GS_update.
jford38 0:9d6ea88b6d14 199 GS_update(sync);
jford38 0:9d6ea88b6d14 200
jford38 0:9d6ea88b6d14 201 // If a button on either side is pressed, the corresponding LED on the player 1 mbed will toggle.
jford38 0:9d6ea88b6d14 202 // This is just for debug purposes, and to know that your button pushes are being registered.
jford38 0:9d6ea88b6d14 203 led1 = p1_buttons[0] ^ p2_buttons[0];
jford38 0:9d6ea88b6d14 204 led2 = p1_buttons[1] ^ p2_buttons[1];
jford38 0:9d6ea88b6d14 205 led3 = p1_buttons[2] ^ p2_buttons[2];
jford38 0:9d6ea88b6d14 206 led4 = p1_buttons[3] ^ p2_buttons[3];
jford38 0:9d6ea88b6d14 207 }
jford38 0:9d6ea88b6d14 208
jford38 0:9d6ea88b6d14 209 }