Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17zl by
PushingEngine/PushingEngine.h@18:e9e5df6ffb87, 2019-05-08 (annotated)
- Committer:
- franklzw
- Date:
- Wed May 08 17:19:40 2019 +0000
- Revision:
- 18:e9e5df6ffb87
- Parent:
- 17:9e6eb80478d3
Final version! Sokoban game with 3 level and a short introduction. ; Use the joystick to select from menu and enter each level. ; Use bottom A,B,X,Y to play the game,and tone can be adjusted by the potentialmeter.; Enjoy!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
franklzw | 2:9f0d9516a6cd | 1 | #ifndef PUSHINGENGINE_H |
franklzw | 2:9f0d9516a6cd | 2 | #define PUSHINGENGINE_H |
franklzw | 2:9f0d9516a6cd | 3 | |
franklzw | 2:9f0d9516a6cd | 4 | #include "mbed.h" |
franklzw | 2:9f0d9516a6cd | 5 | #include "N5110.h" |
franklzw | 2:9f0d9516a6cd | 6 | #include "Gamepad.h" |
franklzw | 2:9f0d9516a6cd | 7 | #include "Ppl.h" |
franklzw | 2:9f0d9516a6cd | 8 | #include "Box.h" |
franklzw | 8:83891ea9a5d9 | 9 | #include "Cross.h" |
franklzw | 10:726c1489894e | 10 | #include "Menu.h" |
franklzw | 8:83891ea9a5d9 | 11 | |
franklzw | 13:5930f0e5889d | 12 | /** PushingEngine Class |
franklzw | 13:5930f0e5889d | 13 | @author Zhenwen liao, University of Leeds |
franklzw | 13:5930f0e5889d | 14 | @brief Controls the Pushing and Determining Move in the Sokoban game |
franklzw | 13:5930f0e5889d | 15 | @date 04 2019 |
franklzw | 13:5930f0e5889d | 16 | */ |
franklzw | 2:9f0d9516a6cd | 17 | class PushingEngine |
franklzw | 2:9f0d9516a6cd | 18 | { |
franklzw | 2:9f0d9516a6cd | 19 | |
franklzw | 2:9f0d9516a6cd | 20 | public: |
franklzw | 13:5930f0e5889d | 21 | /** Constructor */ |
franklzw | 2:9f0d9516a6cd | 22 | PushingEngine(); |
franklzw | 17:9e6eb80478d3 | 23 | |
franklzw | 13:5930f0e5889d | 24 | /** Destructor */ |
franklzw | 2:9f0d9516a6cd | 25 | ~PushingEngine(); |
franklzw | 2:9f0d9516a6cd | 26 | |
franklzw | 13:5930f0e5889d | 27 | /** Initialise the first position of two boxes, ppl, two crosses and initial reading of Menu parameter |
franklzw | 13:5930f0e5889d | 28 | * @param box1_x @details the first x position of box1 (int) |
franklzw | 13:5930f0e5889d | 29 | * @param box1_y @details the first y position of box1 (int) |
franklzw | 13:5930f0e5889d | 30 | * @param box2_x @details the first x position of box2 (int) |
franklzw | 13:5930f0e5889d | 31 | * @param box2_y @details the first y position of box2 (int) |
franklzw | 13:5930f0e5889d | 32 | * @param ppl_x @details the first x position of ppl (int) |
franklzw | 13:5930f0e5889d | 33 | * @param ppl_y @details the first y position of ppl (int) |
franklzw | 13:5930f0e5889d | 34 | * @param cross1_x @details the first x position of cross1 (int) |
franklzw | 13:5930f0e5889d | 35 | * @param cross1_y @details the first y position of cross1 (int) |
franklzw | 13:5930f0e5889d | 36 | * @param cross2_x @details the first x position of cross2 (int) |
franklzw | 13:5930f0e5889d | 37 | * @param cross2_y @details the first y position of cross2 (int) |
franklzw | 13:5930f0e5889d | 38 | * @param menu_x @details the initial reading of level for menu (int) |
franklzw | 13:5930f0e5889d | 39 | * @param menu_y @details the initial reading of level confirmation for menu (int) |
franklzw | 13:5930f0e5889d | 40 | */ |
franklzw | 8:83891ea9a5d9 | 41 | void init(int box1_x,int box1_y,int box2_x,int box2_y,int ppl_x,int ppl_y, |
franklzw | 10:726c1489894e | 42 | int cross1_x,int cross1_y,int cross2_x,int cross2_y,int menu_x,int menu_y); |
franklzw | 17:9e6eb80478d3 | 43 | |
franklzw | 13:5930f0e5889d | 44 | /** Read the inputs from Gamepad |
franklzw | 13:5930f0e5889d | 45 | * @details Read the potentialmeter, botton A,B,X,Y |
franklzw | 17:9e6eb80478d3 | 46 | * the reading of potentialmeter is used to control the tone of A,B,X,Y |
franklzw | 17:9e6eb80478d3 | 47 | * being pressed |
franklzw | 13:5930f0e5889d | 48 | */ |
franklzw | 2:9f0d9516a6cd | 49 | void read_input(Gamepad &pad); |
franklzw | 17:9e6eb80478d3 | 50 | |
franklzw | 13:5930f0e5889d | 51 | /** Update the Pushing and determining proccess |
franklzw | 13:5930f0e5889d | 52 | * @details First,check box and ppl touching or not |
franklzw | 13:5930f0e5889d | 53 | * Second,check if next move of ppl will kick the box out off screen |
franklzw | 13:5930f0e5889d | 54 | * Third, update the parameter accorading to the determination of above function |
franklzw | 13:5930f0e5889d | 55 | * Fourth, reset the indicator for function |
franklzw | 13:5930f0e5889d | 56 | * Fifth, check if ppl will cover the box after next move |
franklzw | 13:5930f0e5889d | 57 | * Sixth, check if box and cross match each other and set the scores |
franklzw | 13:5930f0e5889d | 58 | */ |
franklzw | 6:6b083e22cb53 | 59 | void update(Gamepad &pad,int barrier_x,int barrier_y); |
franklzw | 17:9e6eb80478d3 | 60 | |
franklzw | 13:5930f0e5889d | 61 | /** Draw all the oject on the screen*/ |
franklzw | 6:6b083e22cb53 | 62 | void draw(N5110 &lcd,int barrier_x,int barrier_y); |
franklzw | 17:9e6eb80478d3 | 63 | |
franklzw | 13:5930f0e5889d | 64 | /** Set the score */ |
franklzw | 9:1fa7f087051e | 65 | int set_score(); |
franklzw | 17:9e6eb80478d3 | 66 | |
franklzw | 17:9e6eb80478d3 | 67 | /** Get the Menu |
franklzw | 13:5930f0e5889d | 68 | * @return the parameter of current Menu state |
franklzw | 13:5930f0e5889d | 69 | */ |
franklzw | 11:f5d0ea7e4b74 | 70 | Vector2D get_menu(N5110 &lcd,Gamepad &pad); |
franklzw | 17:9e6eb80478d3 | 71 | |
franklzw | 17:9e6eb80478d3 | 72 | /** Reset the menu parameter to level 1 and not confrim*/ |
franklzw | 17:9e6eb80478d3 | 73 | void reset_menu(); |
franklzw | 17:9e6eb80478d3 | 74 | |
franklzw | 17:9e6eb80478d3 | 75 | /** Print a set of short introuduction of the game */ |
franklzw | 17:9e6eb80478d3 | 76 | void introduction(N5110 &lcd,Gamepad &pad); |
franklzw | 18:e9e5df6ffb87 | 77 | |
franklzw | 17:9e6eb80478d3 | 78 | /** |
franklzw | 17:9e6eb80478d3 | 79 | * @brief Check if ppl is touching box1 |
franklzw | 17:9e6eb80478d3 | 80 | * @details First get the current position of ppl and box1. |
franklzw | 17:9e6eb80478d3 | 81 | * Then, calculate the x,y position difference between |
franklzw | 17:9e6eb80478d3 | 82 | * box1 and ppl respectively. |
franklzw | 17:9e6eb80478d3 | 83 | * If y positions are the same,x of ppl is 8-pixel bigger than x of box1 |
franklzw | 17:9e6eb80478d3 | 84 | * that is to say ppl is on the right hand side of the box1 and box can be |
franklzw | 17:9e6eb80478d3 | 85 | * pushed to left. In this case, the indicactor for box1 _s will be set to 1. |
franklzw | 17:9e6eb80478d3 | 86 | * And so as the rest of cases,indicactor will be set to 2 for pushing right, |
franklzw | 17:9e6eb80478d3 | 87 | * 3 for pusing down, 4 for pushing up and 0 for ppl and box1 not touching. |
franklzw | 17:9e6eb80478d3 | 88 | */ |
franklzw | 4:750d3f9b54de | 89 | void check_ppl_box1_touching(Gamepad &pad); |
franklzw | 18:e9e5df6ffb87 | 90 | |
franklzw | 17:9e6eb80478d3 | 91 | /** |
franklzw | 17:9e6eb80478d3 | 92 | * @brief Check if ppl is touching box2, Basically the same as check_ppl_box1_touching |
franklzw | 17:9e6eb80478d3 | 93 | * @details If y positions are the same,x of ppl is 8-pixel bigger than x of box2 |
franklzw | 17:9e6eb80478d3 | 94 | * that is to say ppl is on the right hand side of the box2 and box can be |
franklzw | 17:9e6eb80478d3 | 95 | * pushed to left. In this case, the indicactor for box1 _r will be set to 1. |
franklzw | 17:9e6eb80478d3 | 96 | * And so as the rest of cases,indicactor will be set to 2 for pushing right, |
franklzw | 17:9e6eb80478d3 | 97 | * 3 for pusing down, 4 for pushing up and 0 for ppl and box1 not touching. |
franklzw | 17:9e6eb80478d3 | 98 | */ |
franklzw | 4:750d3f9b54de | 99 | void check_ppl_box2_touching(Gamepad &pad); |
franklzw | 18:e9e5df6ffb87 | 100 | |
franklzw | 17:9e6eb80478d3 | 101 | /** |
franklzw | 17:9e6eb80478d3 | 102 | * @brief Hold the position of Ppl when box1 face against the wall |
franklzw | 17:9e6eb80478d3 | 103 | * @details There is a scenario which the box is against the wall, if ppl try to push |
franklzw | 17:9e6eb80478d3 | 104 | * the box to wall side. The box will go off screen, and ppl will take its position. |
franklzw | 17:9e6eb80478d3 | 105 | * To fix this, i implement this function which works with the check_ppl_box1_touching function. |
franklzw | 17:9e6eb80478d3 | 106 | * First, check the position of the box,to see if it is on the egde of the screen. |
franklzw | 17:9e6eb80478d3 | 107 | * Then, use the indicator from box&ppl touching function to see if the off-screen will happen. |
franklzw | 17:9e6eb80478d3 | 108 | * If the x position of box1 is equal to 2 which means it cant be move left any more. |
franklzw | 17:9e6eb80478d3 | 109 | * Then, we check if the touching indicator _s is equal to 1. If true, we will |
franklzw | 17:9e6eb80478d3 | 110 | * set a new indicator _temp to 1. This indicator _temp is then use to freeze the ppl posision |
franklzw | 17:9e6eb80478d3 | 111 | * in its own class fucntion. The rest of the cases is implemented in the same way. |
franklzw | 17:9e6eb80478d3 | 112 | */ |
franklzw | 6:6b083e22cb53 | 113 | void hold_ppl_box1_wall(Gamepad &pad); |
franklzw | 18:e9e5df6ffb87 | 114 | |
franklzw | 17:9e6eb80478d3 | 115 | /** |
franklzw | 17:9e6eb80478d3 | 116 | * @brief Hold the position of Ppl when box2 face against the wall. Basically the same as hold_ppl_box1_wall |
franklzw | 17:9e6eb80478d3 | 117 | * @details There is a scenario which the box is against the wall, if ppl try to push |
franklzw | 17:9e6eb80478d3 | 118 | * the box to wall side. The box will go off screen, and ppl will take its position. |
franklzw | 17:9e6eb80478d3 | 119 | * To fix this, i implement this function which works with the check_ppl_box2_touching function. |
franklzw | 17:9e6eb80478d3 | 120 | * First, check the position of the box,to see if it is on the egde of the screen. |
franklzw | 17:9e6eb80478d3 | 121 | * Then, use the indicator from box&ppl touching function to see if the off-screen will happen. |
franklzw | 17:9e6eb80478d3 | 122 | * If the x position of box1 is equal to 2 which means it cant be move left any more. |
franklzw | 17:9e6eb80478d3 | 123 | * Then, we check if the touching indicator _r is equal to 1. If true, we will |
franklzw | 17:9e6eb80478d3 | 124 | * set a new indicator _temp to 1. This indicator _temp is then use to freeze the ppl posision |
franklzw | 17:9e6eb80478d3 | 125 | * in its own class fucntion. The rest of the cases is implemented in the same way. |
franklzw | 17:9e6eb80478d3 | 126 | */ |
franklzw | 6:6b083e22cb53 | 127 | void hold_ppl_box2_wall(Gamepad &pad); |
franklzw | 18:e9e5df6ffb87 | 128 | |
franklzw | 17:9e6eb80478d3 | 129 | /** Check if ppl cover the box |
franklzw | 17:9e6eb80478d3 | 130 | * @details Check if move ppl cover the box then result input back into the update function to determine |
franklzw | 17:9e6eb80478d3 | 131 | * if ppl will be moved or stay at it last position |
franklzw | 17:9e6eb80478d3 | 132 | * @return True if moving ppl will cover box; false if not |
franklzw | 17:9e6eb80478d3 | 133 | */ |
franklzw | 7:6f8aeadc4370 | 134 | bool ppl_cover_box(Gamepad &pad); |
franklzw | 17:9e6eb80478d3 | 135 | |
franklzw | 17:9e6eb80478d3 | 136 | /** Check box cover cross1 and score |
franklzw | 17:9e6eb80478d3 | 137 | * @details Check if box cover which means the player get one score. |
franklzw | 17:9e6eb80478d3 | 138 | * First, the positon of cross1, box1 and box2 are read. Then, compare both of cross1 to box1 |
franklzw | 17:9e6eb80478d3 | 139 | cross1 to box2. If any of these two comparsion return true, one score will be added |
franklzw | 17:9e6eb80478d3 | 140 | */ |
franklzw | 8:83891ea9a5d9 | 141 | void box_cover_cross1_score(Gamepad &pad); |
franklzw | 17:9e6eb80478d3 | 142 | |
franklzw | 17:9e6eb80478d3 | 143 | /** Check box cover cross2 and score |
franklzw | 17:9e6eb80478d3 | 144 | * @details Check if box cover which means the player get one score. |
franklzw | 17:9e6eb80478d3 | 145 | * First, the positon of cross2, box1 and box2 are read. Then, compare both of cross2 to box1 |
franklzw | 17:9e6eb80478d3 | 146 | cross2 to box2. If any of these two comparsion return true, one score will be added |
franklzw | 17:9e6eb80478d3 | 147 | */ |
franklzw | 8:83891ea9a5d9 | 148 | void box_cover_cross2_score(Gamepad &pad); |
franklzw | 2:9f0d9516a6cd | 149 | |
franklzw | 18:e9e5df6ffb87 | 150 | private: |
franklzw | 18:e9e5df6ffb87 | 151 | |
franklzw | 2:9f0d9516a6cd | 152 | Box _b1; |
franklzw | 2:9f0d9516a6cd | 153 | Box _b2; |
franklzw | 2:9f0d9516a6cd | 154 | |
franklzw | 2:9f0d9516a6cd | 155 | // positions of the Boxes |
franklzw | 2:9f0d9516a6cd | 156 | int _b1x; |
franklzw | 2:9f0d9516a6cd | 157 | int _b2x; |
franklzw | 2:9f0d9516a6cd | 158 | int _b1y; |
franklzw | 2:9f0d9516a6cd | 159 | int _b2y; |
franklzw | 2:9f0d9516a6cd | 160 | |
franklzw | 2:9f0d9516a6cd | 161 | Ppl _ppl; |
franklzw | 2:9f0d9516a6cd | 162 | |
franklzw | 2:9f0d9516a6cd | 163 | // poitions of the ppl |
franklzw | 2:9f0d9516a6cd | 164 | int _pplx; |
franklzw | 2:9f0d9516a6cd | 165 | int _pply; |
franklzw | 17:9e6eb80478d3 | 166 | |
franklzw | 8:83891ea9a5d9 | 167 | Cross _c1; |
franklzw | 8:83891ea9a5d9 | 168 | Cross _c2; |
franklzw | 17:9e6eb80478d3 | 169 | |
franklzw | 8:83891ea9a5d9 | 170 | // position of the crosses |
franklzw | 8:83891ea9a5d9 | 171 | int _c1x; |
franklzw | 8:83891ea9a5d9 | 172 | int _c2x; |
franklzw | 8:83891ea9a5d9 | 173 | int _c1y; |
franklzw | 8:83891ea9a5d9 | 174 | int _c2y; |
franklzw | 17:9e6eb80478d3 | 175 | |
franklzw | 10:726c1489894e | 176 | Menu _menu; |
franklzw | 17:9e6eb80478d3 | 177 | |
franklzw | 11:f5d0ea7e4b74 | 178 | //reading of bottom |
franklzw | 5:b50ce6160013 | 179 | int _bb; |
franklzw | 5:b50ce6160013 | 180 | int _bx; |
franklzw | 5:b50ce6160013 | 181 | int _by; |
franklzw | 5:b50ce6160013 | 182 | int _ba; |
franklzw | 5:b50ce6160013 | 183 | int _bjoy; |
franklzw | 10:726c1489894e | 184 | int _bj; |
franklzw | 2:9f0d9516a6cd | 185 | |
franklzw | 17:9e6eb80478d3 | 186 | //indicator for ppl and ppl touching |
franklzw | 5:b50ce6160013 | 187 | int _s; //ppl with b1 |
franklzw | 5:b50ce6160013 | 188 | int _r; //ppl with b2 |
franklzw | 11:f5d0ea7e4b74 | 189 | |
franklzw | 17:9e6eb80478d3 | 190 | //indicator for holding ppl position |
franklzw | 17:9e6eb80478d3 | 191 | int _temp; |
franklzw | 17:9e6eb80478d3 | 192 | |
franklzw | 8:83891ea9a5d9 | 193 | int _score; |
franklzw | 2:9f0d9516a6cd | 194 | |
franklzw | 2:9f0d9516a6cd | 195 | }; |
franklzw | 2:9f0d9516a6cd | 196 | |
franklzw | 2:9f0d9516a6cd | 197 | #endif |