Revenge of the Mouse
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Revision 14:36c306e26317, committed 2015-10-26
- Comitter:
- jford38
- Date:
- Mon Oct 26 02:58:37 2015 +0000
- Parent:
- 13:22619424a52d
- Child:
- 15:4b27a3a95772
- Commit message:
- Split things into multiple files. Working pretty well now!
Changed in this revision
--- a/Game_Synchronizer.lib Sun Oct 25 20:12:36 2015 +0000 +++ b/Game_Synchronizer.lib Mon Oct 26 02:58:37 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#024665ba6939 +http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#e34b35474115
--- a/MMA8452.lib Sun Oct 25 20:12:36 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/ece2035ta/code/pockettanks/#fccb20977af0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tank/tank.cpp Mon Oct 26 02:58:37 2015 +0000 @@ -0,0 +1,77 @@ +#include "tank.h" +#include "globals.h" +#include "math.h" +#include "game_synchronizer.h" + +extern Game_Synchronizer sync; + + +Tank::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 Tank::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 Tank::min_y(void) { return y; } +int Tank::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 Tank::max_y(void) { + int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0; + return y + h + wheel_rad + barrel_extent; +} + +void Tank::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 Tank::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; + } + + int right_edge = sync.read_pixel(min_x()-1, (min_y()+max_y())/2.0); + int left_edge = sync.read_pixel(max_x()+1, (min_y()+max_y())/2.0); + + if( ( right_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x < old_x ) || + ( left_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x > old_x ) ) { + 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 Tank::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); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tank/tank.h Mon Oct 26 02:58:37 2015 +0000 @@ -0,0 +1,28 @@ +#ifndef TANK_H__ +#define TANK_H__ + + +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); + int min_x(void); + int min_y(void); + int max_x(void); + int max_y(void); + + void barrel_end(int& bx, int& by); + + void reposition(int new_x, int new_y, float new_theta); + void draw(); +}; + +#endif \ No newline at end of file
--- a/globals.h Sun Oct 25 20:12:36 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#ifndef GLOBAL_H -#define GLOBAL_H - -#ifndef ULCD_4DGL_H_ -#define ULCD_4DGL_H_ -#include "uLCD_4DGL.h" -#endif - -// === [global object] === -extern uLCD_4DGL uLCD; - - -// === [global settings] === -#define BACKGROUND_COLOR 0x000000 -#define GRID_RADIUS 3 -//I changed this -#define GRID_SIZE 8 //(GRID_RADIUS*2+1) -#define NUM_GRID_X 15 -#define NUM_GRID_Y 14 -#define NUM_GRID 210 //(NUM_GRID_X*NUM_GRID_Y) -//end of change - - - -#endif //GLOBAL_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globals/globals.cpp Mon Oct 26 02:58:37 2015 +0000 @@ -0,0 +1,13 @@ +#include "globals.h" + +int CONVERT_24_TO_16_BPP(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; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globals/globals.h Mon Oct 26 02:58:37 2015 +0000 @@ -0,0 +1,12 @@ +#ifndef GLOBAL_H__ +#define GLOBAL_H__ + +#define SKY_COLOR 0x7EC0EE +#define GND_COLOR 0x66CD00 +#define TANK_RED 0xCD0000 +#define TANK_BLUE 0x000080 +#define PI 3.1415926535797 + +int CONVERT_24_TO_16_BPP(int col_24); + +#endif //GLOBAL_H__ \ No newline at end of file
--- 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();
--- a/map/map.h Sun Oct 25 20:12:36 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -#ifndef MAP_H__ -#define MAP_H__ - -#define MAP_WIDTH 128 -#define MAP_HEIGHT 128 - -#define GROUND_VAL 0 -#define SKY_VAL 1 -#define OBSTACLE_VAL 2 - -int terrain[MAP_WIDTH][MAP_HEIGHT]; - -void map_init() { - for(int x = 0; x < MAP_WIDTH; x++) { - for(int y = 0; y < MAP_HEIGHT; y++) { - if(x < 20) {terrain[x][y] = GROUND_VAL; } - else if( 50 < y && 78 > y) { terrain[x][y] = OBSTACLE_VAL; } - } - } -} - - - -#endif \ No newline at end of file