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