frogger game revision
Dependencies: 4DGL-uLCD-SE MMA8452 SDFileSystem mbed wave_player
Fork of ECE2035_FroggerGame_SUM2015 by
Revision 9:47f07aa85f1a, committed 2016-02-01
- Comitter:
- ssong86
- Date:
- Mon Feb 01 06:43:06 2016 +0000
- Parent:
- 8:1b6fc10c5cea
- Commit message:
- frogger
Changed in this revision
diff -r 1b6fc10c5cea -r 47f07aa85f1a SDFileSystem.lib --- a/SDFileSystem.lib Wed Jul 08 15:45:22 2015 +0000 +++ b/SDFileSystem.lib Mon Feb 01 06:43:06 2016 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/neilt6/code/SDFileSystem/#c2c1f0b16380 +http://developer.mbed.org/users/neilt6/code/SDFileSystem/#a47f74caa04e
diff -r 1b6fc10c5cea -r 47f07aa85f1a Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Mon Feb 01 06:43:06 2016 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// a new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 1b6fc10c5cea -r 47f07aa85f1a cars/cars.cpp --- a/cars/cars.cpp Wed Jul 08 15:45:22 2015 +0000 +++ b/cars/cars.cpp Mon Feb 01 06:43:06 2016 +0000 @@ -77,26 +77,42 @@ void car_init(car_t * g) { if(g->lane == 1) - { g->car_blk_x = 3; + { g->car_blk_x = 5; g->car_blk_y = 0; - drawCar(g, 3, 0); + drawCar(g, 5, 0); return; } if (g->lane == 2) { - g->car_blk_x = 7; + g->car_blk_x = 8; g->car_blk_y = 0; - drawCar(g, 7, 0); + drawCar(g, 8, 0); return; } if (g->lane == 3) { + g->car_blk_x = 11; + g->car_blk_y = 0; + drawCar(g, 11, 0); + return; + } + + if (g->lane == 4) + { + g->car_blk_x = 6; + g->car_blk_y = 0; + drawCar(g, 6, 0); + return; + } + + if (g->lane == 5) + { g->car_blk_x = 10; g->car_blk_y = 0; - drawCar(g, 10, 0); + drawCar(g,10,0); return; - } + } return; }
diff -r 1b6fc10c5cea -r 47f07aa85f1a main.cpp --- a/main.cpp Wed Jul 08 15:45:22 2015 +0000 +++ b/main.cpp Mon Feb 01 06:43:06 2016 +0000 @@ -10,6 +10,11 @@ #include "globals.h" #include "map_public.h" #include "robot.h" +#include "cars.h" +//#include "wave_player.h" + +void youwin(); + //Platform initialization DigitalIn right_pb(p24); // push bottom @@ -20,6 +25,8 @@ Serial pc(USBTX,USBRX); // used by Accelerometer MMA8452 acc(p28, p27, 100000); // Accelerometer DigitalOut myled(LED1); +SDFileSystem sd(p5,p6,p7,p8,"sd"); + int main() { @@ -32,50 +39,960 @@ timer.start(); tick = timer.read_ms(); pre_tick = tick; - + + double score=0; + // Initialize the buttons left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed - + + /// [Example of the game control implementation] /// Here is the example to initialize the game <br><br> uLCD.cls(); - map_init(); - robot_init(0,8); + //map_init(); + //robot_init(0,8); + double x_, y_, z_; double x=0; double y=8; - + + int life = 3; + + + int level=0; + + + //Start Screen + uLCD.locate(0,0); + uLCD.printf("RoboFrog"); + uLCD.locate(0,2); + uLCD.printf("Press UP"); + uLCD.locate(0,3); + uLCD.printf("->Level 1"); + uLCD.locate(0,5); + uLCD.printf("Press LEFT"); + uLCD.locate(0,6); + uLCD.printf("->Level 2"); + uLCD.locate(0,8); + uLCD.printf("Press RIGHT"); + uLCD.locate(0,9); + uLCD.printf("->Level 3"); + + + while(1) + { + if(!up_pb) + { + level=1; + break; + } + if(!left_pb) + { + level=2; + break; + } + if(!right_pb) + { + level=3; + break; + } + } + uLCD.cls(); + if(level==1) + { /// 1. Begin the game loop - while(1) { + + uLCD.printf("Level 1 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_draw(16,0); + robot_draw(15,0); + robot_draw(14,0); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + + + while(1) + { tick = timer.read_ms(); // Read current time - /// 2. Implement the code to get user input and update the roboFrogger /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> - - // [Some hints for user-input detection] acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y=15; + robot_clear(x,0); + map_draw_grid(x,0); + } + + + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score;//saving scores uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life: "); + + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + // + //lv2 + x=0; + y=8; + uLCD.cls(); + uLCD.printf("Level 2 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + //yellowCar + car_t yellowCar; + yellowCar.lane=4; + yellowCar.car_color=0xFFFF00; + car_init(&yellowCar); + + + while(1) { + tick = timer.read_ms(); // Read current time + /// 2. Implement the code to get user input and update the roboFrogger + /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> + acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); + yellowCar.car_motion = CAR_UP; + drive(&yellowCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } - uLCD.printf("Score x%4.1f ",1); //You could remove this code if you already make the accelerometer work. - /// -[Hint] Here is a simple way to utilize the readings of accelerometer: - /// If x is larger than certain value (ex:0.3), then make the roboFrogger move right. - /// If x<-0.3, then make it move left. <br> + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || + (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score; + uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life:%d",life); + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + ////// + //lv3 + x=0; + y=8; + uLCD.cls(); + uLCD.printf("Level 3 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + //yellowCar + car_t yellowCar; + yellowCar.lane=4; + yellowCar.car_color=0xFFFF00; + car_init(&yellowCar); + //orangeCar + car_t orangeCar; + orangeCar.lane=5; + orangeCar.car_color=0xFFA500; + car_init(&orangeCar); + + + while(1) { + tick = timer.read_ms(); // Read current time + /// 2. Implement the code to get user input and update the roboFrogger + /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> + acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); + yellowCar.car_motion = CAR_UP; + drive(&yellowCar); + + car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); + orangeCar.car_motion = CAR_UP; + drive(&orangeCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || + (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || + (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score; + uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life:%d",life); + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + uLCD.cls(); + uLCD.locate(6,8); + uLCD.printf("You Win"); + uLCD.locate(3,10); + uLCD.printf("Score is:%4.1f",score); + while(1) + {} + } + } + + ///// + } + } + + // + } + }//end while + }//end level==1 + if(level==2) + { + //// + //lv2 + uLCD.cls(); + uLCD.printf("Level 2 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + //yellowCar + car_t yellowCar; + yellowCar.lane=4; + yellowCar.car_color=0xFFFF00; + car_init(&yellowCar); + + + while(1) { + tick = timer.read_ms(); // Read current time + /// 2. Implement the code to get user input and update the roboFrogger + /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> + acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); + yellowCar.car_motion = CAR_UP; + drive(&yellowCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || + (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score; + uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life:%d",life); + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + //lv3 + x=0; + y=8; + uLCD.cls(); + uLCD.printf("Level 3 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + //yellowCar + car_t yellowCar; + yellowCar.lane=4; + yellowCar.car_color=0xFFFF00; + car_init(&yellowCar); + //orangeCar + car_t orangeCar; + orangeCar.lane=5; + orangeCar.car_color=0xFFA500; + car_init(&orangeCar); + + + while(1) { + tick = timer.read_ms(); // Read current time + /// 2. Implement the code to get user input and update the roboFrogger + /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> + acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); + yellowCar.car_motion = CAR_UP; + drive(&yellowCar); + + car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); + orangeCar.car_motion = CAR_UP; + drive(&orangeCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || + (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || + (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score; + uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life:%d",life); + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + uLCD.cls(); + uLCD.locate(6,8); + uLCD.printf("You Win"); + uLCD.locate(3,10); + uLCD.printf("Score is:%4.1f",score); + while(1) + {} + } + } + } + } + //// + } + + if(level==3) + { + + uLCD.cls(); + uLCD.printf("Level 3 Start!"); + wait(0.5); + uLCD.cls(); + map_init(); + robot_init(0,8); + //level 1 car objects + //redCar + car_t redCar; + redCar.lane=1; + redCar.car_color=0xFF0000; + car_init(&redCar); + //blueCar + car_t blueCar; + blueCar.lane=2; + blueCar.car_color=0x0000FF; + car_init(&blueCar); + //whiteCar + car_t whiteCar; + whiteCar.lane=3; + whiteCar.car_color=0xFFFFFF; + car_init(&whiteCar); + //yellowCar + car_t yellowCar; + yellowCar.lane=4; + yellowCar.car_color=0xFFFF00; + car_init(&yellowCar); + //orangeCar + car_t orangeCar; + orangeCar.lane=5; + orangeCar.car_color=0xFFA500; + car_init(&orangeCar); + + + while(1) { + tick = timer.read_ms(); // Read current time + /// 2. Implement the code to get user input and update the roboFrogger + /// -[Hint] Implement the code to move RoboFrogger. You could use either push-button or accelerometer. <br> + acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer + //car objects movemetn + car_move(&redCar,redCar.car_blk_x,redCar.car_blk_y); + redCar.car_motion = CAR_UP; + drive(&redCar); + + car_move(&blueCar,blueCar.car_blk_x,blueCar.car_blk_y); + blueCar.car_motion = CAR_UP; + drive(&blueCar); + + car_move(&whiteCar,whiteCar.car_blk_x,whiteCar.car_blk_y); + whiteCar.car_motion = CAR_UP; + drive(&whiteCar); + + car_move(&yellowCar,yellowCar.car_blk_x,yellowCar.car_blk_y); + yellowCar.car_motion = CAR_UP; + drive(&yellowCar); + + car_move(&orangeCar,orangeCar.car_blk_x,yellowCar.car_blk_y); + orangeCar.car_motion = CAR_UP; + drive(&orangeCar); + + //push button input and robot movement + if(!left_pb) + { + x=x-1; + robot_draw(x,y); + robot_clear(x+1,y); + map_draw_grid(x+1,y); + } + if(!right_pb) + { + x=x+1; + robot_draw(x,y); + robot_clear(x-1,y); + map_draw_grid(x-1,y); + } + if(!up_pb) + { + y=y-1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + robot_draw(x,y); + robot_clear(x,y+1); + map_draw_grid(x,y+1); + } + if(!down_pb) + { + y=y+1; + if(y>15) + { + y = 0; + robot_clear(x,15); + map_draw_grid(x,15); + } + + if(y<0) + { + y= 15; + robot_clear(x,0); + map_draw_grid(x,0); + } + + robot_draw(x,y); + robot_clear(x,y-1); + map_draw_grid(x,y-1); + } + + + if((x==redCar.car_blk_x && y==redCar.car_blk_y) || + (x==blueCar.car_blk_x && y == blueCar.car_blk_y) || + (x==whiteCar.car_blk_x && y == whiteCar.car_blk_y) || + (x==yellowCar.car_blk_x && y == yellowCar.car_blk_y) || + (x==orangeCar.car_blk_x && y == orangeCar.car_blk_y)) + { + life--; + robot_clear(x,y); + map_draw_grid(x,y); + x=0; + y=8; + robot_init(0,8); + } + + if(life==0) + collision(); + + score = map_eat_candy(x,y)+score; + uLCD.locate(0,1); + uLCD.printf("Score:%4.1f ",score); //You could remove this code if you already make the accelerometer work. + uLCD.locate(10,1); + uLCD.printf(" Life:%d",life); + if((tick-pre_tick)>500) // Time step control + pre_tick = tick; // update the previous tick + if(x==16||(!left_pb&&!right_pb&&!up_pb&&!down_pb))//heat the safetyzone, go to next level + { + uLCD.cls(); + uLCD.locate(6,8); + uLCD.printf("You Win"); + uLCD.locate(3,10); + uLCD.printf("Score is:%4.1f",score); + while(1) + {} + } + + } + } + +}//end main - if((tick-pre_tick)>500) { // Time step control - pre_tick = tick; // update the previous tick - - /// 3. Update the RoboFrogger on the screen - /// -[Hint] You could update the position of RoboFrogger (draw it on the screen) here based on the user-input at step 2. <br> - - - - /// 4. Implement the code to check the end of game. - - } - } -} \ No newline at end of file
diff -r 1b6fc10c5cea -r 47f07aa85f1a map/map.cpp --- a/map/map.cpp Wed Jul 08 15:45:22 2015 +0000 +++ b/map/map.cpp Mon Feb 01 06:43:06 2016 +0000 @@ -105,13 +105,25 @@ return map[XY2IDX(grid_x,grid_y)]; } -bool map_eat_candy(int grid_x, int grid_y){ +double map_eat_candy(int grid_x, int grid_y){ int idx=XY2IDX(grid_x,grid_y); + int smallCandy=0,bigCandy=0; + double score=0; + if(map[idx].status==GRID_CANDY) + { + map[idx].status = GRID_ROAD; + smallCandy++; + num_candy--; + score = 0.1*smallCandy+score; + return score; + } - if(map[idx].status==GRID_CANDY || map[idx].status==GRID_BIG_CANDY){ + else if(map[idx].status==GRID_BIG_CANDY){ map[idx].status = GRID_ROAD; + bigCandy++; num_candy--; - return 1; + score = score+bigCandy; + return score; } return 0; }
diff -r 1b6fc10c5cea -r 47f07aa85f1a map/map_public.h --- a/map/map_public.h Wed Jul 08 15:45:22 2015 +0000 +++ b/map/map_public.h Mon Feb 01 06:43:06 2016 +0000 @@ -53,7 +53,7 @@ @param grid_y The vertical position in the grid. @return 1:There is a candy be eaten. 0:The is no candy at the grid. */ -bool map_eat_candy(int grid_x, int grid_y); +double map_eat_candy(int grid_x, int grid_y); /** Get the information about the grid @param grid_x The horizontal position in the grid.
diff -r 1b6fc10c5cea -r 47f07aa85f1a robot/robot.cpp --- a/robot/robot.cpp Wed Jul 08 15:45:22 2015 +0000 +++ b/robot/robot.cpp Mon Feb 01 06:43:06 2016 +0000 @@ -23,8 +23,8 @@ // MAKE uLCD.filled_circle(frog_x, frog_y, 2, 0xCC0066); uLCD.filled_rectangle(frog_x-2,frog_y+1,frog_x+2,frog_y+3,0x33FF66); - uLCD.line(frog_x-1, frog_y+4,frog_x-1, frog_y+5, 0xFF0000);//legs - uLCD.line(frog_x+2, frog_y+4,frog_x+2, frog_y+5, 0xFF0000); + //uLCD.line(frog_x-1, frog_y+4,frog_x-1, frog_y+5, 0xFF0000);//legs + //uLCD.line(frog_x+2, frog_y+4,frog_x+2, frog_y+5, 0xFF0000); uLCD.line(frog_x+2, frog_y+1,frog_x+4, frog_y+1, 0xFF0000);//hands uLCD.line(frog_x-2, frog_y+1,frog_x-4, frog_y+1, 0xFF0000); }