For Nikhil
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
main.cpp@22:3c68eea5a609, 2015-10-29 (annotated)
- Committer:
- jford38
- Date:
- Thu Oct 29 05:14:49 2015 +0000
- Revision:
- 22:3c68eea5a609
- Parent:
- 21:edfeb289b21f
- Child:
- 23:77049670cae6
The Shell for students to fill in to play their tanks game.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 6:3be57cf4bd33 | 1 | // Student Side. |
jford38 | 0:899c85cd266f | 2 | |
jford38 | 0:899c85cd266f | 3 | #include "mbed.h" |
jford38 | 15:4b27a3a95772 | 4 | |
jford38 | 17:7bc7127782e4 | 5 | #include "SDFileSystem.h" |
jford38 | 17:7bc7127782e4 | 6 | #include "wave_player.h" |
jford38 | 6:3be57cf4bd33 | 7 | #include "game_synchronizer.h" |
jford38 | 14:36c306e26317 | 8 | #include "tank.h" |
jford38 | 15:4b27a3a95772 | 9 | #include "bullet.h" |
jford38 | 20:6a58052b0140 | 10 | #include "misc.h" |
jford38 | 10:5da9b27e050e | 11 | |
jford38 | 17:7bc7127782e4 | 12 | |
jford38 | 6:3be57cf4bd33 | 13 | DigitalOut led1(LED1); |
jford38 | 6:3be57cf4bd33 | 14 | DigitalOut led2(LED2); |
jford38 | 6:3be57cf4bd33 | 15 | DigitalOut led3(LED3); |
jford38 | 6:3be57cf4bd33 | 16 | DigitalOut led4(LED4); |
jford38 | 0:899c85cd266f | 17 | |
jford38 | 17:7bc7127782e4 | 18 | DigitalIn pb_u(p21); // Up Button |
jford38 | 17:7bc7127782e4 | 19 | DigitalIn pb_r(p22); // Right Button |
jford38 | 17:7bc7127782e4 | 20 | DigitalIn pb_d(p23); // Down Button |
jford38 | 17:7bc7127782e4 | 21 | DigitalIn pb_l(p24); // Left Button |
jford38 | 6:3be57cf4bd33 | 22 | |
jford38 | 17:7bc7127782e4 | 23 | Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging! |
jford38 | 17:7bc7127782e4 | 24 | MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate) |
jford38 | 17:7bc7127782e4 | 25 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset) |
jford38 | 17:7bc7127782e4 | 26 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs) |
jford38 | 17:7bc7127782e4 | 27 | AnalogOut DACout(p18); // speaker |
jford38 | 17:7bc7127782e4 | 28 | wave_player player(&DACout); // wav player |
jford38 | 17:7bc7127782e4 | 29 | Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER) |
jford38 | 17:7bc7127782e4 | 30 | Timer frame_timer; // Timer |
jford38 | 0:899c85cd266f | 31 | |
jford38 | 20:6a58052b0140 | 32 | // Global variables go here. |
jford38 | 22:3c68eea5a609 | 33 | |
jford38 | 17:7bc7127782e4 | 34 | |
jford38 | 20:6a58052b0140 | 35 | |
jford38 | 20:6a58052b0140 | 36 | |
jford38 | 17:7bc7127782e4 | 37 | // Ask the user whether to run the game in Single- or Multi-Player mode. |
jford38 | 17:7bc7127782e4 | 38 | // Note that this function uses uLCD instead of sync because it is only |
jford38 | 17:7bc7127782e4 | 39 | // writing to the local (Player1) lcd. Sync hasn't been initialized yet, |
jford38 | 17:7bc7127782e4 | 40 | // so you can't use it anyway. For the same reason, you must access |
jford38 | 17:7bc7127782e4 | 41 | // the buttons directly e.g. if( !pb_r ) { do something; }. |
jford38 | 22:3c68eea5a609 | 42 | |
jford38 | 22:3c68eea5a609 | 43 | // return MULTI_PLAYER if the user selects multi-player. |
jford38 | 22:3c68eea5a609 | 44 | // return SINGLE_PLAYER if the user selects single-player. |
jford38 | 7:9506f2d84162 | 45 | int game_menu(void) { |
jford38 | 7:9506f2d84162 | 46 | |
jford38 | 18:18dfc9fb33b5 | 47 | uLCD.baudrate(BAUD_3000000); |
jford38 | 22:3c68eea5a609 | 48 | |
jford38 | 22:3c68eea5a609 | 49 | // the locate command tells the screen where to place the text. |
jford38 | 22:3c68eea5a609 | 50 | uLCD.locate(0,15); |
jford38 | 22:3c68eea5a609 | 51 | uLCD.puts("Replace me"); |
jford38 | 17:7bc7127782e4 | 52 | |
jford38 | 22:3c68eea5a609 | 53 | // Button Example: |
jford38 | 22:3c68eea5a609 | 54 | // Use !pb_r to access the player1 right button value. |
jford38 | 22:3c68eea5a609 | 55 | wait(2); |
jford38 | 22:3c68eea5a609 | 56 | return SINGLE_PLAYER; |
jford38 | 7:9506f2d84162 | 57 | } |
jford38 | 7:9506f2d84162 | 58 | |
jford38 | 17:7bc7127782e4 | 59 | // Initialize the world map. I've provided a basic map here, |
jford38 | 17:7bc7127782e4 | 60 | // but as part of the assignment you must create more |
jford38 | 17:7bc7127782e4 | 61 | // interesting map(s). |
jford38 | 17:7bc7127782e4 | 62 | // Note that calls to sync.function() will run function() |
jford38 | 17:7bc7127782e4 | 63 | // on both players' LCDs (assuming you are in multiplayer mode). |
jford38 | 17:7bc7127782e4 | 64 | // In single player mode, only your lcd will be modified. (Makes sense, right?) |
jford38 | 10:5da9b27e050e | 65 | void map_init() { |
jford38 | 17:7bc7127782e4 | 66 | |
jford38 | 17:7bc7127782e4 | 67 | // Fill the entire screen with sky blue. |
jford38 | 10:5da9b27e050e | 68 | sync.background_color(SKY_COLOR); |
jford38 | 17:7bc7127782e4 | 69 | |
jford38 | 17:7bc7127782e4 | 70 | // Call the clear screen function to force the LCD to redraw the screen |
jford38 | 17:7bc7127782e4 | 71 | // with the new background color. |
jford38 | 10:5da9b27e050e | 72 | sync.cls(); |
jford38 | 17:7bc7127782e4 | 73 | |
jford38 | 17:7bc7127782e4 | 74 | // Draw the ground in green. |
jford38 | 10:5da9b27e050e | 75 | sync.filled_rectangle(0,0,128,20, GND_COLOR); |
jford38 | 17:7bc7127782e4 | 76 | |
jford38 | 22:3c68eea5a609 | 77 | // Draw some obstacles. They don't have to be black, |
jford38 | 22:3c68eea5a609 | 78 | // but they shouldn't be the same color as the sky or your tanks. |
jford38 | 17:7bc7127782e4 | 79 | // Get creative here. You could use brown and grey to draw a mountain |
jford38 | 17:7bc7127782e4 | 80 | // or something cool like that. |
jford38 | 10:5da9b27e050e | 81 | sync.filled_rectangle(59, 20, 69, 60, BLACK); |
jford38 | 17:7bc7127782e4 | 82 | |
jford38 | 17:7bc7127782e4 | 83 | // Before you write text on the screens, tell the LCD where to put it. |
jford38 | 17:7bc7127782e4 | 84 | sync.locate(0,15); |
jford38 | 17:7bc7127782e4 | 85 | |
jford38 | 20:6a58052b0140 | 86 | // Set the text background color to match the sky. Just for looks. |
jford38 | 14:36c306e26317 | 87 | sync.textbackground_color(SKY_COLOR); |
jford38 | 22:3c68eea5a609 | 88 | |
jford38 | 20:6a58052b0140 | 89 | // Display the game title. |
jford38 | 22:3c68eea5a609 | 90 | char title[] = " Title"; |
jford38 | 17:7bc7127782e4 | 91 | sync.puts(title, sizeof(title)); |
jford38 | 10:5da9b27e050e | 92 | sync.update(); |
jford38 | 10:5da9b27e050e | 93 | } |
jford38 | 10:5da9b27e050e | 94 | |
jford38 | 21:edfeb289b21f | 95 | |
jford38 | 21:edfeb289b21f | 96 | // Initialize the game hardware. |
jford38 | 21:edfeb289b21f | 97 | // Call game_menu to find out which mode to play the game in (Single- or Multi-Player) |
jford38 | 21:edfeb289b21f | 98 | // Initialize the game synchronizer. |
jford38 | 6:3be57cf4bd33 | 99 | void game_init(void) { |
jford38 | 6:3be57cf4bd33 | 100 | |
jford38 | 6:3be57cf4bd33 | 101 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
jford38 | 6:3be57cf4bd33 | 102 | |
jford38 | 17:7bc7127782e4 | 103 | pb_u.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 104 | pb_r.mode(PullUp); |
jford38 | 17:7bc7127782e4 | 105 | pb_d.mode(PullUp); |
jford38 | 17:7bc7127782e4 | 106 | pb_l.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 107 | |
jford38 | 6:3be57cf4bd33 | 108 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
jford38 | 6:3be57cf4bd33 | 109 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
jford38 | 7:9506f2d84162 | 110 | int mode = game_menu(); |
jford38 | 17:7bc7127782e4 | 111 | sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player. |
jford38 | 10:5da9b27e050e | 112 | map_init(); |
jford38 | 6:3be57cf4bd33 | 113 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
jford38 | 6:3be57cf4bd33 | 114 | } |
jford38 | 6:3be57cf4bd33 | 115 | |
jford38 | 22:3c68eea5a609 | 116 | // Display some kind of game over screen which lets us know who won. |
jford38 | 22:3c68eea5a609 | 117 | // Play a cool sound! |
jford38 | 22:3c68eea5a609 | 118 | void game_over() { |
jford38 | 22:3c68eea5a609 | 119 | |
jford38 | 19:7aa3af04d6a8 | 120 | } |
jford38 | 19:7aa3af04d6a8 | 121 | |
jford38 | 21:edfeb289b21f | 122 | |
jford38 | 0:899c85cd266f | 123 | int main (void) { |
jford38 | 20:6a58052b0140 | 124 | |
jford38 | 20:6a58052b0140 | 125 | int* p1_buttons; |
jford38 | 20:6a58052b0140 | 126 | int* p2_buttons; |
jford38 | 20:6a58052b0140 | 127 | |
jford38 | 20:6a58052b0140 | 128 | float ax1, ay1, az1; |
jford38 | 20:6a58052b0140 | 129 | float ax2, ay2, az2; |
jford38 | 8:e6dd05393290 | 130 | |
jford38 | 8:e6dd05393290 | 131 | game_init(); |
jford38 | 8:e6dd05393290 | 132 | |
jford38 | 22:3c68eea5a609 | 133 | // Create your tanks. |
jford38 | 18:18dfc9fb33b5 | 134 | Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color) |
jford38 | 18:18dfc9fb33b5 | 135 | Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color) |
jford38 | 22:3c68eea5a609 | 136 | |
jford38 | 22:3c68eea5a609 | 137 | // For each tank, create a bullet. |
jford38 | 12:088a8203a9bb | 138 | Bullet b1(&t1); |
jford38 | 12:088a8203a9bb | 139 | Bullet b2(&t2); |
jford38 | 12:088a8203a9bb | 140 | |
jford38 | 5:cfec780c935b | 141 | |
jford38 | 20:6a58052b0140 | 142 | while(true) { |
jford38 | 12:088a8203a9bb | 143 | |
jford38 | 22:3c68eea5a609 | 144 | // Read the buttons/accelerometer and store the values |
jford38 | 22:3c68eea5a609 | 145 | // in the synchronizer's internal array. |
jford38 | 17:7bc7127782e4 | 146 | sync.set_p1_inputs(); |
jford38 | 17:7bc7127782e4 | 147 | |
jford38 | 22:3c68eea5a609 | 148 | // Get a pointer to the buttons for both sides. |
jford38 | 20:6a58052b0140 | 149 | p1_buttons = sync.get_p1_buttons(); |
jford38 | 20:6a58052b0140 | 150 | p2_buttons = sync.get_p2_buttons(); |
jford38 | 20:6a58052b0140 | 151 | |
jford38 | 22:3c68eea5a609 | 152 | // Get the accelerometer values. |
jford38 | 22:3c68eea5a609 | 153 | sync.get_p1_accel_data(&ax1, &ay1, &az1); |
jford38 | 22:3c68eea5a609 | 154 | sync.get_p2_accel_data(&ax2, &ay2, &az2); |
jford38 | 22:3c68eea5a609 | 155 | |
jford38 | 22:3c68eea5a609 | 156 | // Game logic goes here. |
jford38 | 8:e6dd05393290 | 157 | |
jford38 | 22:3c68eea5a609 | 158 | // Depending on whose turn it is, |
jford38 | 22:3c68eea5a609 | 159 | // - update their tank's position using the accelerometer state for that player. |
jford38 | 22:3c68eea5a609 | 160 | // - update their bullet's position using the time elapsed since the previous frame. |
jford38 | 22:3c68eea5a609 | 161 | |
jford38 | 22:3c68eea5a609 | 162 | // You have to handle the case where sync.play_mode == SINGLE_PLAYER |
jford38 | 22:3c68eea5a609 | 163 | // as well as the case where sync.play_mode == MULTI_PLAYER |
jford38 | 17:7bc7127782e4 | 164 | |
jford38 | 22:3c68eea5a609 | 165 | // Useful functions: |
jford38 | 22:3c68eea5a609 | 166 | // t1.reposition(...); |
jford38 | 22:3c68eea5a609 | 167 | // t2.reposition(...); |
jford38 | 22:3c68eea5a609 | 168 | // b1.timestep(...); |
jford38 | 22:3c68eea5a609 | 169 | // b2.timestep(...); |
jford38 | 22:3c68eea5a609 | 170 | |
jford38 | 22:3c68eea5a609 | 171 | // End of game logic |
jford38 | 22:3c68eea5a609 | 172 | |
jford38 | 22:3c68eea5a609 | 173 | // sync.update() flushes the internal buffer and performs all the draw commands on both sides. |
jford38 | 22:3c68eea5a609 | 174 | // This must be called at the end of every frame, or things won't be drawn and buttons won't get |
jford38 | 22:3c68eea5a609 | 175 | // updated. |
jford38 | 22:3c68eea5a609 | 176 | sync.update(); |
jford38 | 3:3ddefff03cb2 | 177 | } |
jford38 | 15:4b27a3a95772 | 178 | |
jford38 | 22:3c68eea5a609 | 179 | game_over(); |
jford38 | 21:edfeb289b21f | 180 | |
jford38 | 0:899c85cd266f | 181 | } |