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:
- 14:36c306e26317
- Parent:
- 12:088a8203a9bb
- Child:
- 15:4b27a3a95772
--- a/main.cpp Sun Oct 25 20:12:36 2015 +0000
+++ b/main.cpp Mon Oct 26 02:58:37 2015 +0000
@@ -2,16 +2,11 @@
#include "mbed.h"
#include "game_synchronizer.h"
-#include "MMA8452.h" // for accelerometer
-#include "math.h"
+
+#include "tank.h"
+#include "globals.h"
#include <vector>
-#define SKY_COLOR 0x7EC0EE
-#define GND_COLOR 0x66CD00
-#define TANK_RED 0xCD0000
-#define TANK_BLUE 0x000080
-#define PI 3.1415926535797
-
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
@@ -60,86 +55,12 @@
sync.cls();
sync.filled_rectangle(0,0,128,20, GND_COLOR);
sync.filled_rectangle(59, 20, 69, 60, BLACK);
+ sync.locate(0,0);
+ sync.textbackground_color(SKY_COLOR);
+ sync.puts("Pocket Tanks 2035", sizeof("Pocket Tanks 2035"));
sync.update();
}
-class Tank {
- public:
- int x, y;
- int w;
- int h;
- int tank_color;
- float barrel_theta;
- int barrel_length;
- int wheel_rad;
-
-
- Tank(int sx, int sy, int width, int height, int color) {
- x = sx; y = sy;
- w = width; h = height;
- tank_color = color;
- barrel_theta = PI/4.0;
- barrel_length = w;
- wheel_rad = 2.0;
- draw();
- }
- int min_x(void) {
- int barrel_extent = (-barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) + w/2.0 : 0;
- return x + barrel_extent; }
- int min_y(void) { return y; }
- int max_x(void) {
- int barrel_extent = (barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) : w/2.0;
- return x + w/2.0 + barrel_extent; }
- int max_y(void) {
- int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0;
- return y + h + wheel_rad + barrel_extent; }
-
- void barrel_end(int& bx, int& by) {
- bx = x + w/2.0 + (barrel_length+1)*cos(barrel_theta);
- by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta);
- }
-
- void reposition(int new_x, int new_y, float new_theta) {
- float old_theta = barrel_theta;
- int old_x = x;
- int old_y = y;
- int minx = min_x();
- int miny = min_y();
- int maxx = max_x();
- int maxy = max_y();
-
- x = new_x; y = new_y; barrel_theta = new_theta - (int)(new_theta/PI)*PI;
-
- if(min_x() < 0 || max_x() > 128) {
- x = old_x; y = old_y; barrel_theta = old_theta;
- draw();
- return;
- }
-
- sync.filled_rectangle(minx, miny, maxx, maxy, SKY_COLOR);
- sync.line(old_x + w/2.0, old_y+h+wheel_rad, old_x + w/2.0 + 1 + barrel_length*cos(old_theta), old_y+h + barrel_length*sin(old_theta), SKY_COLOR);
- draw();
- }
- void draw() {
- sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK);
- sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color);
- sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK);
- sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK);
- }
-};
-
-int convert24to16bpp(int col_24) {
- int b = col_24 & 0xFF;
- int g = (col_24 >> 8) & 0xFF;
- int r = (col_24 >> 16)& 0xFF;
-
- r >>= 3;
- g >>= 2;
- b >>= 3;
-
- return r<<11 | g<<5 | b;
-}
-
class Bullet{
public:
int x0;
@@ -175,7 +96,7 @@
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) {
+ if(new_x == x && new_y == y) { // Didn't move!
return;
}
@@ -185,8 +106,8 @@
}
int col = sync.read_pixel(new_x, new_y);
- if(col != convert24to16bpp(SKY_COLOR)) {
- if(col == convert24to16bpp(TANK_BLUE)) {
+ 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);
@@ -252,12 +173,13 @@
//if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0);
if(!pb_u) t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0);
if(p2_buttons[0]) t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0);
- if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0);
+ //if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0);
if(!pb_d) { b1.shoot(); }
+ if(p2_buttons[2]) { b2.shoot(); }
- b1.time_step(0.01);
- //b2.time_step(1);
+ b1.time_step(frame_timer.read());
+ b2.time_step(frame_timer.read());
frame_timer.reset();
sync.update();
