frogger game revision
Dependencies: 4DGL-uLCD-SE MMA8452 SDFileSystem mbed wave_player
Fork of ECE2035_FroggerGame_SUM2015 by
main.cpp@9:47f07aa85f1a, 2016-02-01 (annotated)
- Committer:
- ssong86
- Date:
- Mon Feb 01 06:43:06 2016 +0000
- Revision:
- 9:47f07aa85f1a
- Parent:
- 5:52a01054e275
frogger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leuyentran | 0:7fe3c940e4b5 | 1 | // Include header files for platform |
leuyentran | 0:7fe3c940e4b5 | 2 | #include "mbed.h" |
leuyentran | 0:7fe3c940e4b5 | 3 | #include "wave_player.h" |
leuyentran | 0:7fe3c940e4b5 | 4 | #include "SDFileSystem.h" |
leuyentran | 0:7fe3c940e4b5 | 5 | #include "Shiftbrite.h" |
leuyentran | 0:7fe3c940e4b5 | 6 | #include <vector> |
ece2035ta | 3:3856a3dccf6d | 7 | #include "MMA8452.h" |
leuyentran | 0:7fe3c940e4b5 | 8 | |
ece2035ta | 5:52a01054e275 | 9 | // Include header files for roboFrogger project |
leuyentran | 0:7fe3c940e4b5 | 10 | #include "globals.h" |
leuyentran | 0:7fe3c940e4b5 | 11 | #include "map_public.h" |
leuyentran | 0:7fe3c940e4b5 | 12 | #include "robot.h" |
ssong86 | 9:47f07aa85f1a | 13 | #include "cars.h" |
ssong86 | 9:47f07aa85f1a | 14 | //#include "wave_player.h" |
ssong86 | 9:47f07aa85f1a | 15 | |
ssong86 | 9:47f07aa85f1a | 16 | void youwin(); |
ssong86 | 9:47f07aa85f1a | 17 | |
leuyentran | 0:7fe3c940e4b5 | 18 | |
leuyentran | 0:7fe3c940e4b5 | 19 | //Platform initialization |
ece2035ta | 4:3fb1d198e9d6 | 20 | DigitalIn right_pb(p24); // push bottom |
ece2035ta | 4:3fb1d198e9d6 | 21 | DigitalIn left_pb(p21); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 22 | DigitalIn up_pb(p22); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 23 | DigitalIn down_pb(p23); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 24 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) |
ece2035ta | 3:3856a3dccf6d | 25 | Serial pc(USBTX,USBRX); // used by Accelerometer |
ece2035ta | 3:3856a3dccf6d | 26 | MMA8452 acc(p28, p27, 100000); // Accelerometer |
leuyentran | 0:7fe3c940e4b5 | 27 | DigitalOut myled(LED1); |
ssong86 | 9:47f07aa85f1a | 28 | SDFileSystem sd(p5,p6,p7,p8,"sd"); |
ssong86 | 9:47f07aa85f1a | 29 | |
leuyentran | 0:7fe3c940e4b5 | 30 | |
leuyentran | 0:7fe3c940e4b5 | 31 | int main() |
leuyentran | 0:7fe3c940e4b5 | 32 | { |
leuyentran | 0:7fe3c940e4b5 | 33 | // Initialize the timer |
leuyentran | 0:7fe3c940e4b5 | 34 | /// [Example of time control implementation] |
leuyentran | 0:7fe3c940e4b5 | 35 | /// Here is a rough example to implement the timer control <br><br> |
leuyentran | 0:7fe3c940e4b5 | 36 | int tick, pre_tick; |
leuyentran | 0:7fe3c940e4b5 | 37 | srand (time(NULL)); |
leuyentran | 0:7fe3c940e4b5 | 38 | Timer timer; |
leuyentran | 0:7fe3c940e4b5 | 39 | timer.start(); |
leuyentran | 0:7fe3c940e4b5 | 40 | tick = timer.read_ms(); |
leuyentran | 0:7fe3c940e4b5 | 41 | pre_tick = tick; |
ssong86 | 9:47f07aa85f1a | 42 | |
ssong86 | 9:47f07aa85f1a | 43 | double score=0; |
ssong86 | 9:47f07aa85f1a | 44 | |
leuyentran | 0:7fe3c940e4b5 | 45 | // Initialize the buttons |
leuyentran | 0:7fe3c940e4b5 | 46 | left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed |
leuyentran | 0:7fe3c940e4b5 | 47 | right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed |
leuyentran | 0:7fe3c940e4b5 | 48 | up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
leuyentran | 0:7fe3c940e4b5 | 49 | down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
ssong86 | 9:47f07aa85f1a | 50 | |
ssong86 | 9:47f07aa85f1a | 51 | |
leuyentran | 0:7fe3c940e4b5 | 52 | |
leuyentran | 0:7fe3c940e4b5 | 53 | /// [Example of the game control implementation] |
leuyentran | 0:7fe3c940e4b5 | 54 | /// Here is the example to initialize the game <br><br> |
leuyentran | 0:7fe3c940e4b5 | 55 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 56 | //map_init(); |
ssong86 | 9:47f07aa85f1a | 57 | //robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 58 | |
ece2035ta | 3:3856a3dccf6d | 59 | double x_, y_, z_; |
leuyentran | 0:7fe3c940e4b5 | 60 | double x=0; |
leuyentran | 0:7fe3c940e4b5 | 61 | double y=8; |
ssong86 | 9:47f07aa85f1a | 62 | |
ssong86 | 9:47f07aa85f1a | 63 | int life = 3; |
ssong86 | 9:47f07aa85f1a | 64 | |
ssong86 | 9:47f07aa85f1a | 65 | |
ssong86 | 9:47f07aa85f1a | 66 | int level=0; |
ssong86 | 9:47f07aa85f1a | 67 | |
ssong86 | 9:47f07aa85f1a | 68 | |
ssong86 | 9:47f07aa85f1a | 69 | //Start Screen |
ssong86 | 9:47f07aa85f1a | 70 | uLCD.locate(0,0); |
ssong86 | 9:47f07aa85f1a | 71 | uLCD.printf("RoboFrog"); |
ssong86 | 9:47f07aa85f1a | 72 | uLCD.locate(0,2); |
ssong86 | 9:47f07aa85f1a | 73 | uLCD.printf("Press UP"); |
ssong86 | 9:47f07aa85f1a | 74 | uLCD.locate(0,3); |
ssong86 | 9:47f07aa85f1a | 75 | uLCD.printf("->Level 1"); |
ssong86 | 9:47f07aa85f1a | 76 | uLCD.locate(0,5); |
ssong86 | 9:47f07aa85f1a | 77 | uLCD.printf("Press LEFT"); |
ssong86 | 9:47f07aa85f1a | 78 | uLCD.locate(0,6); |
ssong86 | 9:47f07aa85f1a | 79 | uLCD.printf("->Level 2"); |
ssong86 | 9:47f07aa85f1a | 80 | uLCD.locate(0,8); |
ssong86 | 9:47f07aa85f1a | 81 | uLCD.printf("Press RIGHT"); |
ssong86 | 9:47f07aa85f1a | 82 | uLCD.locate(0,9); |
ssong86 | 9:47f07aa85f1a | 83 | uLCD.printf("->Level 3"); |
ssong86 | 9:47f07aa85f1a | 84 | |
ssong86 | 9:47f07aa85f1a | 85 | |
ssong86 | 9:47f07aa85f1a | 86 | while(1) |
ssong86 | 9:47f07aa85f1a | 87 | { |
ssong86 | 9:47f07aa85f1a | 88 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 89 | { |
ssong86 | 9:47f07aa85f1a | 90 | level=1; |
ssong86 | 9:47f07aa85f1a | 91 | break; |
ssong86 | 9:47f07aa85f1a | 92 | } |
ssong86 | 9:47f07aa85f1a | 93 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 94 | { |
ssong86 | 9:47f07aa85f1a | 95 | level=2; |
ssong86 | 9:47f07aa85f1a | 96 | break; |
ssong86 | 9:47f07aa85f1a | 97 | } |
ssong86 | 9:47f07aa85f1a | 98 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 99 | { |
ssong86 | 9:47f07aa85f1a | 100 | level=3; |
ssong86 | 9:47f07aa85f1a | 101 | break; |
ssong86 | 9:47f07aa85f1a | 102 | } |
ssong86 | 9:47f07aa85f1a | 103 | } |
ssong86 | 9:47f07aa85f1a | 104 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 105 | if(level==1) |
ssong86 | 9:47f07aa85f1a | 106 | { |
leuyentran | 0:7fe3c940e4b5 | 107 | /// 1. Begin the game loop |
ssong86 | 9:47f07aa85f1a | 108 | |
ssong86 | 9:47f07aa85f1a | 109 | uLCD.printf("Level 1 Start!"); |
ssong86 | 9:47f07aa85f1a | 110 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 111 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 112 | map_init(); |
ssong86 | 9:47f07aa85f1a | 113 | robot_draw(16,0); |
ssong86 | 9:47f07aa85f1a | 114 | robot_draw(15,0); |
ssong86 | 9:47f07aa85f1a | 115 | robot_draw(14,0); |
ssong86 | 9:47f07aa85f1a | 116 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 117 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 118 | //redCar |
ssong86 | 9:47f07aa85f1a | 119 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 120 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 121 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 122 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 123 | //blueCar |
ssong86 | 9:47f07aa85f1a | 124 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 125 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 126 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 127 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 128 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 129 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 130 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 131 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 132 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 133 | |
ssong86 | 9:47f07aa85f1a | 134 | |
ssong86 | 9:47f07aa85f1a | 135 | while(1) |
ssong86 | 9:47f07aa85f1a | 136 | { |
leuyentran | 0:7fe3c940e4b5 | 137 | tick = timer.read_ms(); // Read current time |
ece2035ta | 5:52a01054e275 | 138 | /// 2. Implement the code to get user input and update the roboFrogger |
ece2035ta | 4:3fb1d198e9d6 | 139 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ece2035ta | 3:3856a3dccf6d | 140 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 141 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 142 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 143 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 144 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 145 | |
ssong86 | 9:47f07aa85f1a | 146 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 147 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 148 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 149 | |
ssong86 | 9:47f07aa85f1a | 150 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 151 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 152 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 153 | |
ssong86 | 9:47f07aa85f1a | 154 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 155 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 156 | { |
ssong86 | 9:47f07aa85f1a | 157 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 158 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 159 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 160 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 161 | |
ssong86 | 9:47f07aa85f1a | 162 | } |
ssong86 | 9:47f07aa85f1a | 163 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 164 | { |
ssong86 | 9:47f07aa85f1a | 165 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 166 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 167 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 168 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 169 | } |
ssong86 | 9:47f07aa85f1a | 170 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 171 | { |
ssong86 | 9:47f07aa85f1a | 172 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 173 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 174 | { |
ssong86 | 9:47f07aa85f1a | 175 | y = 0; |
ssong86 | 9:47f07aa85f1a | 176 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 177 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 178 | } |
ssong86 | 9:47f07aa85f1a | 179 | |
ssong86 | 9:47f07aa85f1a | 180 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 181 | { |
ssong86 | 9:47f07aa85f1a | 182 | y= 15; |
ssong86 | 9:47f07aa85f1a | 183 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 184 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 185 | } |
ssong86 | 9:47f07aa85f1a | 186 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 187 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 188 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 189 | } |
ssong86 | 9:47f07aa85f1a | 190 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 191 | { |
ssong86 | 9:47f07aa85f1a | 192 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 193 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 194 | { |
ssong86 | 9:47f07aa85f1a | 195 | y = 0; |
ssong86 | 9:47f07aa85f1a | 196 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 197 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 198 | } |
ssong86 | 9:47f07aa85f1a | 199 | |
ssong86 | 9:47f07aa85f1a | 200 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 201 | { |
ssong86 | 9:47f07aa85f1a | 202 | y=15; |
ssong86 | 9:47f07aa85f1a | 203 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 204 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 205 | } |
ssong86 | 9:47f07aa85f1a | 206 | |
ssong86 | 9:47f07aa85f1a | 207 | |
ssong86 | 9:47f07aa85f1a | 208 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 209 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 210 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 211 | } |
ssong86 | 9:47f07aa85f1a | 212 | |
ssong86 | 9:47f07aa85f1a | 213 | |
ssong86 | 9:47f07aa85f1a | 214 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 215 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 216 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 217 | { |
ssong86 | 9:47f07aa85f1a | 218 | life--; |
ssong86 | 9:47f07aa85f1a | 219 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 220 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 221 | x=0; |
ssong86 | 9:47f07aa85f1a | 222 | y=8; |
ssong86 | 9:47f07aa85f1a | 223 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 224 | } |
ssong86 | 9:47f07aa85f1a | 225 | |
ssong86 | 9:47f07aa85f1a | 226 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 227 | collision(); |
ssong86 | 9:47f07aa85f1a | 228 | |
ssong86 | 9:47f07aa85f1a | 229 | score = map_eat_candy(x,y)+score;//saving scores |
leuyentran | 0:7fe3c940e4b5 | 230 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 231 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 232 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 233 | uLCD.printf(" Life: "); |
ssong86 | 9:47f07aa85f1a | 234 | |
ssong86 | 9:47f07aa85f1a | 235 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 236 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 237 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 238 | { |
ssong86 | 9:47f07aa85f1a | 239 | // |
ssong86 | 9:47f07aa85f1a | 240 | //lv2 |
ssong86 | 9:47f07aa85f1a | 241 | x=0; |
ssong86 | 9:47f07aa85f1a | 242 | y=8; |
ssong86 | 9:47f07aa85f1a | 243 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 244 | uLCD.printf("Level 2 Start!"); |
ssong86 | 9:47f07aa85f1a | 245 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 246 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 247 | map_init(); |
ssong86 | 9:47f07aa85f1a | 248 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 249 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 250 | //redCar |
ssong86 | 9:47f07aa85f1a | 251 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 252 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 253 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 254 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 255 | //blueCar |
ssong86 | 9:47f07aa85f1a | 256 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 257 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 258 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 259 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 260 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 261 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 262 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 263 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 264 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 265 | //yellowCar |
ssong86 | 9:47f07aa85f1a | 266 | car_t yellowCar; |
ssong86 | 9:47f07aa85f1a | 267 | yellowCar.lane=4; |
ssong86 | 9:47f07aa85f1a | 268 | yellowCar.car_color=0xFFFF00; |
ssong86 | 9:47f07aa85f1a | 269 | car_init(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 270 | |
ssong86 | 9:47f07aa85f1a | 271 | |
ssong86 | 9:47f07aa85f1a | 272 | while(1) { |
ssong86 | 9:47f07aa85f1a | 273 | tick = timer.read_ms(); // Read current time |
ssong86 | 9:47f07aa85f1a | 274 | /// 2. Implement the code to get user input and update the roboFrogger |
ssong86 | 9:47f07aa85f1a | 275 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ssong86 | 9:47f07aa85f1a | 276 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 277 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 278 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 279 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 280 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 281 | |
ssong86 | 9:47f07aa85f1a | 282 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 283 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 284 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 285 | |
ssong86 | 9:47f07aa85f1a | 286 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 287 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 288 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 289 | |
ssong86 | 9:47f07aa85f1a | 290 | car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 291 | yellowCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 292 | drive(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 293 | |
ssong86 | 9:47f07aa85f1a | 294 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 295 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 296 | { |
ssong86 | 9:47f07aa85f1a | 297 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 298 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 299 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 300 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 301 | } |
ssong86 | 9:47f07aa85f1a | 302 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 303 | { |
ssong86 | 9:47f07aa85f1a | 304 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 305 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 306 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 307 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 308 | } |
ssong86 | 9:47f07aa85f1a | 309 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 310 | { |
ssong86 | 9:47f07aa85f1a | 311 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 312 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 313 | { |
ssong86 | 9:47f07aa85f1a | 314 | y = 0; |
ssong86 | 9:47f07aa85f1a | 315 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 316 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 317 | } |
ssong86 | 9:47f07aa85f1a | 318 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 319 | { |
ssong86 | 9:47f07aa85f1a | 320 | y= 15; |
ssong86 | 9:47f07aa85f1a | 321 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 322 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 323 | } |
ssong86 | 9:47f07aa85f1a | 324 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 325 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 326 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 327 | } |
ssong86 | 9:47f07aa85f1a | 328 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 329 | { |
ssong86 | 9:47f07aa85f1a | 330 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 331 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 332 | { |
ssong86 | 9:47f07aa85f1a | 333 | y = 0; |
ssong86 | 9:47f07aa85f1a | 334 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 335 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 336 | } |
ssong86 | 9:47f07aa85f1a | 337 | |
ssong86 | 9:47f07aa85f1a | 338 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 339 | { |
ssong86 | 9:47f07aa85f1a | 340 | y= 15; |
ssong86 | 9:47f07aa85f1a | 341 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 342 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 343 | } |
ece2035ta | 3:3856a3dccf6d | 344 | |
ssong86 | 9:47f07aa85f1a | 345 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 346 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 347 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 348 | } |
ssong86 | 9:47f07aa85f1a | 349 | |
ssong86 | 9:47f07aa85f1a | 350 | |
ssong86 | 9:47f07aa85f1a | 351 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 352 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 353 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 354 | (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 355 | { |
ssong86 | 9:47f07aa85f1a | 356 | life--; |
ssong86 | 9:47f07aa85f1a | 357 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 358 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 359 | x=0; |
ssong86 | 9:47f07aa85f1a | 360 | y=8; |
ssong86 | 9:47f07aa85f1a | 361 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 362 | } |
ssong86 | 9:47f07aa85f1a | 363 | |
ssong86 | 9:47f07aa85f1a | 364 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 365 | collision(); |
ssong86 | 9:47f07aa85f1a | 366 | |
ssong86 | 9:47f07aa85f1a | 367 | score = map_eat_candy(x,y)+score; |
ssong86 | 9:47f07aa85f1a | 368 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 369 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 370 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 371 | uLCD.printf(" Life:%d",life); |
ssong86 | 9:47f07aa85f1a | 372 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 373 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 374 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 375 | { |
ssong86 | 9:47f07aa85f1a | 376 | ////// |
ssong86 | 9:47f07aa85f1a | 377 | //lv3 |
ssong86 | 9:47f07aa85f1a | 378 | x=0; |
ssong86 | 9:47f07aa85f1a | 379 | y=8; |
ssong86 | 9:47f07aa85f1a | 380 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 381 | uLCD.printf("Level 3 Start!"); |
ssong86 | 9:47f07aa85f1a | 382 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 383 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 384 | map_init(); |
ssong86 | 9:47f07aa85f1a | 385 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 386 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 387 | //redCar |
ssong86 | 9:47f07aa85f1a | 388 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 389 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 390 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 391 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 392 | //blueCar |
ssong86 | 9:47f07aa85f1a | 393 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 394 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 395 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 396 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 397 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 398 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 399 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 400 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 401 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 402 | //yellowCar |
ssong86 | 9:47f07aa85f1a | 403 | car_t yellowCar; |
ssong86 | 9:47f07aa85f1a | 404 | yellowCar.lane=4; |
ssong86 | 9:47f07aa85f1a | 405 | yellowCar.car_color=0xFFFF00; |
ssong86 | 9:47f07aa85f1a | 406 | car_init(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 407 | //orangeCar |
ssong86 | 9:47f07aa85f1a | 408 | car_t orangeCar; |
ssong86 | 9:47f07aa85f1a | 409 | orangeCar.lane=5; |
ssong86 | 9:47f07aa85f1a | 410 | orangeCar.car_color=0xFFA500; |
ssong86 | 9:47f07aa85f1a | 411 | car_init(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 412 | |
ssong86 | 9:47f07aa85f1a | 413 | |
ssong86 | 9:47f07aa85f1a | 414 | while(1) { |
ssong86 | 9:47f07aa85f1a | 415 | tick = timer.read_ms(); // Read current time |
ssong86 | 9:47f07aa85f1a | 416 | /// 2. Implement the code to get user input and update the roboFrogger |
ssong86 | 9:47f07aa85f1a | 417 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ssong86 | 9:47f07aa85f1a | 418 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 419 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 420 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 421 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 422 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 423 | |
ssong86 | 9:47f07aa85f1a | 424 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 425 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 426 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 427 | |
ssong86 | 9:47f07aa85f1a | 428 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 429 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 430 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 431 | |
ssong86 | 9:47f07aa85f1a | 432 | car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 433 | yellowCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 434 | drive(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 435 | |
ssong86 | 9:47f07aa85f1a | 436 | car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 437 | orangeCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 438 | drive(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 439 | |
ssong86 | 9:47f07aa85f1a | 440 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 441 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 442 | { |
ssong86 | 9:47f07aa85f1a | 443 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 444 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 445 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 446 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 447 | } |
ssong86 | 9:47f07aa85f1a | 448 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 449 | { |
ssong86 | 9:47f07aa85f1a | 450 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 451 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 452 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 453 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 454 | } |
ssong86 | 9:47f07aa85f1a | 455 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 456 | { |
ssong86 | 9:47f07aa85f1a | 457 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 458 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 459 | { |
ssong86 | 9:47f07aa85f1a | 460 | y = 0; |
ssong86 | 9:47f07aa85f1a | 461 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 462 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 463 | } |
ssong86 | 9:47f07aa85f1a | 464 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 465 | { |
ssong86 | 9:47f07aa85f1a | 466 | y= 15; |
ssong86 | 9:47f07aa85f1a | 467 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 468 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 469 | } |
ssong86 | 9:47f07aa85f1a | 470 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 471 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 472 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 473 | } |
ssong86 | 9:47f07aa85f1a | 474 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 475 | { |
ssong86 | 9:47f07aa85f1a | 476 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 477 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 478 | { |
ssong86 | 9:47f07aa85f1a | 479 | y = 0; |
ssong86 | 9:47f07aa85f1a | 480 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 481 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 482 | } |
ssong86 | 9:47f07aa85f1a | 483 | |
ssong86 | 9:47f07aa85f1a | 484 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 485 | { |
ssong86 | 9:47f07aa85f1a | 486 | y= 15; |
ssong86 | 9:47f07aa85f1a | 487 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 488 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 489 | } |
ssong86 | 9:47f07aa85f1a | 490 | |
ssong86 | 9:47f07aa85f1a | 491 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 492 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 493 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 494 | } |
ssong86 | 9:47f07aa85f1a | 495 | |
ssong86 | 9:47f07aa85f1a | 496 | |
ssong86 | 9:47f07aa85f1a | 497 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 498 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 499 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 500 | (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 501 | (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 502 | { |
ssong86 | 9:47f07aa85f1a | 503 | life--; |
ssong86 | 9:47f07aa85f1a | 504 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 505 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 506 | x=0; |
ssong86 | 9:47f07aa85f1a | 507 | y=8; |
ssong86 | 9:47f07aa85f1a | 508 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 509 | } |
ssong86 | 9:47f07aa85f1a | 510 | |
ssong86 | 9:47f07aa85f1a | 511 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 512 | collision(); |
ssong86 | 9:47f07aa85f1a | 513 | |
ssong86 | 9:47f07aa85f1a | 514 | score = map_eat_candy(x,y)+score; |
ssong86 | 9:47f07aa85f1a | 515 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 516 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 517 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 518 | uLCD.printf(" Life:%d",life); |
ssong86 | 9:47f07aa85f1a | 519 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 520 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 521 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 522 | { |
ssong86 | 9:47f07aa85f1a | 523 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 524 | uLCD.locate(6,8); |
ssong86 | 9:47f07aa85f1a | 525 | uLCD.printf("You Win"); |
ssong86 | 9:47f07aa85f1a | 526 | uLCD.locate(3,10); |
ssong86 | 9:47f07aa85f1a | 527 | uLCD.printf("Score is:%4.1f",score); |
ssong86 | 9:47f07aa85f1a | 528 | while(1) |
ssong86 | 9:47f07aa85f1a | 529 | {} |
ssong86 | 9:47f07aa85f1a | 530 | } |
ssong86 | 9:47f07aa85f1a | 531 | } |
ssong86 | 9:47f07aa85f1a | 532 | |
ssong86 | 9:47f07aa85f1a | 533 | ///// |
ssong86 | 9:47f07aa85f1a | 534 | } |
ssong86 | 9:47f07aa85f1a | 535 | } |
ssong86 | 9:47f07aa85f1a | 536 | |
ssong86 | 9:47f07aa85f1a | 537 | // |
ssong86 | 9:47f07aa85f1a | 538 | } |
ssong86 | 9:47f07aa85f1a | 539 | }//end while |
ssong86 | 9:47f07aa85f1a | 540 | }//end level==1 |
ssong86 | 9:47f07aa85f1a | 541 | if(level==2) |
ssong86 | 9:47f07aa85f1a | 542 | { |
ssong86 | 9:47f07aa85f1a | 543 | //// |
ssong86 | 9:47f07aa85f1a | 544 | //lv2 |
ssong86 | 9:47f07aa85f1a | 545 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 546 | uLCD.printf("Level 2 Start!"); |
ssong86 | 9:47f07aa85f1a | 547 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 548 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 549 | map_init(); |
ssong86 | 9:47f07aa85f1a | 550 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 551 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 552 | //redCar |
ssong86 | 9:47f07aa85f1a | 553 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 554 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 555 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 556 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 557 | //blueCar |
ssong86 | 9:47f07aa85f1a | 558 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 559 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 560 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 561 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 562 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 563 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 564 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 565 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 566 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 567 | //yellowCar |
ssong86 | 9:47f07aa85f1a | 568 | car_t yellowCar; |
ssong86 | 9:47f07aa85f1a | 569 | yellowCar.lane=4; |
ssong86 | 9:47f07aa85f1a | 570 | yellowCar.car_color=0xFFFF00; |
ssong86 | 9:47f07aa85f1a | 571 | car_init(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 572 | |
ssong86 | 9:47f07aa85f1a | 573 | |
ssong86 | 9:47f07aa85f1a | 574 | while(1) { |
ssong86 | 9:47f07aa85f1a | 575 | tick = timer.read_ms(); // Read current time |
ssong86 | 9:47f07aa85f1a | 576 | /// 2. Implement the code to get user input and update the roboFrogger |
ssong86 | 9:47f07aa85f1a | 577 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ssong86 | 9:47f07aa85f1a | 578 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 579 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 580 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 581 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 582 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 583 | |
ssong86 | 9:47f07aa85f1a | 584 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 585 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 586 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 587 | |
ssong86 | 9:47f07aa85f1a | 588 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 589 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 590 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 591 | |
ssong86 | 9:47f07aa85f1a | 592 | car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 593 | yellowCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 594 | drive(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 595 | |
ssong86 | 9:47f07aa85f1a | 596 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 597 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 598 | { |
ssong86 | 9:47f07aa85f1a | 599 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 600 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 601 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 602 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 603 | } |
ssong86 | 9:47f07aa85f1a | 604 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 605 | { |
ssong86 | 9:47f07aa85f1a | 606 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 607 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 608 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 609 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 610 | } |
ssong86 | 9:47f07aa85f1a | 611 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 612 | { |
ssong86 | 9:47f07aa85f1a | 613 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 614 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 615 | { |
ssong86 | 9:47f07aa85f1a | 616 | y = 0; |
ssong86 | 9:47f07aa85f1a | 617 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 618 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 619 | } |
ssong86 | 9:47f07aa85f1a | 620 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 621 | { |
ssong86 | 9:47f07aa85f1a | 622 | y= 15; |
ssong86 | 9:47f07aa85f1a | 623 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 624 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 625 | } |
ssong86 | 9:47f07aa85f1a | 626 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 627 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 628 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 629 | } |
ssong86 | 9:47f07aa85f1a | 630 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 631 | { |
ssong86 | 9:47f07aa85f1a | 632 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 633 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 634 | { |
ssong86 | 9:47f07aa85f1a | 635 | y = 0; |
ssong86 | 9:47f07aa85f1a | 636 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 637 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 638 | } |
ssong86 | 9:47f07aa85f1a | 639 | |
ssong86 | 9:47f07aa85f1a | 640 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 641 | { |
ssong86 | 9:47f07aa85f1a | 642 | y= 15; |
ssong86 | 9:47f07aa85f1a | 643 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 644 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 645 | } |
ssong86 | 9:47f07aa85f1a | 646 | |
ssong86 | 9:47f07aa85f1a | 647 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 648 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 649 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 650 | } |
ssong86 | 9:47f07aa85f1a | 651 | |
ssong86 | 9:47f07aa85f1a | 652 | |
ssong86 | 9:47f07aa85f1a | 653 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 654 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 655 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 656 | (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 657 | { |
ssong86 | 9:47f07aa85f1a | 658 | life--; |
ssong86 | 9:47f07aa85f1a | 659 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 660 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 661 | x=0; |
ssong86 | 9:47f07aa85f1a | 662 | y=8; |
ssong86 | 9:47f07aa85f1a | 663 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 664 | } |
ssong86 | 9:47f07aa85f1a | 665 | |
ssong86 | 9:47f07aa85f1a | 666 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 667 | collision(); |
ssong86 | 9:47f07aa85f1a | 668 | |
ssong86 | 9:47f07aa85f1a | 669 | score = map_eat_candy(x,y)+score; |
ssong86 | 9:47f07aa85f1a | 670 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 671 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 672 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 673 | uLCD.printf(" Life:%d",life); |
ssong86 | 9:47f07aa85f1a | 674 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 675 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 676 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 677 | { |
ssong86 | 9:47f07aa85f1a | 678 | //lv3 |
ssong86 | 9:47f07aa85f1a | 679 | x=0; |
ssong86 | 9:47f07aa85f1a | 680 | y=8; |
ssong86 | 9:47f07aa85f1a | 681 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 682 | uLCD.printf("Level 3 Start!"); |
ssong86 | 9:47f07aa85f1a | 683 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 684 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 685 | map_init(); |
ssong86 | 9:47f07aa85f1a | 686 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 687 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 688 | //redCar |
ssong86 | 9:47f07aa85f1a | 689 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 690 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 691 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 692 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 693 | //blueCar |
ssong86 | 9:47f07aa85f1a | 694 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 695 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 696 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 697 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 698 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 699 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 700 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 701 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 702 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 703 | //yellowCar |
ssong86 | 9:47f07aa85f1a | 704 | car_t yellowCar; |
ssong86 | 9:47f07aa85f1a | 705 | yellowCar.lane=4; |
ssong86 | 9:47f07aa85f1a | 706 | yellowCar.car_color=0xFFFF00; |
ssong86 | 9:47f07aa85f1a | 707 | car_init(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 708 | //orangeCar |
ssong86 | 9:47f07aa85f1a | 709 | car_t orangeCar; |
ssong86 | 9:47f07aa85f1a | 710 | orangeCar.lane=5; |
ssong86 | 9:47f07aa85f1a | 711 | orangeCar.car_color=0xFFA500; |
ssong86 | 9:47f07aa85f1a | 712 | car_init(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 713 | |
ssong86 | 9:47f07aa85f1a | 714 | |
ssong86 | 9:47f07aa85f1a | 715 | while(1) { |
ssong86 | 9:47f07aa85f1a | 716 | tick = timer.read_ms(); // Read current time |
ssong86 | 9:47f07aa85f1a | 717 | /// 2. Implement the code to get user input and update the roboFrogger |
ssong86 | 9:47f07aa85f1a | 718 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ssong86 | 9:47f07aa85f1a | 719 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 720 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 721 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 722 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 723 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 724 | |
ssong86 | 9:47f07aa85f1a | 725 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 726 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 727 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 728 | |
ssong86 | 9:47f07aa85f1a | 729 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 730 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 731 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 732 | |
ssong86 | 9:47f07aa85f1a | 733 | car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 734 | yellowCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 735 | drive(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 736 | |
ssong86 | 9:47f07aa85f1a | 737 | car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 738 | orangeCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 739 | drive(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 740 | |
ssong86 | 9:47f07aa85f1a | 741 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 742 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 743 | { |
ssong86 | 9:47f07aa85f1a | 744 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 745 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 746 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 747 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 748 | } |
ssong86 | 9:47f07aa85f1a | 749 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 750 | { |
ssong86 | 9:47f07aa85f1a | 751 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 752 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 753 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 754 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 755 | } |
ssong86 | 9:47f07aa85f1a | 756 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 757 | { |
ssong86 | 9:47f07aa85f1a | 758 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 759 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 760 | { |
ssong86 | 9:47f07aa85f1a | 761 | y = 0; |
ssong86 | 9:47f07aa85f1a | 762 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 763 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 764 | } |
ssong86 | 9:47f07aa85f1a | 765 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 766 | { |
ssong86 | 9:47f07aa85f1a | 767 | y= 15; |
ssong86 | 9:47f07aa85f1a | 768 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 769 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 770 | } |
ssong86 | 9:47f07aa85f1a | 771 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 772 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 773 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 774 | } |
ssong86 | 9:47f07aa85f1a | 775 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 776 | { |
ssong86 | 9:47f07aa85f1a | 777 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 778 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 779 | { |
ssong86 | 9:47f07aa85f1a | 780 | y = 0; |
ssong86 | 9:47f07aa85f1a | 781 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 782 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 783 | } |
ssong86 | 9:47f07aa85f1a | 784 | |
ssong86 | 9:47f07aa85f1a | 785 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 786 | { |
ssong86 | 9:47f07aa85f1a | 787 | y= 15; |
ssong86 | 9:47f07aa85f1a | 788 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 789 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 790 | } |
ssong86 | 9:47f07aa85f1a | 791 | |
ssong86 | 9:47f07aa85f1a | 792 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 793 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 794 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 795 | } |
ssong86 | 9:47f07aa85f1a | 796 | |
ssong86 | 9:47f07aa85f1a | 797 | |
ssong86 | 9:47f07aa85f1a | 798 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 799 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 800 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 801 | (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 802 | (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 803 | { |
ssong86 | 9:47f07aa85f1a | 804 | life--; |
ssong86 | 9:47f07aa85f1a | 805 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 806 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 807 | x=0; |
ssong86 | 9:47f07aa85f1a | 808 | y=8; |
ssong86 | 9:47f07aa85f1a | 809 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 810 | } |
ssong86 | 9:47f07aa85f1a | 811 | |
ssong86 | 9:47f07aa85f1a | 812 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 813 | collision(); |
ssong86 | 9:47f07aa85f1a | 814 | |
ssong86 | 9:47f07aa85f1a | 815 | score = map_eat_candy(x,y)+score; |
ssong86 | 9:47f07aa85f1a | 816 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 817 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 818 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 819 | uLCD.printf(" Life:%d",life); |
ssong86 | 9:47f07aa85f1a | 820 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 821 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 822 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 823 | { |
ssong86 | 9:47f07aa85f1a | 824 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 825 | uLCD.locate(6,8); |
ssong86 | 9:47f07aa85f1a | 826 | uLCD.printf("You Win"); |
ssong86 | 9:47f07aa85f1a | 827 | uLCD.locate(3,10); |
ssong86 | 9:47f07aa85f1a | 828 | uLCD.printf("Score is:%4.1f",score); |
ssong86 | 9:47f07aa85f1a | 829 | while(1) |
ssong86 | 9:47f07aa85f1a | 830 | {} |
ssong86 | 9:47f07aa85f1a | 831 | } |
ssong86 | 9:47f07aa85f1a | 832 | } |
ssong86 | 9:47f07aa85f1a | 833 | } |
ssong86 | 9:47f07aa85f1a | 834 | } |
ssong86 | 9:47f07aa85f1a | 835 | //// |
ssong86 | 9:47f07aa85f1a | 836 | } |
ssong86 | 9:47f07aa85f1a | 837 | |
ssong86 | 9:47f07aa85f1a | 838 | if(level==3) |
ssong86 | 9:47f07aa85f1a | 839 | { |
ssong86 | 9:47f07aa85f1a | 840 | |
ssong86 | 9:47f07aa85f1a | 841 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 842 | uLCD.printf("Level 3 Start!"); |
ssong86 | 9:47f07aa85f1a | 843 | wait(0.5); |
ssong86 | 9:47f07aa85f1a | 844 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 845 | map_init(); |
ssong86 | 9:47f07aa85f1a | 846 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 847 | //level 1 car objects |
ssong86 | 9:47f07aa85f1a | 848 | //redCar |
ssong86 | 9:47f07aa85f1a | 849 | car_t redCar; |
ssong86 | 9:47f07aa85f1a | 850 | redCar.lane=1; |
ssong86 | 9:47f07aa85f1a | 851 | redCar.car_color=0xFF0000; |
ssong86 | 9:47f07aa85f1a | 852 | car_init(&redCar); |
ssong86 | 9:47f07aa85f1a | 853 | //blueCar |
ssong86 | 9:47f07aa85f1a | 854 | car_t blueCar; |
ssong86 | 9:47f07aa85f1a | 855 | blueCar.lane=2; |
ssong86 | 9:47f07aa85f1a | 856 | blueCar.car_color=0x0000FF; |
ssong86 | 9:47f07aa85f1a | 857 | car_init(&blueCar); |
ssong86 | 9:47f07aa85f1a | 858 | //whiteCar |
ssong86 | 9:47f07aa85f1a | 859 | car_t whiteCar; |
ssong86 | 9:47f07aa85f1a | 860 | whiteCar.lane=3; |
ssong86 | 9:47f07aa85f1a | 861 | whiteCar.car_color=0xFFFFFF; |
ssong86 | 9:47f07aa85f1a | 862 | car_init(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 863 | //yellowCar |
ssong86 | 9:47f07aa85f1a | 864 | car_t yellowCar; |
ssong86 | 9:47f07aa85f1a | 865 | yellowCar.lane=4; |
ssong86 | 9:47f07aa85f1a | 866 | yellowCar.car_color=0xFFFF00; |
ssong86 | 9:47f07aa85f1a | 867 | car_init(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 868 | //orangeCar |
ssong86 | 9:47f07aa85f1a | 869 | car_t orangeCar; |
ssong86 | 9:47f07aa85f1a | 870 | orangeCar.lane=5; |
ssong86 | 9:47f07aa85f1a | 871 | orangeCar.car_color=0xFFA500; |
ssong86 | 9:47f07aa85f1a | 872 | car_init(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 873 | |
ssong86 | 9:47f07aa85f1a | 874 | |
ssong86 | 9:47f07aa85f1a | 875 | while(1) { |
ssong86 | 9:47f07aa85f1a | 876 | tick = timer.read_ms(); // Read current time |
ssong86 | 9:47f07aa85f1a | 877 | /// 2. Implement the code to get user input and update the roboFrogger |
ssong86 | 9:47f07aa85f1a | 878 | /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> |
ssong86 | 9:47f07aa85f1a | 879 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
ssong86 | 9:47f07aa85f1a | 880 | //car objects movemetn |
ssong86 | 9:47f07aa85f1a | 881 | car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 882 | redCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 883 | drive(&redCar); |
ssong86 | 9:47f07aa85f1a | 884 | |
ssong86 | 9:47f07aa85f1a | 885 | car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 886 | blueCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 887 | drive(&blueCar); |
ssong86 | 9:47f07aa85f1a | 888 | |
ssong86 | 9:47f07aa85f1a | 889 | car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 890 | whiteCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 891 | drive(&whiteCar); |
ssong86 | 9:47f07aa85f1a | 892 | |
ssong86 | 9:47f07aa85f1a | 893 | car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 894 | yellowCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 895 | drive(&yellowCar); |
ssong86 | 9:47f07aa85f1a | 896 | |
ssong86 | 9:47f07aa85f1a | 897 | car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); |
ssong86 | 9:47f07aa85f1a | 898 | orangeCar.car_motion = CAR_UP; |
ssong86 | 9:47f07aa85f1a | 899 | drive(&orangeCar); |
ssong86 | 9:47f07aa85f1a | 900 | |
ssong86 | 9:47f07aa85f1a | 901 | //push button input and robot movement |
ssong86 | 9:47f07aa85f1a | 902 | if(!left_pb) |
ssong86 | 9:47f07aa85f1a | 903 | { |
ssong86 | 9:47f07aa85f1a | 904 | x=x-1; |
ssong86 | 9:47f07aa85f1a | 905 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 906 | robot_clear(x+1,y); |
ssong86 | 9:47f07aa85f1a | 907 | map_draw_grid(x+1,y); |
ssong86 | 9:47f07aa85f1a | 908 | } |
ssong86 | 9:47f07aa85f1a | 909 | if(!right_pb) |
ssong86 | 9:47f07aa85f1a | 910 | { |
ssong86 | 9:47f07aa85f1a | 911 | x=x+1; |
ssong86 | 9:47f07aa85f1a | 912 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 913 | robot_clear(x-1,y); |
ssong86 | 9:47f07aa85f1a | 914 | map_draw_grid(x-1,y); |
ssong86 | 9:47f07aa85f1a | 915 | } |
ssong86 | 9:47f07aa85f1a | 916 | if(!up_pb) |
ssong86 | 9:47f07aa85f1a | 917 | { |
ssong86 | 9:47f07aa85f1a | 918 | y=y-1; |
ssong86 | 9:47f07aa85f1a | 919 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 920 | { |
ssong86 | 9:47f07aa85f1a | 921 | y = 0; |
ssong86 | 9:47f07aa85f1a | 922 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 923 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 924 | } |
ssong86 | 9:47f07aa85f1a | 925 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 926 | { |
ssong86 | 9:47f07aa85f1a | 927 | y= 15; |
ssong86 | 9:47f07aa85f1a | 928 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 929 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 930 | } |
ssong86 | 9:47f07aa85f1a | 931 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 932 | robot_clear(x,y+1); |
ssong86 | 9:47f07aa85f1a | 933 | map_draw_grid(x,y+1); |
ssong86 | 9:47f07aa85f1a | 934 | } |
ssong86 | 9:47f07aa85f1a | 935 | if(!down_pb) |
ssong86 | 9:47f07aa85f1a | 936 | { |
ssong86 | 9:47f07aa85f1a | 937 | y=y+1; |
ssong86 | 9:47f07aa85f1a | 938 | if(y>15) |
ssong86 | 9:47f07aa85f1a | 939 | { |
ssong86 | 9:47f07aa85f1a | 940 | y = 0; |
ssong86 | 9:47f07aa85f1a | 941 | robot_clear(x,15); |
ssong86 | 9:47f07aa85f1a | 942 | map_draw_grid(x,15); |
ssong86 | 9:47f07aa85f1a | 943 | } |
ssong86 | 9:47f07aa85f1a | 944 | |
ssong86 | 9:47f07aa85f1a | 945 | if(y<0) |
ssong86 | 9:47f07aa85f1a | 946 | { |
ssong86 | 9:47f07aa85f1a | 947 | y= 15; |
ssong86 | 9:47f07aa85f1a | 948 | robot_clear(x,0); |
ssong86 | 9:47f07aa85f1a | 949 | map_draw_grid(x,0); |
ssong86 | 9:47f07aa85f1a | 950 | } |
ssong86 | 9:47f07aa85f1a | 951 | |
ssong86 | 9:47f07aa85f1a | 952 | robot_draw(x,y); |
ssong86 | 9:47f07aa85f1a | 953 | robot_clear(x,y-1); |
ssong86 | 9:47f07aa85f1a | 954 | map_draw_grid(x,y-1); |
ssong86 | 9:47f07aa85f1a | 955 | } |
ssong86 | 9:47f07aa85f1a | 956 | |
ssong86 | 9:47f07aa85f1a | 957 | |
ssong86 | 9:47f07aa85f1a | 958 | if((x==redCar.car_blk_x && y==redCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 959 | (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 960 | (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 961 | (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || |
ssong86 | 9:47f07aa85f1a | 962 | (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) |
ssong86 | 9:47f07aa85f1a | 963 | { |
ssong86 | 9:47f07aa85f1a | 964 | life--; |
ssong86 | 9:47f07aa85f1a | 965 | robot_clear(x,y); |
ssong86 | 9:47f07aa85f1a | 966 | map_draw_grid(x,y); |
ssong86 | 9:47f07aa85f1a | 967 | x=0; |
ssong86 | 9:47f07aa85f1a | 968 | y=8; |
ssong86 | 9:47f07aa85f1a | 969 | robot_init(0,8); |
ssong86 | 9:47f07aa85f1a | 970 | } |
ssong86 | 9:47f07aa85f1a | 971 | |
ssong86 | 9:47f07aa85f1a | 972 | if(life==0) |
ssong86 | 9:47f07aa85f1a | 973 | collision(); |
ssong86 | 9:47f07aa85f1a | 974 | |
ssong86 | 9:47f07aa85f1a | 975 | score = map_eat_candy(x,y)+score; |
ssong86 | 9:47f07aa85f1a | 976 | uLCD.locate(0,1); |
ssong86 | 9:47f07aa85f1a | 977 | uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. |
ssong86 | 9:47f07aa85f1a | 978 | uLCD.locate(10,1); |
ssong86 | 9:47f07aa85f1a | 979 | uLCD.printf(" Life:%d",life); |
ssong86 | 9:47f07aa85f1a | 980 | if((tick-pre_tick)>500) // Time step control |
ssong86 | 9:47f07aa85f1a | 981 | pre_tick = tick; // update the previous tick |
ssong86 | 9:47f07aa85f1a | 982 | if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level |
ssong86 | 9:47f07aa85f1a | 983 | { |
ssong86 | 9:47f07aa85f1a | 984 | uLCD.cls(); |
ssong86 | 9:47f07aa85f1a | 985 | uLCD.locate(6,8); |
ssong86 | 9:47f07aa85f1a | 986 | uLCD.printf("You Win"); |
ssong86 | 9:47f07aa85f1a | 987 | uLCD.locate(3,10); |
ssong86 | 9:47f07aa85f1a | 988 | uLCD.printf("Score is:%4.1f",score); |
ssong86 | 9:47f07aa85f1a | 989 | while(1) |
ssong86 | 9:47f07aa85f1a | 990 | {} |
ssong86 | 9:47f07aa85f1a | 991 | } |
ssong86 | 9:47f07aa85f1a | 992 | |
ssong86 | 9:47f07aa85f1a | 993 | } |
ssong86 | 9:47f07aa85f1a | 994 | } |
ssong86 | 9:47f07aa85f1a | 995 | |
ssong86 | 9:47f07aa85f1a | 996 | }//end main |
leuyentran | 0:7fe3c940e4b5 | 997 | |
leuyentran | 0:7fe3c940e4b5 | 998 |