The classic dueling tanks game for mbed.
Dependencies: 4DGL-uLCD-SE DRV2605 EthernetInterface Game_Synchronizer MMA8452 SDFileSystem SparkfunAnalogJoystick mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
main.cpp
- Committer:
- jford38
- Date:
- 2015-10-29
- Revision:
- 22:3c68eea5a609
- Parent:
- 21:edfeb289b21f
- Child:
- 23:77049670cae6
File content as of revision 22:3c68eea5a609:
// Student Side.
#include "mbed.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include "game_synchronizer.h"
#include "tank.h"
#include "bullet.h"
#include "misc.h"
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
Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER)
Timer frame_timer; // Timer
// Global variables go here.
// Ask the user whether to run the game in Single- or Multi-Player mode.
// Note that this function uses uLCD instead of sync because it is only
// writing to the local (Player1) lcd. Sync hasn't been initialized yet,
// so you can't use it anyway. For the same reason, you must access
// the buttons directly e.g. if( !pb_r ) { do something; }.
// return MULTI_PLAYER if the user selects multi-player.
// return SINGLE_PLAYER if the user selects single-player.
int game_menu(void) {
uLCD.baudrate(BAUD_3000000);
// the locate command tells the screen where to place the text.
uLCD.locate(0,15);
uLCD.puts("Replace me");
// Button Example:
// Use !pb_r to access the player1 right button value.
wait(2);
return SINGLE_PLAYER;
}
// Initialize the world map. I've provided a basic map here,
// but as part of the assignment you must create more
// interesting map(s).
// Note that calls to sync.function() will run function()
// on both players' LCDs (assuming you are in multiplayer mode).
// In single player mode, only your lcd will be modified. (Makes sense, right?)
void map_init() {
// Fill the entire screen with sky blue.
sync.background_color(SKY_COLOR);
// Call the clear screen function to force the LCD to redraw the screen
// with the new background color.
sync.cls();
// Draw the ground in green.
sync.filled_rectangle(0,0,128,20, GND_COLOR);
// Draw some obstacles. They don't have to be black,
// but they shouldn't be the same color as the sky or your tanks.
// Get creative here. You could use brown and grey to draw a mountain
// or something cool like that.
sync.filled_rectangle(59, 20, 69, 60, BLACK);
// Before you write text on the screens, tell the LCD where to put it.
sync.locate(0,15);
// Set the text background color to match the sky. Just for looks.
sync.textbackground_color(SKY_COLOR);
// Display the game title.
char title[] = " Title";
sync.puts(title, sizeof(title));
sync.update();
}
// 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.
int mode = game_menu();
sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
map_init();
pc.printf("Initialized...\n"); // Let us know you finished initializing.
}
// Display some kind of game over screen which lets us know who won.
// Play a cool sound!
void game_over() {
}
int main (void) {
int* p1_buttons;
int* p2_buttons;
float ax1, ay1, az1;
float ax2, ay2, az2;
game_init();
// Create your tanks.
Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color)
Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color)
// For each tank, create a bullet.
Bullet b1(&t1);
Bullet b2(&t2);
while(true) {
// Read the buttons/accelerometer and store the values
// in the synchronizer's internal array.
sync.set_p1_inputs();
// Get a pointer to the buttons for both sides.
p1_buttons = sync.get_p1_buttons();
p2_buttons = sync.get_p2_buttons();
// Get the accelerometer values.
sync.get_p1_accel_data(&ax1, &ay1, &az1);
sync.get_p2_accel_data(&ax2, &ay2, &az2);
// Game logic goes here.
// Depending on whose turn it is,
// - update their tank's position using the accelerometer state for that player.
// - update their bullet's position using the time elapsed since the previous frame.
// You have to handle the case where sync.play_mode == SINGLE_PLAYER
// as well as the case where sync.play_mode == MULTI_PLAYER
// Useful functions:
// t1.reposition(...);
// t2.reposition(...);
// b1.timestep(...);
// b2.timestep(...);
// End of game logic
// sync.update() flushes the internal buffer and performs all the draw commands on both sides.
// This must be called at the end of every frame, or things won't be drawn and buttons won't get
// updated.
sync.update();
}
game_over();
}
