Game for Project 2
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
main.cpp@0:7fe3c940e4b5, 2015-06-25 (annotated)
- Committer:
- leuyentran
- Date:
- Thu Jun 25 14:35:52 2015 +0000
- Revision:
- 0:7fe3c940e4b5
Georgia Tech ECE2035 Sum 15
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> |
leuyentran | 0:7fe3c940e4b5 | 7 | |
leuyentran | 0:7fe3c940e4b5 | 8 | // Include header files for pacman project |
leuyentran | 0:7fe3c940e4b5 | 9 | #include "globals.h" |
leuyentran | 0:7fe3c940e4b5 | 10 | #include "map_public.h" |
leuyentran | 0:7fe3c940e4b5 | 11 | #include "robot.h" |
leuyentran | 0:7fe3c940e4b5 | 12 | |
leuyentran | 0:7fe3c940e4b5 | 13 | //Platform initialization |
leuyentran | 0:7fe3c940e4b5 | 14 | DigitalIn left_pb(p24); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 15 | DigitalIn right_pb(p21); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 16 | DigitalIn up_pb(p22); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 17 | DigitalIn down_pb(p23); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 18 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) |
leuyentran | 0:7fe3c940e4b5 | 19 | |
leuyentran | 0:7fe3c940e4b5 | 20 | DigitalOut myled(LED1); |
leuyentran | 0:7fe3c940e4b5 | 21 | |
leuyentran | 0:7fe3c940e4b5 | 22 | int main() |
leuyentran | 0:7fe3c940e4b5 | 23 | { |
leuyentran | 0:7fe3c940e4b5 | 24 | // Initialize the timer |
leuyentran | 0:7fe3c940e4b5 | 25 | /// [Example of time control implementation] |
leuyentran | 0:7fe3c940e4b5 | 26 | /// Here is a rough example to implement the timer control <br><br> |
leuyentran | 0:7fe3c940e4b5 | 27 | int tick, pre_tick; |
leuyentran | 0:7fe3c940e4b5 | 28 | srand (time(NULL)); |
leuyentran | 0:7fe3c940e4b5 | 29 | Timer timer; |
leuyentran | 0:7fe3c940e4b5 | 30 | timer.start(); |
leuyentran | 0:7fe3c940e4b5 | 31 | tick = timer.read_ms(); |
leuyentran | 0:7fe3c940e4b5 | 32 | pre_tick = tick; |
leuyentran | 0:7fe3c940e4b5 | 33 | |
leuyentran | 0:7fe3c940e4b5 | 34 | // Initialize the buttons |
leuyentran | 0:7fe3c940e4b5 | 35 | left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed |
leuyentran | 0:7fe3c940e4b5 | 36 | right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed |
leuyentran | 0:7fe3c940e4b5 | 37 | up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
leuyentran | 0:7fe3c940e4b5 | 38 | down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
leuyentran | 0:7fe3c940e4b5 | 39 | |
leuyentran | 0:7fe3c940e4b5 | 40 | |
leuyentran | 0:7fe3c940e4b5 | 41 | /// [Example of the game control implementation] |
leuyentran | 0:7fe3c940e4b5 | 42 | /// Here is the example to initialize the game <br><br> |
leuyentran | 0:7fe3c940e4b5 | 43 | uLCD.cls(); |
leuyentran | 0:7fe3c940e4b5 | 44 | map_init(); |
leuyentran | 0:7fe3c940e4b5 | 45 | robot_init(0,8); |
leuyentran | 0:7fe3c940e4b5 | 46 | double x=0; |
leuyentran | 0:7fe3c940e4b5 | 47 | double y=8; |
leuyentran | 0:7fe3c940e4b5 | 48 | |
leuyentran | 0:7fe3c940e4b5 | 49 | /// 1. Begin the game loop |
leuyentran | 0:7fe3c940e4b5 | 50 | while(1) { |
leuyentran | 0:7fe3c940e4b5 | 51 | tick = timer.read_ms(); // Read current time |
leuyentran | 0:7fe3c940e4b5 | 52 | |
leuyentran | 0:7fe3c940e4b5 | 53 | /// 2. Implement the code to get user input and update the Pacman |
leuyentran | 0:7fe3c940e4b5 | 54 | /// -[Hint] Implement the code to move Pacman. You could use either push-button or accelerometer. <br> |
leuyentran | 0:7fe3c940e4b5 | 55 | |
leuyentran | 0:7fe3c940e4b5 | 56 | // [Some hints for user-input detection] |
leuyentran | 0:7fe3c940e4b5 | 57 | //acc.readXYZGravity(&x,&y,&z); //read accelerometer |
leuyentran | 0:7fe3c940e4b5 | 58 | uLCD.locate(0,1); |
leuyentran | 0:7fe3c940e4b5 | 59 | uLCD.printf("Score x%4.1f ",1); //You could remove this code if you already make the accelerometer work. |
leuyentran | 0:7fe3c940e4b5 | 60 | /// -[Hint] Here is a simple way to utilize the readings of accelerometer: |
leuyentran | 0:7fe3c940e4b5 | 61 | /// If x is larger than certain value (ex:0.3), then make the Pacman move right. |
leuyentran | 0:7fe3c940e4b5 | 62 | /// If x<-0.3, then make it move left. <br> |
leuyentran | 0:7fe3c940e4b5 | 63 | |
leuyentran | 0:7fe3c940e4b5 | 64 | |
leuyentran | 0:7fe3c940e4b5 | 65 | if((tick-pre_tick)>500) { // Time step control |
leuyentran | 0:7fe3c940e4b5 | 66 | pre_tick = tick; // update the previous tick |
leuyentran | 0:7fe3c940e4b5 | 67 | |
leuyentran | 0:7fe3c940e4b5 | 68 | /// 3. Update the Pacman on the screen |
leuyentran | 0:7fe3c940e4b5 | 69 | /// -[Hint] You could update the position of Pacman (draw it on the screen) here based on the user-input at step 2. <br> |
leuyentran | 0:7fe3c940e4b5 | 70 | if (!up_pb) { //MOVE UP |
leuyentran | 0:7fe3c940e4b5 | 71 | robot_clear(x,y); |
leuyentran | 0:7fe3c940e4b5 | 72 | map_draw_grid(x,y); |
leuyentran | 0:7fe3c940e4b5 | 73 | if (y!=0) { |
leuyentran | 0:7fe3c940e4b5 | 74 | y=y-1; |
leuyentran | 0:7fe3c940e4b5 | 75 | } else { |
leuyentran | 0:7fe3c940e4b5 | 76 | y=15; |
leuyentran | 0:7fe3c940e4b5 | 77 | } |
leuyentran | 0:7fe3c940e4b5 | 78 | wait(0.1); |
leuyentran | 0:7fe3c940e4b5 | 79 | robot_init(x,y); |
leuyentran | 0:7fe3c940e4b5 | 80 | } else if(!down_pb) { //MOVE DOWN |
leuyentran | 0:7fe3c940e4b5 | 81 | robot_clear(x,y); |
leuyentran | 0:7fe3c940e4b5 | 82 | map_draw_grid(x,y); |
leuyentran | 0:7fe3c940e4b5 | 83 | if(y!=15) { |
leuyentran | 0:7fe3c940e4b5 | 84 | y=y+1; |
leuyentran | 0:7fe3c940e4b5 | 85 | } else { |
leuyentran | 0:7fe3c940e4b5 | 86 | y=0; |
leuyentran | 0:7fe3c940e4b5 | 87 | } |
leuyentran | 0:7fe3c940e4b5 | 88 | wait(0.1); |
leuyentran | 0:7fe3c940e4b5 | 89 | robot_init(x,y); |
leuyentran | 0:7fe3c940e4b5 | 90 | } else if (!left_pb) { //MOVE LEFT |
leuyentran | 0:7fe3c940e4b5 | 91 | robot_clear(x,y); |
leuyentran | 0:7fe3c940e4b5 | 92 | map_draw_grid(x,y); |
leuyentran | 0:7fe3c940e4b5 | 93 | if (x!=0) { |
leuyentran | 0:7fe3c940e4b5 | 94 | x-=1; |
leuyentran | 0:7fe3c940e4b5 | 95 | } |
leuyentran | 0:7fe3c940e4b5 | 96 | wait(0.1); |
leuyentran | 0:7fe3c940e4b5 | 97 | robot_init(x,y); |
leuyentran | 0:7fe3c940e4b5 | 98 | |
leuyentran | 0:7fe3c940e4b5 | 99 | } else if(!right_pb) { //MOVE RIGHT |
leuyentran | 0:7fe3c940e4b5 | 100 | robot_clear(x,y); |
leuyentran | 0:7fe3c940e4b5 | 101 | map_draw_grid(x,y); |
leuyentran | 0:7fe3c940e4b5 | 102 | if (x!=16) { |
leuyentran | 0:7fe3c940e4b5 | 103 | x+=1; |
leuyentran | 0:7fe3c940e4b5 | 104 | } |
leuyentran | 0:7fe3c940e4b5 | 105 | wait(0.1); |
leuyentran | 0:7fe3c940e4b5 | 106 | robot_init(x,y); |
leuyentran | 0:7fe3c940e4b5 | 107 | } |
leuyentran | 0:7fe3c940e4b5 | 108 | |
leuyentran | 0:7fe3c940e4b5 | 109 | |
leuyentran | 0:7fe3c940e4b5 | 110 | |
leuyentran | 0:7fe3c940e4b5 | 111 | /// 4. Implement the code to check the end of game. |
leuyentran | 0:7fe3c940e4b5 | 112 | |
leuyentran | 0:7fe3c940e4b5 | 113 | } |
leuyentran | 0:7fe3c940e4b5 | 114 | } |
leuyentran | 0:7fe3c940e4b5 | 115 | } |