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 FATFileSystem
CaMove/CaMove.h@23:5afb0e9b5b11, 2019-05-09 (annotated)
- Committer:
- rottenegg
- Date:
- Thu May 09 06:36:26 2019 +0000
- Revision:
- 23:5afb0e9b5b11
- Parent:
- 22:0a12e52848fc
- Child:
- 24:7f26feb4666d
CaMove Documentation not showing
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rottenegg | 4:34bf3587cf42 | 1 | #ifndef CAMOVE_H |
| rottenegg | 4:34bf3587cf42 | 2 | #define CAMOVE_H |
| rottenegg | 4:34bf3587cf42 | 3 | |
| rottenegg | 5:860087ff295e | 4 | /** CaMove Class |
| rottenegg | 5:860087ff295e | 5 | @brief Class for Charater Animation and Movement Interation. |
| rottenegg | 5:860087ff295e | 6 | @brief Controls Interrupts for Interation Button and The Joystick for Charater Movement |
| rottenegg | 5:860087ff295e | 7 | @brief Uses the FilePaths.h to supply File Location Information and uses Bitmap.h to create animations on the LCD |
| rottenegg | 5:860087ff295e | 8 | |
| rottenegg | 5:860087ff295e | 9 | @author Saad Tayyab |
| rottenegg | 5:860087ff295e | 10 | @date 2th April 2019 |
| rottenegg | 5:860087ff295e | 11 | |
| rottenegg | 5:860087ff295e | 12 | @code |
| rottenegg | 5:860087ff295e | 13 | //Required Libraries |
| rottenegg | 5:860087ff295e | 14 | #include "mbed.h" |
| rottenegg | 5:860087ff295e | 15 | #include "CaMove.h" |
| rottenegg | 17:7d4d8905b608 | 16 | #include "Bitmap.h" |
| rottenegg | 5:860087ff295e | 17 | #include "SDFileSystem.h" |
| rottenegg | 5:860087ff295e | 18 | |
| rottenegg | 5:860087ff295e | 19 | //Define SD Card |
| rottenegg | 5:860087ff295e | 20 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
| rottenegg | 5:860087ff295e | 21 | |
| rottenegg | 5:860087ff295e | 22 | //Define LCD |
| rottenegg | 17:7d4d8905b608 | 23 | Bitmap lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| rottenegg | 5:860087ff295e | 24 | |
| rottenegg | 5:860087ff295e | 25 | //Define CaMove |
| rottenegg | 5:860087ff295e | 26 | CaMove CM(PTB9,PTB11,PTB10); |
| rottenegg | 5:860087ff295e | 27 | |
| rottenegg | 5:860087ff295e | 28 | //A mini game instance where charater can click two black boxes in either corner |
| rottenegg | 5:860087ff295e | 29 | //depending on clicked box the charater will either enter a new instance where he |
| rottenegg | 5:860087ff295e | 30 | //can leave the screen and when he does the program ends or the instance where the |
| rottenegg | 5:860087ff295e | 31 | //screen fades to black. The program ends with a circle on sceen. |
| rottenegg | 5:860087ff295e | 32 | |
| rottenegg | 5:860087ff295e | 33 | int main() { |
| rottenegg | 5:860087ff295e | 34 | //Initalize and Set Spawn Values |
| rottenegg | 5:860087ff295e | 35 | CM.init(40,20,Fd); |
| rottenegg | 5:860087ff295e | 36 | lcd.init(); |
| rottenegg | 5:860087ff295e | 37 | |
| rottenegg | 5:860087ff295e | 38 | //Setting Interative Regions |
| rottenegg | 5:860087ff295e | 39 | CM.set_region(6,6,8,8); |
| rottenegg | 5:860087ff295e | 40 | CM.set_region(60,30,8,8); |
| rottenegg | 5:860087ff295e | 41 | |
| rottenegg | 5:860087ff295e | 42 | //While Loop that ends when Interative Region triggered |
| rottenegg | 5:860087ff295e | 43 | while(!CM.is_trg()) { |
| rottenegg | 5:860087ff295e | 44 | lcd.clear(); |
| rottenegg | 5:860087ff295e | 45 | lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); |
| rottenegg | 5:860087ff295e | 46 | lcd.drawRect(3,3,68,38,FILL_TRANSPARENT); |
| rottenegg | 5:860087ff295e | 47 | lcd.drawRect(4,4,8,8,FILL_BLACK); |
| rottenegg | 5:860087ff295e | 48 | lcd.drawRect(62,32,8,8,FILL_BLACK); |
| rottenegg | 5:860087ff295e | 49 | |
| rottenegg | 5:860087ff295e | 50 | //Move the Charater and Update Movement Animation |
| rottenegg | 5:860087ff295e | 51 | CM.move(lcd); |
| rottenegg | 5:860087ff295e | 52 | lcd.refresh(); |
| rottenegg | 5:860087ff295e | 53 | wait(0.1); |
| rottenegg | 5:860087ff295e | 54 | } |
| rottenegg | 5:860087ff295e | 55 | lcd.clear(); |
| rottenegg | 5:860087ff295e | 56 | //Interative Region 1 Triggered |
| rottenegg | 5:860087ff295e | 57 | if (CM.get_treg() == 0) { |
| rottenegg | 5:860087ff295e | 58 | |
| rottenegg | 5:860087ff295e | 59 | //Delete Interartive Regions |
| rottenegg | 5:860087ff295e | 60 | CM.delete_regions(); |
| rottenegg | 5:860087ff295e | 61 | CM.init(40,20,Bd); |
| rottenegg | 5:860087ff295e | 62 | |
| rottenegg | 5:860087ff295e | 63 | //While Loop Dependent on if Charater is In Screen |
| rottenegg | 5:860087ff295e | 64 | while(CM.in_screen()) { |
| rottenegg | 5:860087ff295e | 65 | lcd.clear(); |
| rottenegg | 5:860087ff295e | 66 | CM.move(lcd); |
| rottenegg | 5:860087ff295e | 67 | lcd.refresh(); |
| rottenegg | 5:860087ff295e | 68 | wait(0.2); |
| rottenegg | 5:860087ff295e | 69 | } |
| rottenegg | 5:860087ff295e | 70 | } |
| rottenegg | 5:860087ff295e | 71 | |
| rottenegg | 5:860087ff295e | 72 | //Interative Region 2 Triggered |
| rottenegg | 5:860087ff295e | 73 | if (CM.get_treg() == 1) { |
| rottenegg | 5:860087ff295e | 74 | for(int i = 0; i < 84; i++) { |
| rottenegg | 5:860087ff295e | 75 | lcd.drawRect(0,0,i,48,FILL_BLACK); |
| rottenegg | 5:860087ff295e | 76 | lcd.refresh(); |
| rottenegg | 5:860087ff295e | 77 | wait(0.05); |
| rottenegg | 5:860087ff295e | 78 | } |
| rottenegg | 5:860087ff295e | 79 | } |
| rottenegg | 5:860087ff295e | 80 | //Ending Screen |
| rottenegg | 5:860087ff295e | 81 | lcd.clear(); |
| rottenegg | 5:860087ff295e | 82 | lcd.drawCircle(40,20,5,FILL_TRANSPARENT); |
| rottenegg | 5:860087ff295e | 83 | lcd.refresh(); |
| rottenegg | 5:860087ff295e | 84 | } |
| rottenegg | 5:860087ff295e | 85 | @endcode |
| rottenegg | 5:860087ff295e | 86 | */ |
| rottenegg | 5:860087ff295e | 87 | |
| rottenegg | 4:34bf3587cf42 | 88 | #include "mbed.h" |
| rottenegg | 4:34bf3587cf42 | 89 | #include "Bitmap.h" |
| rottenegg | 4:34bf3587cf42 | 90 | #include <vector> |
| rottenegg | 4:34bf3587cf42 | 91 | |
| rottenegg | 9:ac396c818874 | 92 | |
| rottenegg | 6:6f84347e9e14 | 93 | /** Enum For Charater's Facing Direction. */ |
| rottenegg | 5:860087ff295e | 94 | enum Direction { |
| rottenegg | 5:860087ff295e | 95 | Lt, /**< Charater Facing Left. */ |
| rottenegg | 5:860087ff295e | 96 | Rt, /**< Charater Facing Right. */ |
| rottenegg | 5:860087ff295e | 97 | Fd, /**< Charater Facing Forward. */ |
| rottenegg | 5:860087ff295e | 98 | Bd /**< Charater Facing Backward. */ |
| rottenegg | 5:860087ff295e | 99 | }; |
| rottenegg | 4:34bf3587cf42 | 100 | |
| rottenegg | 21:f3b0ce18b44f | 101 | |
| rottenegg | 4:34bf3587cf42 | 102 | class CaMove { |
| rottenegg | 5:860087ff295e | 103 | public : |
| rottenegg | 5:860087ff295e | 104 | /** Constructor |
| rottenegg | 5:860087ff295e | 105 | * |
| rottenegg | 5:860087ff295e | 106 | *@param Button Pin for the button to control interations. |
| rottenegg | 5:860087ff295e | 107 | *@param Pot_h Pin for the joystick horizontal potentiometer. |
| rottenegg | 5:860087ff295e | 108 | *@param Pot_v Pin for the joystick vertical potentiometer. |
| rottenegg | 5:860087ff295e | 109 | * |
| rottenegg | 5:860087ff295e | 110 | */ |
| rottenegg | 5:860087ff295e | 111 | CaMove(PinName Button, |
| rottenegg | 5:860087ff295e | 112 | PinName Pot_h, |
| rottenegg | 5:860087ff295e | 113 | PinName Pot_v); |
| rottenegg | 5:860087ff295e | 114 | /** Destructor */ |
| rottenegg | 4:34bf3587cf42 | 115 | ~CaMove(); |
| rottenegg | 5:860087ff295e | 116 | /** Function to initilze the charater spawn point. |
| rottenegg | 5:860087ff295e | 117 | * |
| rottenegg | 5:860087ff295e | 118 | *@param x X-location on screen to spawn at (0 to 84). |
| rottenegg | 5:860087ff295e | 119 | *@param y Y-location on screen to spawn at (0 to 48). |
| rottenegg | 5:860087ff295e | 120 | *@param D In which direction the charater should spawn. |
| rottenegg | 5:860087ff295e | 121 | * |
| rottenegg | 5:860087ff295e | 122 | */ |
| rottenegg | 5:860087ff295e | 123 | void init(int x,int y,Direction D); |
| rottenegg | 21:f3b0ce18b44f | 124 | /** Function that initilizes chasing Charater Models. |
| rottenegg | 21:f3b0ce18b44f | 125 | *@param chaser Enter Integer to select chaser (1-Death Zone , 2- Girl (slow), 4-Girl (fast), other-Ghost) |
| rottenegg | 21:f3b0ce18b44f | 126 | */ |
| rottenegg | 21:f3b0ce18b44f | 127 | void AIinit(int chaser); |
| rottenegg | 21:f3b0ce18b44f | 128 | |
| rottenegg | 21:f3b0ce18b44f | 129 | //Player Functions |
| rottenegg | 21:f3b0ce18b44f | 130 | |
| rottenegg | 21:f3b0ce18b44f | 131 | /** Function that moves the charater but also manages all animations related to its movement and collisions. Double Black lines cannot be crossed and Single Black lines can only be crossed one way. |
| rottenegg | 5:860087ff295e | 132 | * |
| rottenegg | 17:7d4d8905b608 | 133 | *@param lcd Bitmap Class Object. |
| rottenegg | 5:860087ff295e | 134 | * |
| rottenegg | 5:860087ff295e | 135 | */ |
| rottenegg | 17:7d4d8905b608 | 136 | void move(Bitmap &lcd); |
| rottenegg | 5:860087ff295e | 137 | /** Function to check if charater is in the screen. |
| rottenegg | 5:860087ff295e | 138 | * |
| rottenegg | 6:6f84347e9e14 | 139 | *@return Returns true if charater is on the screen otherwise false. |
| rottenegg | 5:860087ff295e | 140 | * |
| rottenegg | 5:860087ff295e | 141 | */ |
| rottenegg | 5:860087ff295e | 142 | bool in_screen(); |
| rottenegg | 21:f3b0ce18b44f | 143 | |
| rottenegg | 21:f3b0ce18b44f | 144 | //Interactive Regions Function pack |
| rottenegg | 21:f3b0ce18b44f | 145 | |
| rottenegg | 5:860087ff295e | 146 | /** Function to set an interative region (rectangle areas only). Everytime it is run creates an additional new region. |
| rottenegg | 5:860087ff295e | 147 | * |
| rottenegg | 5:860087ff295e | 148 | *@param xmin the top left x-location of region. |
| rottenegg | 5:860087ff295e | 149 | *@param ymin the top left y-location of region. |
| rottenegg | 5:860087ff295e | 150 | *@param xl Width of region. |
| rottenegg | 5:860087ff295e | 151 | *@param yl Height of region. |
| rottenegg | 5:860087ff295e | 152 | * |
| rottenegg | 5:860087ff295e | 153 | */ |
| rottenegg | 5:860087ff295e | 154 | void set_region(int xmin, int ymin, int xl, int yl); |
| rottenegg | 5:860087ff295e | 155 | /** Function to delete all interative regions. */ |
| rottenegg | 4:34bf3587cf42 | 156 | void delete_regions(); |
| rottenegg | 5:860087ff295e | 157 | /** Function that checks if the charater triggered an interative region (set to run on button press). */ |
| rottenegg | 4:34bf3587cf42 | 158 | void intercheck(); |
| rottenegg | 5:860087ff295e | 159 | /** Function that checks if an interative region was triggered. |
| rottenegg | 5:860087ff295e | 160 | * |
| rottenegg | 5:860087ff295e | 161 | *@returns Returns true only when an interactive region was triggered for that instance but otherwise remains false. */ |
| rottenegg | 5:860087ff295e | 162 | bool is_trg(); |
| rottenegg | 5:860087ff295e | 163 | /** Function that tells which interactive was triggered. |
| rottenegg | 5:860087ff295e | 164 | * |
| rottenegg | 6:6f84347e9e14 | 165 | *@return Returns an integer based on which region was triggered, regions are labelled according to order of declaration in program counts from 0. Will return -1 if no regions triggered. |
| rottenegg | 5:860087ff295e | 166 | * |
| rottenegg | 5:860087ff295e | 167 | */ |
| rottenegg | 5:860087ff295e | 168 | int get_treg(); |
| rottenegg | 21:f3b0ce18b44f | 169 | |
| rottenegg | 21:f3b0ce18b44f | 170 | //Chaser Functions |
| rottenegg | 21:f3b0ce18b44f | 171 | |
| rottenegg | 9:ac396c818874 | 172 | /** Function that spawns the chasing Charater. |
| rottenegg | 9:ac396c818874 | 173 | * |
| rottenegg | 9:ac396c818874 | 174 | *@param x The x-location where Charater will Spawn. |
| rottenegg | 9:ac396c818874 | 175 | *@param y The y-location where Charater will Spawn. |
| rottenegg | 9:ac396c818874 | 176 | */ |
| rottenegg | 9:ac396c818874 | 177 | void spawn(int x, int y); |
| rottenegg | 9:ac396c818874 | 178 | /** Function that moves the Charater per Frame. |
| rottenegg | 17:7d4d8905b608 | 179 | *@param lcd The Bitmap class object. |
| rottenegg | 9:ac396c818874 | 180 | */ |
| rottenegg | 20:01efa2bce75d | 181 | void chase(Bitmap &lcd, int girl); |
| rottenegg | 9:ac396c818874 | 182 | /** Function that tell if the player is caught by chaser Charater. |
| rottenegg | 9:ac396c818874 | 183 | *@returns Returns a boolean true is caught and false if not. |
| rottenegg | 9:ac396c818874 | 184 | */ |
| rottenegg | 9:ac396c818874 | 185 | bool is_caught(); |
| rottenegg | 5:860087ff295e | 186 | |
| rottenegg | 5:860087ff295e | 187 | private : |
| rottenegg | 5:860087ff295e | 188 | //Hardware Pins |
| rottenegg | 5:860087ff295e | 189 | InterruptIn *_check; |
| rottenegg | 5:860087ff295e | 190 | AnalogIn *_hoz; |
| rottenegg | 5:860087ff295e | 191 | AnalogIn *_ver; |
| rottenegg | 5:860087ff295e | 192 | //Enum declaration |
| rottenegg | 5:860087ff295e | 193 | Direction _fc; |
| rottenegg | 21:f3b0ce18b44f | 194 | //Priavte Variables |
| rottenegg | 4:34bf3587cf42 | 195 | int _h; |
| rottenegg | 4:34bf3587cf42 | 196 | int _v; |
| rottenegg | 4:34bf3587cf42 | 197 | short int _itr; |
| rottenegg | 18:14e5391beccf | 198 | bool LowerH, UpperH, MidH, LowerV, UpperV, MidV; |
| rottenegg | 21:f3b0ce18b44f | 199 | |
| rottenegg | 21:f3b0ce18b44f | 200 | std::vector<int> _vreg; //Vector for storing interactive region x - y locations. |
| rottenegg | 21:f3b0ce18b44f | 201 | |
| rottenegg | 5:860087ff295e | 202 | //Volatile variables for ISR Interrupts |
| rottenegg | 5:860087ff295e | 203 | volatile int _treg; |
| rottenegg | 5:860087ff295e | 204 | volatile bool _trg; |
| rottenegg | 21:f3b0ce18b44f | 205 | |
| rottenegg | 21:f3b0ce18b44f | 206 | //AI Pack Variables |
| rottenegg | 9:ac396c818874 | 207 | bool enabled; |
| rottenegg | 9:ac396c818874 | 208 | int _ch; |
| rottenegg | 9:ac396c818874 | 209 | int _cv; |
| rottenegg | 18:14e5391beccf | 210 | int increment; |
| rottenegg | 18:14e5391beccf | 211 | |
| rottenegg | 21:f3b0ce18b44f | 212 | //Internal Functions |
| rottenegg | 21:f3b0ce18b44f | 213 | //Player Internal Functions |
| rottenegg | 21:f3b0ce18b44f | 214 | void check_collision(bool left,bool right,bool down,bool up); |
| rottenegg | 21:f3b0ce18b44f | 215 | //Interaction Internal Functions |
| rottenegg | 18:14e5391beccf | 216 | void set_statements(int Vvalue); |
| rottenegg | 18:14e5391beccf | 217 | void check_sides(int Vvalue); |
| rottenegg | 21:f3b0ce18b44f | 218 | //Aiding Functions |
| rottenegg | 18:14e5391beccf | 219 | void set_treg(int Vvalue); |
| rottenegg | 21:f3b0ce18b44f | 220 | //Rendering Functions |
| rottenegg | 21:f3b0ce18b44f | 221 | void rend(Bitmap &lcd); |
| rottenegg | 21:f3b0ce18b44f | 222 | void render_chaser(Bitmap &lcd,int chaser); |
| rottenegg | 21:f3b0ce18b44f | 223 | |
| rottenegg | 4:34bf3587cf42 | 224 | }; |
| rottenegg | 4:34bf3587cf42 | 225 | |
| rottenegg | 5:860087ff295e | 226 | #endif |