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
Diff: main.cpp
- Revision:
- 15:4b27a3a95772
- Parent:
- 14:36c306e26317
- Child:
- 16:b73f0842d3cd
--- a/main.cpp Mon Oct 26 02:58:37 2015 +0000
+++ b/main.cpp Mon Oct 26 04:12:35 2015 +0000
@@ -1,11 +1,11 @@
// Student Side.
#include "mbed.h"
+
#include "game_synchronizer.h"
-
#include "tank.h"
+#include "bullet.h"
#include "globals.h"
-#include <vector>
DigitalOut led1(LED1);
DigitalOut led2(LED2);
@@ -61,67 +61,6 @@
sync.update();
}
-class Bullet{
- public:
- int x0;
- int y0;
- float vx0;
- float vy0;
- int x;
- int y;
- int speed;
- float time;
- bool in_flight;
-
- Tank* tank;
-
- Bullet(Tank* t) {
- tank = t;
- speed = 30;
- in_flight = false;
- }
- void shoot(void) {
- if(in_flight) {return;}
- time = 0;
- tank->barrel_end(x0, y0);
- x = x0; y = y0;
- vx0 = speed*cos(tank->barrel_theta);
- vy0 = speed*sin(tank->barrel_theta);
- in_flight = true;
- }
-
- void time_step(float dt) {
- if(!in_flight) {return;}
- time += dt;
- int new_x = x0 + vx0*time;
- int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time;
-
- if(new_x == x && new_y == y) { // Didn't move!
- return;
- }
-
- if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) {
- in_flight = false;
- return;
- }
-
- int col = sync.read_pixel(new_x, new_y);
- if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
- if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) {
- pc.printf("Player 1 wins!\n");
- }
- sync.pixel(x, y, SKY_COLOR);
- sync.filled_circle(new_x, new_y, 4, SKY_COLOR);
- in_flight = false;
- return;
- }
- sync.pixel(x, y, SKY_COLOR);
-
- x = new_x;
- y = new_y;
- sync.pixel(x, y, BLACK);
- }
-};
void game_init(void) {
@@ -178,10 +117,31 @@
if(!pb_d) { b1.shoot(); }
if(p2_buttons[2]) { b2.shoot(); }
- b1.time_step(frame_timer.read());
- b2.time_step(frame_timer.read());
+ int retval = b1.time_step(frame_timer.read());
+ if(retval) {
+ sync.update();
+ pc.printf("P1 wins!");
+ break;
+ }
+ retval = b2.time_step(frame_timer.read());
+ if(retval) {
+ sync.update();
+ pc.printf("P2 wins!");
+ break;
+ }
frame_timer.reset();
sync.update();
}
+
+
+ int i = 0;
+ while(1) {
+ sync.cls();
+ sync.locate(i, 6);
+ sync.puts("GAME OVER!\n", sizeof("GAME OVER!\n"));
+ sync.update();
+ (++i) %= 10;
+ wait(0.1);
+ }
}
\ No newline at end of file
