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
Sprites/Sprites.h@49:9bea7089b657, 2019-04-24 (annotated)
- Committer:
- el17arm
- Date:
- Wed Apr 24 16:45:47 2019 +0000
- Revision:
- 49:9bea7089b657
- Parent:
- 45:bad704c546d4
- Child:
- 50:65ba437510f8
Sprites documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17arm | 33:023f57d52b1c | 1 | #ifndef SPRITES_H |
el17arm | 33:023f57d52b1c | 2 | #define SPRITES_H |
el17arm | 33:023f57d52b1c | 3 | |
el17arm | 33:023f57d52b1c | 4 | #include "mbed.h" |
el17arm | 33:023f57d52b1c | 5 | #include "N5110.h" |
el17arm | 33:023f57d52b1c | 6 | #include "Gamepad.h" |
el17arm | 41:0cf320f73424 | 7 | |
el17arm | 45:bad704c546d4 | 8 | /** Sprites Class |
el17arm | 45:bad704c546d4 | 9 | @author Andrew Milner University of Leeds |
el17arm | 45:bad704c546d4 | 10 | @brief Draws sprites and sets all collision rules |
el17arm | 45:bad704c546d4 | 11 | @date April 2019 |
el17arm | 45:bad704c546d4 | 12 | */ |
el17arm | 45:bad704c546d4 | 13 | |
el17arm | 45:bad704c546d4 | 14 | /////////// All sprite bitmaps//////////////// |
el17arm | 33:023f57d52b1c | 15 | |
el17arm | 49:9bea7089b657 | 16 | // right facing miner bitmap |
el17arm | 33:023f57d52b1c | 17 | const int miner_right[24] = { |
el17arm | 41:0cf320f73424 | 18 | |
el17arm | 33:023f57d52b1c | 19 | 1,1,1, |
el17arm | 33:023f57d52b1c | 20 | 1,1,0, |
el17arm | 33:023f57d52b1c | 21 | 1,1,1, |
el17arm | 33:023f57d52b1c | 22 | 0,1,0, |
el17arm | 33:023f57d52b1c | 23 | 1,1,1, |
el17arm | 33:023f57d52b1c | 24 | 1,1,1, |
el17arm | 33:023f57d52b1c | 25 | 0,1,0, |
el17arm | 33:023f57d52b1c | 26 | 0,1,1, |
el17arm | 33:023f57d52b1c | 27 | |
el17arm | 33:023f57d52b1c | 28 | }; |
el17arm | 49:9bea7089b657 | 29 | // left facing miner bitmap |
el17arm | 49:9bea7089b657 | 30 | const int miner_left[24] = { |
el17arm | 41:0cf320f73424 | 31 | |
el17arm | 33:023f57d52b1c | 32 | 1,1,1, |
el17arm | 33:023f57d52b1c | 33 | 0,1,1, |
el17arm | 33:023f57d52b1c | 34 | 1,1,1, |
el17arm | 33:023f57d52b1c | 35 | 0,1,0, |
el17arm | 33:023f57d52b1c | 36 | 1,1,1, |
el17arm | 33:023f57d52b1c | 37 | 1,1,1, |
el17arm | 33:023f57d52b1c | 38 | 0,1,0, |
el17arm | 33:023f57d52b1c | 39 | 1,1,0, |
el17arm | 33:023f57d52b1c | 40 | }; |
el17arm | 49:9bea7089b657 | 41 | // enemy bitmap |
el17arm | 33:023f57d52b1c | 42 | const int enemy[15] = { |
el17arm | 33:023f57d52b1c | 43 | |
el17arm | 33:023f57d52b1c | 44 | 1,1,1, |
el17arm | 33:023f57d52b1c | 45 | 1,0,1, |
el17arm | 33:023f57d52b1c | 46 | 1,1,1, |
el17arm | 33:023f57d52b1c | 47 | 0,1,0, |
el17arm | 33:023f57d52b1c | 48 | 1,1,1, |
el17arm | 33:023f57d52b1c | 49 | }; |
el17arm | 49:9bea7089b657 | 50 | // key bitmap |
el17arm | 33:023f57d52b1c | 51 | const int key[12] = { |
el17arm | 41:0cf320f73424 | 52 | |
el17arm | 33:023f57d52b1c | 53 | 1,1,0,0, |
el17arm | 33:023f57d52b1c | 54 | 1,0,1,1, |
el17arm | 33:023f57d52b1c | 55 | 1,1,0,1, |
el17arm | 33:023f57d52b1c | 56 | }; |
el17arm | 49:9bea7089b657 | 57 | // blank bitmap to show key collected |
el17arm | 33:023f57d52b1c | 58 | const int key_collected[12] = { |
el17arm | 33:023f57d52b1c | 59 | |
el17arm | 33:023f57d52b1c | 60 | 0,0,0,0, |
el17arm | 33:023f57d52b1c | 61 | 0,0,0,0, |
el17arm | 33:023f57d52b1c | 62 | 0,0,0,0, |
el17arm | 33:023f57d52b1c | 63 | }; |
el17arm | 49:9bea7089b657 | 64 | // trap bitmap |
el17arm | 33:023f57d52b1c | 65 | const int spike[9] = { |
el17arm | 33:023f57d52b1c | 66 | |
el17arm | 34:c5a042973e0c | 67 | 1,0,1, |
el17arm | 33:023f57d52b1c | 68 | 0,1,0, |
el17arm | 34:c5a042973e0c | 69 | 1,0,1, |
el17arm | 33:023f57d52b1c | 70 | }; |
el17arm | 49:9bea7089b657 | 71 | // solid block bitmap |
el17arm | 34:c5a042973e0c | 72 | const int solid_block[18] = { |
el17arm | 33:023f57d52b1c | 73 | |
el17arm | 34:c5a042973e0c | 74 | 1,1,1,1,1,1, |
el17arm | 34:c5a042973e0c | 75 | 1,0,1,1,0,1, |
el17arm | 34:c5a042973e0c | 76 | 1,1,1,1,1,1, |
el17arm | 33:023f57d52b1c | 77 | }; |
el17arm | 49:9bea7089b657 | 78 | // level exit bitmap |
el17arm | 33:023f57d52b1c | 79 | const int door[30] = { |
el17arm | 33:023f57d52b1c | 80 | |
el17arm | 33:023f57d52b1c | 81 | 1,1,1,1,1, |
el17arm | 33:023f57d52b1c | 82 | 1,0,1,0,1, |
el17arm | 33:023f57d52b1c | 83 | 1,1,0,1,1, |
el17arm | 33:023f57d52b1c | 84 | 1,0,1,0,1, |
el17arm | 33:023f57d52b1c | 85 | 1,1,0,1,1, |
el17arm | 33:023f57d52b1c | 86 | 1,0,1,0,1, |
el17arm | 33:023f57d52b1c | 87 | }; |
el17arm | 38:55bb9da08a52 | 88 | |
el17arm | 45:bad704c546d4 | 89 | /////////structs for sprite parameters and collision detection////////////// |
el17arm | 38:55bb9da08a52 | 90 | |
el17arm | 49:9bea7089b657 | 91 | /** Keys position struct */ |
el17arm | 49:9bea7089b657 | 92 | |
el17arm | 38:55bb9da08a52 | 93 | struct keyed { |
el17arm | 49:9bea7089b657 | 94 | bool key[5]; /**< initialise key flag array identifies each key individually */ |
el17arm | 49:9bea7089b657 | 95 | double kx[5]; /**< key x position*/ |
el17arm | 49:9bea7089b657 | 96 | double ky[5]; /**< key y position*/ |
el17arm | 38:55bb9da08a52 | 97 | }; |
el17arm | 49:9bea7089b657 | 98 | /** Enemy position and movement struct */ |
el17arm | 40:913339e324b8 | 99 | struct enemies_init { |
el17arm | 49:9bea7089b657 | 100 | bool eflag[5]; /**< initialise enemy flag array identifies each key individually */ |
el17arm | 49:9bea7089b657 | 101 | double ex[5]; /**< enemy x position */ |
el17arm | 49:9bea7089b657 | 102 | double ey[5]; /**< enemy y position */ |
el17arm | 49:9bea7089b657 | 103 | int counter[5]; /**< enemy counter, counts pixels moved */ |
el17arm | 49:9bea7089b657 | 104 | int distance[5];/**< distance enemy will travel before turning round */ |
el17arm | 40:913339e324b8 | 105 | }; |
el17arm | 49:9bea7089b657 | 106 | /** Solid blocks struct */ |
el17arm | 43:1ec6b6293c6b | 107 | struct Solid_blocks_init { |
el17arm | 49:9bea7089b657 | 108 | double bx[5]; /**< block x position array identifies each block individually */ |
el17arm | 49:9bea7089b657 | 109 | double by[5]; /**< block y position array */ |
el17arm | 49:9bea7089b657 | 110 | bool collision[5]; /**< collision indicator for each individual block */ |
el17arm | 40:913339e324b8 | 111 | |
el17arm | 43:1ec6b6293c6b | 112 | }; |
el17arm | 49:9bea7089b657 | 113 | /////////////structs for populating up to 6 levels////////////////// |
el17arm | 49:9bea7089b657 | 114 | /** Enemy location for each level */ |
el17arm | 38:55bb9da08a52 | 115 | struct Enemies { |
el17arm | 49:9bea7089b657 | 116 | double ex[5]; /**< enemy x positions for each level */ |
el17arm | 49:9bea7089b657 | 117 | double ey[5]; /**< enemy y positions for each level */ |
el17arm | 49:9bea7089b657 | 118 | int c[5]; /**< enemy counters for each level */ |
el17arm | 49:9bea7089b657 | 119 | int d[5]; /**< enemy travel distances for each level */ |
el17arm | 49:9bea7089b657 | 120 | double v[5]; /**< varies enemies velocity */ |
el17arm | 38:55bb9da08a52 | 121 | }; |
el17arm | 38:55bb9da08a52 | 122 | |
el17arm | 38:55bb9da08a52 | 123 | struct Traps { |
el17arm | 49:9bea7089b657 | 124 | double tx[5]; /**< traps x positions for each level */ |
el17arm | 49:9bea7089b657 | 125 | double ty[5]; /**< traps y positions for each level */ |
el17arm | 38:55bb9da08a52 | 126 | }; |
el17arm | 38:55bb9da08a52 | 127 | |
el17arm | 38:55bb9da08a52 | 128 | struct Keys { |
el17arm | 49:9bea7089b657 | 129 | double kx[5]; /**< keys x positions for each level */ |
el17arm | 49:9bea7089b657 | 130 | double ky[5]; /**< keys y positions for each level */ |
el17arm | 38:55bb9da08a52 | 131 | }; |
el17arm | 38:55bb9da08a52 | 132 | |
el17arm | 38:55bb9da08a52 | 133 | struct Solid_blocks { |
el17arm | 49:9bea7089b657 | 134 | double bx[5]; /**< blocks x positions for each level */ |
el17arm | 49:9bea7089b657 | 135 | double by[5]; /**< blocks y positions for each level */ |
el17arm | 38:55bb9da08a52 | 136 | }; |
el17arm | 38:55bb9da08a52 | 137 | |
el17arm | 38:55bb9da08a52 | 138 | struct Soft_blocks { |
el17arm | 49:9bea7089b657 | 139 | double sx1[5]; /**< sinking blocks x1 positions for each level */ |
el17arm | 49:9bea7089b657 | 140 | double sy[5]; /**< sinking blocks y positions for each level */ |
el17arm | 49:9bea7089b657 | 141 | int sx2[5]; /**< sinking blocks x2 (length) positions for each level */ |
el17arm | 40:913339e324b8 | 142 | }; |
el17arm | 40:913339e324b8 | 143 | |
el17arm | 40:913339e324b8 | 144 | struct Level_exit { |
el17arm | 49:9bea7089b657 | 145 | double lx[5]; /**< Exit x position for each level */ |
el17arm | 49:9bea7089b657 | 146 | double ly[5]; /**< Exit y position for each level */ |
el17arm | 33:023f57d52b1c | 147 | }; |
el17arm | 33:023f57d52b1c | 148 | |
el17arm | 33:023f57d52b1c | 149 | class Sprites |
el17arm | 33:023f57d52b1c | 150 | { |
el17arm | 33:023f57d52b1c | 151 | |
el17arm | 33:023f57d52b1c | 152 | public: |
el17arm | 49:9bea7089b657 | 153 | /** constructor |
el17arm | 49:9bea7089b657 | 154 | */ |
el17arm | 33:023f57d52b1c | 155 | Sprites(); |
el17arm | 49:9bea7089b657 | 156 | /** deconstructor |
el17arm | 49:9bea7089b657 | 157 | */ |
el17arm | 33:023f57d52b1c | 158 | ~Sprites(); |
el17arm | 49:9bea7089b657 | 159 | /** gets x and y position of player |
el17arm | 49:9bea7089b657 | 160 | */ |
el17arm | 41:0cf320f73424 | 161 | Vector2D get_pos(); |
el17arm | 49:9bea7089b657 | 162 | /** States starting position of player |
el17arm | 49:9bea7089b657 | 163 | @param x position |
el17arm | 49:9bea7089b657 | 164 | @param y position |
el17arm | 49:9bea7089b657 | 165 | */ |
el17arm | 33:023f57d52b1c | 166 | void miner_init(int x, int y); |
el17arm | 45:bad704c546d4 | 167 | void miner_move(Direction d, N5110 &lcd); |
el17arm | 33:023f57d52b1c | 168 | void miner_draw(N5110 &lcd); |
el17arm | 33:023f57d52b1c | 169 | void miner_gravity(N5110 &lcd); |
el17arm | 45:bad704c546d4 | 170 | void miner_jump(N5110 &lcd, Gamepad &pad); |
el17arm | 33:023f57d52b1c | 171 | void miner_land(N5110 &lcd); |
el17arm | 33:023f57d52b1c | 172 | |
el17arm | 33:023f57d52b1c | 173 | void enemy_init(int i, int x, int y, int d); |
el17arm | 33:023f57d52b1c | 174 | void enemy_move(int i, double v, N5110 &lcd); |
el17arm | 33:023f57d52b1c | 175 | bool enemy_collision(int i); |
el17arm | 33:023f57d52b1c | 176 | |
el17arm | 33:023f57d52b1c | 177 | void key_collect(int k, int x, int y, N5110 &lcd, Gamepad &pad); |
el17arm | 40:913339e324b8 | 178 | int keys_collected(); |
el17arm | 33:023f57d52b1c | 179 | |
el17arm | 37:4d525a37d5d2 | 180 | bool trap(int x, int y, N5110 &lcd); |
el17arm | 43:1ec6b6293c6b | 181 | void blocks(Direction d, int i, int x, int y, N5110 &lcd); |
el17arm | 43:1ec6b6293c6b | 182 | bool block_collision(); |
el17arm | 38:55bb9da08a52 | 183 | void soft_blocks(int x, int y, int z, N5110 &lcd); |
el17arm | 33:023f57d52b1c | 184 | |
el17arm | 41:0cf320f73424 | 185 | bool exit_level(int x, int y, N5110 &lcd); |
el17arm | 49:9bea7089b657 | 186 | |
el17arm | 43:1ec6b6293c6b | 187 | keyed _k; |
el17arm | 44:e29458976114 | 188 | enemies_init _enem; |
el17arm | 41:0cf320f73424 | 189 | |
el17arm | 43:1ec6b6293c6b | 190 | |
el17arm | 38:55bb9da08a52 | 191 | |
el17arm | 33:023f57d52b1c | 192 | private: |
el17arm | 33:023f57d52b1c | 193 | |
el17arm | 33:023f57d52b1c | 194 | int _direction; |
el17arm | 33:023f57d52b1c | 195 | int _gravity; |
el17arm | 33:023f57d52b1c | 196 | bool _jump; |
el17arm | 33:023f57d52b1c | 197 | int _y; |
el17arm | 33:023f57d52b1c | 198 | int _x; |
el17arm | 33:023f57d52b1c | 199 | bool _j_flag; |
el17arm | 33:023f57d52b1c | 200 | int _j_counter; |
el17arm | 33:023f57d52b1c | 201 | |
el17arm | 34:c5a042973e0c | 202 | bool block[5]; |
el17arm | 36:203ec93b4cc7 | 203 | bool right_collision; |
el17arm | 36:203ec93b4cc7 | 204 | bool left_collision; |
el17arm | 36:203ec93b4cc7 | 205 | |
el17arm | 36:203ec93b4cc7 | 206 | bool _collision; |
el17arm | 36:203ec93b4cc7 | 207 | int _keys; |
el17arm | 36:203ec93b4cc7 | 208 | |
el17arm | 38:55bb9da08a52 | 209 | |
el17arm | 43:1ec6b6293c6b | 210 | Solid_blocks_init _b; |
el17arm | 44:e29458976114 | 211 | |
el17arm | 33:023f57d52b1c | 212 | }; |
el17arm | 33:023f57d52b1c | 213 | #endif |