frogger game revision

Dependencies:   4DGL-uLCD-SE MMA8452 SDFileSystem mbed wave_player

Dependents:   C_Assembly

Fork of ECE2035_FroggerGame_SUM2015 by ECE 2035

Committer:
ssong86
Date:
Mon Feb 01 06:43:06 2016 +0000
Revision:
9:47f07aa85f1a
Parent:
8:1b6fc10c5cea
frogger

Who changed what in which revision?

UserRevisionLine numberNew 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)
ssong86 9:47f07aa85f1a 80 { g->car_blk_x = 5;
ece2035ta 8:1b6fc10c5cea 81 g->car_blk_y = 0;
ssong86 9:47f07aa85f1a 82 drawCar(g, 5, 0);
ece2035ta 8:1b6fc10c5cea 83 return;
ece2035ta 8:1b6fc10c5cea 84 }
ece2035ta 8:1b6fc10c5cea 85 if (g->lane == 2)
ece2035ta 8:1b6fc10c5cea 86 {
ssong86 9:47f07aa85f1a 87 g->car_blk_x = 8;
ece2035ta 8:1b6fc10c5cea 88 g->car_blk_y = 0;
ssong86 9:47f07aa85f1a 89 drawCar(g, 8, 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 {
ssong86 9:47f07aa85f1a 95 g->car_blk_x = 11;
ssong86 9:47f07aa85f1a 96 g->car_blk_y = 0;
ssong86 9:47f07aa85f1a 97 drawCar(g, 11, 0);
ssong86 9:47f07aa85f1a 98 return;
ssong86 9:47f07aa85f1a 99 }
ssong86 9:47f07aa85f1a 100
ssong86 9:47f07aa85f1a 101 if (g->lane == 4)
ssong86 9:47f07aa85f1a 102 {
ssong86 9:47f07aa85f1a 103 g->car_blk_x = 6;
ssong86 9:47f07aa85f1a 104 g->car_blk_y = 0;
ssong86 9:47f07aa85f1a 105 drawCar(g, 6, 0);
ssong86 9:47f07aa85f1a 106 return;
ssong86 9:47f07aa85f1a 107 }
ssong86 9:47f07aa85f1a 108
ssong86 9:47f07aa85f1a 109 if (g->lane == 5)
ssong86 9:47f07aa85f1a 110 {
ece2035ta 8:1b6fc10c5cea 111 g->car_blk_x = 10;
ece2035ta 8:1b6fc10c5cea 112 g->car_blk_y = 0;
ssong86 9:47f07aa85f1a 113 drawCar(g,10,0);
ece2035ta 8:1b6fc10c5cea 114 return;
ssong86 9:47f07aa85f1a 115 }
ece2035ta 8:1b6fc10c5cea 116
ece2035ta 8:1b6fc10c5cea 117 return;
ece2035ta 8:1b6fc10c5cea 118 }
ece2035ta 8:1b6fc10c5cea 119
ece2035ta 8:1b6fc10c5cea 120 void drive(car_t * g)
ece2035ta 8:1b6fc10c5cea 121 {
ece2035ta 8:1b6fc10c5cea 122
ece2035ta 8:1b6fc10c5cea 123 switch (g->car_motion) {
ece2035ta 8:1b6fc10c5cea 124 case CAR_UP:
ece2035ta 8:1b6fc10c5cea 125 car_up(g);
ece2035ta 8:1b6fc10c5cea 126 break;
ece2035ta 8:1b6fc10c5cea 127 case CAR_DOWN:
ece2035ta 8:1b6fc10c5cea 128 car_down(g);
ece2035ta 8:1b6fc10c5cea 129 break;
ece2035ta 8:1b6fc10c5cea 130 default:
ece2035ta 8:1b6fc10c5cea 131 break;
ece2035ta 8:1b6fc10c5cea 132 }
ece2035ta 8:1b6fc10c5cea 133 return;
ece2035ta 8:1b6fc10c5cea 134 }