frogger game revision
Dependencies: 4DGL-uLCD-SE MMA8452 SDFileSystem mbed wave_player
Fork of ECE2035_FroggerGame_SUM2015 by
main.cpp@3:3856a3dccf6d, 2015-07-05 (annotated)
- Committer:
- ece2035ta
- Date:
- Sun Jul 05 16:55:35 2015 +0000
- Revision:
- 3:3856a3dccf6d
- Parent:
- 0:7fe3c940e4b5
- Child:
- 4:3fb1d198e9d6
ECE 2035
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 | |
leuyentran | 0:7fe3c940e4b5 | 9 | // Include header files for pacman project |
leuyentran | 0:7fe3c940e4b5 | 10 | #include "globals.h" |
leuyentran | 0:7fe3c940e4b5 | 11 | #include "map_public.h" |
leuyentran | 0:7fe3c940e4b5 | 12 | #include "robot.h" |
leuyentran | 0:7fe3c940e4b5 | 13 | |
leuyentran | 0:7fe3c940e4b5 | 14 | //Platform initialization |
leuyentran | 0:7fe3c940e4b5 | 15 | DigitalIn left_pb(p24); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 16 | DigitalIn right_pb(p21); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 17 | DigitalIn up_pb(p22); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 18 | DigitalIn down_pb(p23); // push bottom |
leuyentran | 0:7fe3c940e4b5 | 19 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) |
ece2035ta | 3:3856a3dccf6d | 20 | Serial pc(USBTX,USBRX); // used by Accelerometer |
ece2035ta | 3:3856a3dccf6d | 21 | MMA8452 acc(p28, p27, 100000); // Accelerometer |
leuyentran | 0:7fe3c940e4b5 | 22 | DigitalOut myled(LED1); |
leuyentran | 0:7fe3c940e4b5 | 23 | |
leuyentran | 0:7fe3c940e4b5 | 24 | int main() |
leuyentran | 0:7fe3c940e4b5 | 25 | { |
leuyentran | 0:7fe3c940e4b5 | 26 | // Initialize the timer |
leuyentran | 0:7fe3c940e4b5 | 27 | /// [Example of time control implementation] |
leuyentran | 0:7fe3c940e4b5 | 28 | /// Here is a rough example to implement the timer control <br><br> |
leuyentran | 0:7fe3c940e4b5 | 29 | int tick, pre_tick; |
leuyentran | 0:7fe3c940e4b5 | 30 | srand (time(NULL)); |
leuyentran | 0:7fe3c940e4b5 | 31 | Timer timer; |
leuyentran | 0:7fe3c940e4b5 | 32 | timer.start(); |
leuyentran | 0:7fe3c940e4b5 | 33 | tick = timer.read_ms(); |
leuyentran | 0:7fe3c940e4b5 | 34 | pre_tick = tick; |
leuyentran | 0:7fe3c940e4b5 | 35 | |
leuyentran | 0:7fe3c940e4b5 | 36 | // Initialize the buttons |
leuyentran | 0:7fe3c940e4b5 | 37 | left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed |
leuyentran | 0:7fe3c940e4b5 | 38 | right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed |
leuyentran | 0:7fe3c940e4b5 | 39 | up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
leuyentran | 0:7fe3c940e4b5 | 40 | down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
leuyentran | 0:7fe3c940e4b5 | 41 | |
leuyentran | 0:7fe3c940e4b5 | 42 | |
leuyentran | 0:7fe3c940e4b5 | 43 | /// [Example of the game control implementation] |
leuyentran | 0:7fe3c940e4b5 | 44 | /// Here is the example to initialize the game <br><br> |
leuyentran | 0:7fe3c940e4b5 | 45 | uLCD.cls(); |
leuyentran | 0:7fe3c940e4b5 | 46 | map_init(); |
leuyentran | 0:7fe3c940e4b5 | 47 | robot_init(0,8); |
ece2035ta | 3:3856a3dccf6d | 48 | double x_, y_, z_; |
leuyentran | 0:7fe3c940e4b5 | 49 | double x=0; |
leuyentran | 0:7fe3c940e4b5 | 50 | double y=8; |
leuyentran | 0:7fe3c940e4b5 | 51 | |
leuyentran | 0:7fe3c940e4b5 | 52 | /// 1. Begin the game loop |
leuyentran | 0:7fe3c940e4b5 | 53 | while(1) { |
leuyentran | 0:7fe3c940e4b5 | 54 | tick = timer.read_ms(); // Read current time |
leuyentran | 0:7fe3c940e4b5 | 55 | |
leuyentran | 0:7fe3c940e4b5 | 56 | /// 2. Implement the code to get user input and update the Pacman |
leuyentran | 0:7fe3c940e4b5 | 57 | /// -[Hint] Implement the code to move Pacman. You could use either push-button or accelerometer. <br> |
leuyentran | 0:7fe3c940e4b5 | 58 | |
leuyentran | 0:7fe3c940e4b5 | 59 | // [Some hints for user-input detection] |
ece2035ta | 3:3856a3dccf6d | 60 | acc.readXYZGravity(&x_,&y_,&z_); //read accelerometer |
leuyentran | 0:7fe3c940e4b5 | 61 | uLCD.locate(0,1); |
ece2035ta | 3:3856a3dccf6d | 62 | |
leuyentran | 0:7fe3c940e4b5 | 63 | uLCD.printf("Score x%4.1f ",1); //You could remove this code if you already make the accelerometer work. |
leuyentran | 0:7fe3c940e4b5 | 64 | /// -[Hint] Here is a simple way to utilize the readings of accelerometer: |
leuyentran | 0:7fe3c940e4b5 | 65 | /// If x is larger than certain value (ex:0.3), then make the Pacman move right. |
leuyentran | 0:7fe3c940e4b5 | 66 | /// If x<-0.3, then make it move left. <br> |
leuyentran | 0:7fe3c940e4b5 | 67 | |
leuyentran | 0:7fe3c940e4b5 | 68 | |
leuyentran | 0:7fe3c940e4b5 | 69 | if((tick-pre_tick)>500) { // Time step control |
leuyentran | 0:7fe3c940e4b5 | 70 | pre_tick = tick; // update the previous tick |
leuyentran | 0:7fe3c940e4b5 | 71 | |
leuyentran | 0:7fe3c940e4b5 | 72 | /// 3. Update the Pacman on the screen |
leuyentran | 0:7fe3c940e4b5 | 73 | /// -[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 | 74 | |
leuyentran | 0:7fe3c940e4b5 | 75 | |
leuyentran | 0:7fe3c940e4b5 | 76 | |
leuyentran | 0:7fe3c940e4b5 | 77 | /// 4. Implement the code to check the end of game. |
leuyentran | 0:7fe3c940e4b5 | 78 | |
leuyentran | 0:7fe3c940e4b5 | 79 | } |
leuyentran | 0:7fe3c940e4b5 | 80 | } |
leuyentran | 0:7fe3c940e4b5 | 81 | } |