pocket tanks
Dependencies: 4DGL-uLCD-SE pockettanks SDFileSystem mbed wave_player
Fork of ECE2035_FroggerGame_SUM1025 by
cars/cars.cpp@3:8f68c00dd75a, 2015-10-22 (annotated)
- Committer:
- ece2035ta
- Date:
- Thu Oct 22 03:47:32 2015 +0000
- Revision:
- 3:8f68c00dd75a
- Parent:
- 1:172fbc00199c
hi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ece2035ta | 1:172fbc00199c | 1 | #include "cars.h" |
ece2035ta | 1:172fbc00199c | 2 | //#include "mbed.h" |
ece2035ta | 1:172fbc00199c | 3 | //#include "globals.h" |
ece2035ta | 1:172fbc00199c | 4 | //#include "map_public.h" |
ece2035ta | 1:172fbc00199c | 5 | |
ece2035ta | 1:172fbc00199c | 6 | |
ece2035ta | 1:172fbc00199c | 7 | //======================================================================= |
ece2035ta | 1:172fbc00199c | 8 | //Private Functions |
ece2035ta | 1:172fbc00199c | 9 | void collision() { |
ece2035ta | 1:172fbc00199c | 10 | uLCD.cls(); |
ece2035ta | 1:172fbc00199c | 11 | uLCD.locate(6,8); |
ece2035ta | 1:172fbc00199c | 12 | uLCD.printf("GAME OVER"); |
ece2035ta | 1:172fbc00199c | 13 | |
ece2035ta | 1:172fbc00199c | 14 | while(1) { } |
ece2035ta | 1:172fbc00199c | 15 | } |
ece2035ta | 1:172fbc00199c | 16 | void car_up(car_t * g) |
ece2035ta | 1:172fbc00199c | 17 | { |
ece2035ta | 1:172fbc00199c | 18 | //Need if statement to loop around |
ece2035ta | 1:172fbc00199c | 19 | if (g->car_blk_y == 0) |
ece2035ta | 1:172fbc00199c | 20 | { |
ece2035ta | 1:172fbc00199c | 21 | car_move(g,g->car_blk_x, 13); |
ece2035ta | 1:172fbc00199c | 22 | return; |
ece2035ta | 1:172fbc00199c | 23 | } |
ece2035ta | 1:172fbc00199c | 24 | |
ece2035ta | 1:172fbc00199c | 25 | car_move(g, g->car_blk_x, g->car_blk_y-1); |
ece2035ta | 1:172fbc00199c | 26 | return; |
ece2035ta | 1:172fbc00199c | 27 | } |
ece2035ta | 1:172fbc00199c | 28 | |
ece2035ta | 1:172fbc00199c | 29 | //Function to choose car direction |
ece2035ta | 1:172fbc00199c | 30 | //This can probably be merged with drive function |
ece2035ta | 1:172fbc00199c | 31 | void car_down(car_t * g) |
ece2035ta | 1:172fbc00199c | 32 | { |
ece2035ta | 1:172fbc00199c | 33 | |
ece2035ta | 1:172fbc00199c | 34 | car_move(g, g->car_blk_x, g->car_blk_y+1); |
ece2035ta | 1:172fbc00199c | 35 | return; |
ece2035ta | 1:172fbc00199c | 36 | } |
ece2035ta | 1:172fbc00199c | 37 | |
ece2035ta | 1:172fbc00199c | 38 | void clean_blk(unsigned int blk_x, unsigned int blk_y) |
ece2035ta | 1:172fbc00199c | 39 | { |
ece2035ta | 1:172fbc00199c | 40 | GRID grid_info = map_get_grid_status(blk_x,blk_y); |
ece2035ta | 1:172fbc00199c | 41 | uLCD.filled_rectangle(grid_info.x, grid_info.y, grid_info.x+GRID_SIZE-1, grid_info.y+GRID_SIZE+1, BACKGROUND_COLOR); |
ece2035ta | 1:172fbc00199c | 42 | return; |
ece2035ta | 1:172fbc00199c | 43 | } |
ece2035ta | 1:172fbc00199c | 44 | |
ece2035ta | 1:172fbc00199c | 45 | void drawCar(car_t * g, int grid_x, int grid_y) |
ece2035ta | 1:172fbc00199c | 46 | { |
ece2035ta | 1:172fbc00199c | 47 | GRID grid_info = map_get_grid_status(grid_x,grid_y); |
ece2035ta | 1:172fbc00199c | 48 | int pos_x = grid_info.x + GRID_RADIUS; |
ece2035ta | 1:172fbc00199c | 49 | int pos_y = grid_info.y + GRID_RADIUS; |
ece2035ta | 1:172fbc00199c | 50 | uLCD.filled_rectangle(pos_x-GRID_RADIUS, pos_y, pos_x+GRID_RADIUS-2,pos_y+GRID_RADIUS+2,g->car_color); |
ece2035ta | 1:172fbc00199c | 51 | return; |
ece2035ta | 1:172fbc00199c | 52 | |
ece2035ta | 1:172fbc00199c | 53 | } |
ece2035ta | 1:172fbc00199c | 54 | //This moves the car from tile to tile |
ece2035ta | 1:172fbc00199c | 55 | |
ece2035ta | 1:172fbc00199c | 56 | void car_move(car_t * g, unsigned int new_blk_x, unsigned int new_blk_y) |
ece2035ta | 1:172fbc00199c | 57 | { |
ece2035ta | 1:172fbc00199c | 58 | // clean up ghost at old position |
ece2035ta | 1:172fbc00199c | 59 | clean_blk(g->car_blk_x, g->car_blk_y); |
ece2035ta | 1:172fbc00199c | 60 | // clean the block at new position |
ece2035ta | 1:172fbc00199c | 61 | clean_blk(new_blk_x, new_blk_y); |
ece2035ta | 1:172fbc00199c | 62 | // draw the ghost at new position |
ece2035ta | 1:172fbc00199c | 63 | drawCar(g, new_blk_x, new_blk_y); |
ece2035ta | 1:172fbc00199c | 64 | |
ece2035ta | 1:172fbc00199c | 65 | // recover map component |
ece2035ta | 1:172fbc00199c | 66 | map_draw_grid(g->car_blk_x, g->car_blk_y); |
ece2035ta | 1:172fbc00199c | 67 | |
ece2035ta | 1:172fbc00199c | 68 | g->car_blk_x = new_blk_x; |
ece2035ta | 1:172fbc00199c | 69 | g->car_blk_y = new_blk_y; |
ece2035ta | 1:172fbc00199c | 70 | return; |
ece2035ta | 1:172fbc00199c | 71 | } |
ece2035ta | 1:172fbc00199c | 72 | |
ece2035ta | 1:172fbc00199c | 73 | |
ece2035ta | 1:172fbc00199c | 74 | //============================================================================== |
ece2035ta | 1:172fbc00199c | 75 | //Public Functions |
ece2035ta | 1:172fbc00199c | 76 | |
ece2035ta | 1:172fbc00199c | 77 | |
ece2035ta | 1:172fbc00199c | 78 | void car_init(car_t * g) { |
ece2035ta | 1:172fbc00199c | 79 | if(g->lane == 1) |
ece2035ta | 1:172fbc00199c | 80 | { g->car_blk_x = 4; |
ece2035ta | 1:172fbc00199c | 81 | g->car_blk_y = 0; |
ece2035ta | 1:172fbc00199c | 82 | drawCar(g, 7, 0); |
ece2035ta | 1:172fbc00199c | 83 | return; |
ece2035ta | 1:172fbc00199c | 84 | } |
ece2035ta | 1:172fbc00199c | 85 | if (g->lane == 2) |
ece2035ta | 1:172fbc00199c | 86 | { |
ece2035ta | 1:172fbc00199c | 87 | g->car_blk_x = 7; |
ece2035ta | 1:172fbc00199c | 88 | g->car_blk_y = 0; |
ece2035ta | 1:172fbc00199c | 89 | drawCar(g, 7, 0); |
ece2035ta | 1:172fbc00199c | 90 | return; |
ece2035ta | 1:172fbc00199c | 91 | } |
ece2035ta | 1:172fbc00199c | 92 | |
ece2035ta | 1:172fbc00199c | 93 | if (g->lane == 3) |
ece2035ta | 1:172fbc00199c | 94 | { |
ece2035ta | 1:172fbc00199c | 95 | g->car_blk_x = 10; |
ece2035ta | 1:172fbc00199c | 96 | g->car_blk_y = 0; |
ece2035ta | 1:172fbc00199c | 97 | drawCar(g, 10, 0); |
ece2035ta | 1:172fbc00199c | 98 | return; |
ece2035ta | 1:172fbc00199c | 99 | } |
ece2035ta | 1:172fbc00199c | 100 | |
ece2035ta | 1:172fbc00199c | 101 | return; |
ece2035ta | 1:172fbc00199c | 102 | } |
ece2035ta | 1:172fbc00199c | 103 | |
ece2035ta | 1:172fbc00199c | 104 | void drive(car_t * g) |
ece2035ta | 1:172fbc00199c | 105 | { |
ece2035ta | 1:172fbc00199c | 106 | |
ece2035ta | 1:172fbc00199c | 107 | switch (g->car_motion) { |
ece2035ta | 1:172fbc00199c | 108 | case CAR_UP: |
ece2035ta | 1:172fbc00199c | 109 | car_up(g); |
ece2035ta | 1:172fbc00199c | 110 | break; |
ece2035ta | 1:172fbc00199c | 111 | case CAR_DOWN: |
ece2035ta | 1:172fbc00199c | 112 | car_down(g); |
ece2035ta | 1:172fbc00199c | 113 | break; |
ece2035ta | 1:172fbc00199c | 114 | default: |
ece2035ta | 1:172fbc00199c | 115 | break; |
ece2035ta | 1:172fbc00199c | 116 | } |
ece2035ta | 1:172fbc00199c | 117 | return; |
ece2035ta | 1:172fbc00199c | 118 | } |