
Final Commit
Dependencies: mbed
SnakeEngine/SnakeEngine.cpp@27:bd0f69a75d8b, 2018-05-08 (annotated)
- Committer:
- JRM1986
- Date:
- Tue May 08 12:32:46 2018 +0000
- Revision:
- 27:bd0f69a75d8b
- Parent:
- 26:23301f48c1ed
Final Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JRM1986 | 2:ea90cec2489a | 1 | #include "SnakeEngine.h" |
JRM1986 | 2:ea90cec2489a | 2 | |
JRM1986 | 25:f03439ee32c6 | 3 | ///////////////// constructor/destructor ///////////////// |
JRM1986 | 2:ea90cec2489a | 4 | |
JRM1986 | 2:ea90cec2489a | 5 | SnakeEngine::SnakeEngine() |
JRM1986 | 2:ea90cec2489a | 6 | { |
JRM1986 | 2:ea90cec2489a | 7 | |
JRM1986 | 2:ea90cec2489a | 8 | } |
JRM1986 | 2:ea90cec2489a | 9 | |
JRM1986 | 2:ea90cec2489a | 10 | SnakeEngine::~SnakeEngine() |
JRM1986 | 2:ea90cec2489a | 11 | { |
JRM1986 | 2:ea90cec2489a | 12 | |
JRM1986 | 7:c38800a428a6 | 13 | } |
JRM1986 | 23:3081be418c89 | 14 | ///////////////// global methods ///////////////// |
JRM1986 | 17:94dd8a691d4a | 15 | |
JRM1986 | 16:85ca9feccf3f | 16 | int g_tail_length() |
JRM1986 | 16:85ca9feccf3f | 17 | { |
JRM1986 | 16:85ca9feccf3f | 18 | |
JRM1986 | 16:85ca9feccf3f | 19 | extern int g_tl; |
JRM1986 | 16:85ca9feccf3f | 20 | |
JRM1986 | 16:85ca9feccf3f | 21 | return g_tl; |
JRM1986 | 16:85ca9feccf3f | 22 | |
JRM1986 | 16:85ca9feccf3f | 23 | } |
JRM1986 | 16:85ca9feccf3f | 24 | |
JRM1986 | 25:f03439ee32c6 | 25 | void g_frame_time(int frame_time) |
JRM1986 | 25:f03439ee32c6 | 26 | { |
JRM1986 | 25:f03439ee32c6 | 27 | |
JRM1986 | 25:f03439ee32c6 | 28 | g_ft = frame_time; |
JRM1986 | 25:f03439ee32c6 | 29 | |
JRM1986 | 25:f03439ee32c6 | 30 | } |
JRM1986 | 25:f03439ee32c6 | 31 | |
JRM1986 | 17:94dd8a691d4a | 32 | |
JRM1986 | 24:4b180348826e | 33 | ///////////////// public methods ///////////////// |
JRM1986 | 24:4b180348826e | 34 | |
JRM1986 | 25:f03439ee32c6 | 35 | void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y, int n_frames) |
JRM1986 | 7:c38800a428a6 | 36 | { |
JRM1986 | 20:277e88a8185f | 37 | _snake_pos_x = snake_pos_x; |
JRM1986 | 20:277e88a8185f | 38 | _snake_pos_y = snake_pos_y; |
JRM1986 | 20:277e88a8185f | 39 | _in = in; |
JRM1986 | 20:277e88a8185f | 40 | _cur = cur; |
JRM1986 | 25:f03439ee32c6 | 41 | _n_frames = n_frames; |
JRM1986 | 25:f03439ee32c6 | 42 | g_frame_time(_n_frames); |
JRM1986 | 18:406fc298a7c4 | 43 | |
JRM1986 | 20:277e88a8185f | 44 | _snake.init(_in, _cur, _snake_pos_x, _snake_pos_y); |
JRM1986 | 20:277e88a8185f | 45 | |
JRM1986 | 18:406fc298a7c4 | 46 | _food.init(true); |
JRM1986 | 9:561e5681b7a6 | 47 | |
JRM1986 | 8:a2b431b9b3f7 | 48 | } |
JRM1986 | 8:a2b431b9b3f7 | 49 | |
JRM1986 | 10:62d8cb7742c3 | 50 | void SnakeEngine::update(Gamepad &pad) |
JRM1986 | 10:62d8cb7742c3 | 51 | { |
JRM1986 | 16:85ca9feccf3f | 52 | bool food_col; |
JRM1986 | 25:f03439ee32c6 | 53 | int food_spawn_time; |
JRM1986 | 17:94dd8a691d4a | 54 | ++g_engine_fc; |
JRM1986 | 17:94dd8a691d4a | 55 | |
JRM1986 | 25:f03439ee32c6 | 56 | increase_speed(5, 1.1); |
JRM1986 | 26:23301f48c1ed | 57 | food_spawn_time = food_spawning_time(3); |
JRM1986 | 25:f03439ee32c6 | 58 | _snake.update(_in, _cur); |
JRM1986 | 26:23301f48c1ed | 59 | printf("Spawn time %i \n", food_spawn_time); |
JRM1986 | 26:23301f48c1ed | 60 | // printf("Frame Time %i \n", g_ft); |
JRM1986 | 10:62d8cb7742c3 | 61 | |
JRM1986 | 25:f03439ee32c6 | 62 | _food.update(_collision, food_spawn_time); |
JRM1986 | 16:85ca9feccf3f | 63 | food_col = detect_food_collision(pad); |
JRM1986 | 18:406fc298a7c4 | 64 | _collision = food_col; |
JRM1986 | 16:85ca9feccf3f | 65 | set_tail_length(food_col); |
JRM1986 | 17:94dd8a691d4a | 66 | set_tail_array(pad); |
JRM1986 | 25:f03439ee32c6 | 67 | g_wall = detect_wall_collision(pad); |
JRM1986 | 25:f03439ee32c6 | 68 | //printf("%i%", g_wall); |
JRM1986 | 10:62d8cb7742c3 | 69 | |
JRM1986 | 10:62d8cb7742c3 | 70 | } |
JRM1986 | 10:62d8cb7742c3 | 71 | |
JRM1986 | 10:62d8cb7742c3 | 72 | |
JRM1986 | 8:a2b431b9b3f7 | 73 | void SnakeEngine::draw(N5110 &lcd) |
JRM1986 | 8:a2b431b9b3f7 | 74 | { |
JRM1986 | 8:a2b431b9b3f7 | 75 | |
JRM1986 | 26:23301f48c1ed | 76 | // puts frame around the game area |
JRM1986 | 24:4b180348826e | 77 | |
JRM1986 | 8:a2b431b9b3f7 | 78 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
JRM1986 | 8:a2b431b9b3f7 | 79 | lcd.setContrast(0.5); |
JRM1986 | 24:4b180348826e | 80 | |
JRM1986 | 26:23301f48c1ed | 81 | // draws food to lcd |
JRM1986 | 26:23301f48c1ed | 82 | |
JRM1986 | 8:a2b431b9b3f7 | 83 | _food.draw(lcd); |
JRM1986 | 26:23301f48c1ed | 84 | |
JRM1986 | 26:23301f48c1ed | 85 | // draws snake head to lcd |
JRM1986 | 26:23301f48c1ed | 86 | |
JRM1986 | 10:62d8cb7742c3 | 87 | _snake.draw(lcd); |
JRM1986 | 26:23301f48c1ed | 88 | |
JRM1986 | 26:23301f48c1ed | 89 | // draws snake tail to lcd |
JRM1986 | 26:23301f48c1ed | 90 | |
JRM1986 | 17:94dd8a691d4a | 91 | draw_tail(lcd); |
JRM1986 | 9:561e5681b7a6 | 92 | |
JRM1986 | 16:85ca9feccf3f | 93 | } |
JRM1986 | 16:85ca9feccf3f | 94 | |
JRM1986 | 24:4b180348826e | 95 | |
JRM1986 | 24:4b180348826e | 96 | void SnakeEngine::draw_tail(N5110 &lcd) |
JRM1986 | 24:4b180348826e | 97 | { |
JRM1986 | 24:4b180348826e | 98 | int length = g_tl; |
JRM1986 | 24:4b180348826e | 99 | int c; |
JRM1986 | 24:4b180348826e | 100 | c = g_engine_fc % 2; |
JRM1986 | 24:4b180348826e | 101 | int i; |
JRM1986 | 24:4b180348826e | 102 | int x; |
JRM1986 | 24:4b180348826e | 103 | int y; |
JRM1986 | 24:4b180348826e | 104 | |
JRM1986 | 24:4b180348826e | 105 | //printf("Odd/Even %i \n", c); |
JRM1986 | 24:4b180348826e | 106 | |
JRM1986 | 24:4b180348826e | 107 | // runs through each segment in the tail and draws to the lcd |
JRM1986 | 24:4b180348826e | 108 | |
JRM1986 | 24:4b180348826e | 109 | for(i=0; i<=length; ++i) { |
JRM1986 | 24:4b180348826e | 110 | |
JRM1986 | 24:4b180348826e | 111 | // when odd draw odd array |
JRM1986 | 24:4b180348826e | 112 | |
JRM1986 | 24:4b180348826e | 113 | if(c == 1) { |
JRM1986 | 24:4b180348826e | 114 | |
JRM1986 | 24:4b180348826e | 115 | x = g_odd_array_x[i]; |
JRM1986 | 24:4b180348826e | 116 | y = g_odd_array_y[i]; |
JRM1986 | 24:4b180348826e | 117 | |
JRM1986 | 24:4b180348826e | 118 | lcd.setPixel(x,y,true); |
JRM1986 | 24:4b180348826e | 119 | |
JRM1986 | 24:4b180348826e | 120 | } |
JRM1986 | 24:4b180348826e | 121 | |
JRM1986 | 24:4b180348826e | 122 | // when even draw the even array |
JRM1986 | 24:4b180348826e | 123 | |
JRM1986 | 24:4b180348826e | 124 | else if (c == 0) { |
JRM1986 | 24:4b180348826e | 125 | |
JRM1986 | 24:4b180348826e | 126 | x = g_even_array_x[i]; |
JRM1986 | 24:4b180348826e | 127 | y = g_even_array_y[i]; |
JRM1986 | 24:4b180348826e | 128 | |
JRM1986 | 24:4b180348826e | 129 | lcd.setPixel(x,y,true); |
JRM1986 | 24:4b180348826e | 130 | |
JRM1986 | 24:4b180348826e | 131 | } |
JRM1986 | 24:4b180348826e | 132 | |
JRM1986 | 24:4b180348826e | 133 | } |
JRM1986 | 24:4b180348826e | 134 | } |
JRM1986 | 24:4b180348826e | 135 | |
JRM1986 | 24:4b180348826e | 136 | |
JRM1986 | 24:4b180348826e | 137 | void SnakeEngine::get_input(Gamepad &pad) |
JRM1986 | 24:4b180348826e | 138 | { |
JRM1986 | 24:4b180348826e | 139 | |
JRM1986 | 24:4b180348826e | 140 | _in = pad.get_direction(); |
JRM1986 | 24:4b180348826e | 141 | |
JRM1986 | 24:4b180348826e | 142 | } |
JRM1986 | 24:4b180348826e | 143 | |
JRM1986 | 16:85ca9feccf3f | 144 | bool SnakeEngine::detect_food_collision(Gamepad &pad) |
JRM1986 | 16:85ca9feccf3f | 145 | { |
JRM1986 | 16:85ca9feccf3f | 146 | Vector2D snake_pos = _snake.get_snake_position(); |
JRM1986 | 16:85ca9feccf3f | 147 | Vector2D food_pos = _food.get_food_position(); |
JRM1986 | 16:85ca9feccf3f | 148 | |
JRM1986 | 16:85ca9feccf3f | 149 | bool success_flag = false; |
JRM1986 | 16:85ca9feccf3f | 150 | |
JRM1986 | 24:4b180348826e | 151 | // when the position of the head and food are the same return true |
JRM1986 | 24:4b180348826e | 152 | |
JRM1986 | 16:85ca9feccf3f | 153 | if((snake_pos.x == food_pos.x) && (snake_pos.y == food_pos.y)) { |
JRM1986 | 16:85ca9feccf3f | 154 | |
JRM1986 | 16:85ca9feccf3f | 155 | success_flag = true; |
JRM1986 | 16:85ca9feccf3f | 156 | |
JRM1986 | 16:85ca9feccf3f | 157 | } |
JRM1986 | 16:85ca9feccf3f | 158 | |
JRM1986 | 16:85ca9feccf3f | 159 | else { |
JRM1986 | 16:85ca9feccf3f | 160 | |
JRM1986 | 16:85ca9feccf3f | 161 | success_flag = false; |
JRM1986 | 16:85ca9feccf3f | 162 | |
JRM1986 | 16:85ca9feccf3f | 163 | } |
JRM1986 | 16:85ca9feccf3f | 164 | |
JRM1986 | 16:85ca9feccf3f | 165 | return success_flag; |
JRM1986 | 16:85ca9feccf3f | 166 | |
JRM1986 | 16:85ca9feccf3f | 167 | } |
JRM1986 | 19:b437806e579b | 168 | bool SnakeEngine::detect_wall_collision(Gamepad &pad) |
JRM1986 | 19:b437806e579b | 169 | { |
JRM1986 | 19:b437806e579b | 170 | |
JRM1986 | 19:b437806e579b | 171 | Vector2D snake_pos = _snake.get_snake_position(); |
JRM1986 | 19:b437806e579b | 172 | |
JRM1986 | 19:b437806e579b | 173 | bool success_flag = false; |
JRM1986 | 19:b437806e579b | 174 | |
JRM1986 | 24:4b180348826e | 175 | // when an snake position is at an edge return true |
JRM1986 | 24:4b180348826e | 176 | |
JRM1986 | 19:b437806e579b | 177 | if((snake_pos.x == (0 || 84)) || (snake_pos.y == (0 || 48))) { |
JRM1986 | 19:b437806e579b | 178 | |
JRM1986 | 19:b437806e579b | 179 | success_flag = true; |
JRM1986 | 19:b437806e579b | 180 | |
JRM1986 | 19:b437806e579b | 181 | } |
JRM1986 | 19:b437806e579b | 182 | |
JRM1986 | 19:b437806e579b | 183 | return success_flag; |
JRM1986 | 19:b437806e579b | 184 | |
JRM1986 | 19:b437806e579b | 185 | } |
JRM1986 | 19:b437806e579b | 186 | |
JRM1986 | 19:b437806e579b | 187 | |
JRM1986 | 16:85ca9feccf3f | 188 | void SnakeEngine::set_tail_length(bool collision_detected) |
JRM1986 | 16:85ca9feccf3f | 189 | { |
JRM1986 | 24:4b180348826e | 190 | // when a collision is detected increment the tail length |
JRM1986 | 16:85ca9feccf3f | 191 | |
JRM1986 | 16:85ca9feccf3f | 192 | if(collision_detected) { |
JRM1986 | 16:85ca9feccf3f | 193 | |
JRM1986 | 16:85ca9feccf3f | 194 | ++g_tl; |
JRM1986 | 16:85ca9feccf3f | 195 | |
JRM1986 | 16:85ca9feccf3f | 196 | } |
JRM1986 | 16:85ca9feccf3f | 197 | |
JRM1986 | 16:85ca9feccf3f | 198 | else { |
JRM1986 | 16:85ca9feccf3f | 199 | |
JRM1986 | 16:85ca9feccf3f | 200 | g_tl = g_tl; |
JRM1986 | 16:85ca9feccf3f | 201 | |
JRM1986 | 16:85ca9feccf3f | 202 | } |
JRM1986 | 16:85ca9feccf3f | 203 | |
JRM1986 | 16:85ca9feccf3f | 204 | } |
JRM1986 | 16:85ca9feccf3f | 205 | |
JRM1986 | 16:85ca9feccf3f | 206 | int SnakeEngine::get_tail_length() |
JRM1986 | 16:85ca9feccf3f | 207 | { |
JRM1986 | 16:85ca9feccf3f | 208 | |
JRM1986 | 16:85ca9feccf3f | 209 | int length = g_tl; |
JRM1986 | 16:85ca9feccf3f | 210 | |
JRM1986 | 16:85ca9feccf3f | 211 | return length; |
JRM1986 | 16:85ca9feccf3f | 212 | |
JRM1986 | 16:85ca9feccf3f | 213 | } |
JRM1986 | 24:4b180348826e | 214 | |
JRM1986 | 24:4b180348826e | 215 | void SnakeEngine::set_odd_array(Gamepad &pad) |
JRM1986 | 24:4b180348826e | 216 | { |
JRM1986 | 24:4b180348826e | 217 | |
JRM1986 | 24:4b180348826e | 218 | int i; |
JRM1986 | 24:4b180348826e | 219 | int length = g_tl; |
JRM1986 | 24:4b180348826e | 220 | |
JRM1986 | 24:4b180348826e | 221 | extern int g_even_array_x[100]; |
JRM1986 | 24:4b180348826e | 222 | extern int g_even_array_y[100]; |
JRM1986 | 24:4b180348826e | 223 | |
JRM1986 | 24:4b180348826e | 224 | Vector2D pos = _snake.get_snake_position(); |
JRM1986 | 24:4b180348826e | 225 | |
JRM1986 | 24:4b180348826e | 226 | // sets the first value in the array to the new position |
JRM1986 | 24:4b180348826e | 227 | |
JRM1986 | 24:4b180348826e | 228 | g_even_array_x[0] = pos.x; |
JRM1986 | 24:4b180348826e | 229 | g_even_array_y[0] = pos.y; |
JRM1986 | 24:4b180348826e | 230 | |
JRM1986 | 24:4b180348826e | 231 | // starting from the last element in the arrays work back to zero |
JRM1986 | 24:4b180348826e | 232 | |
JRM1986 | 24:4b180348826e | 233 | for(i = length; i >= 0; --i) { |
JRM1986 | 24:4b180348826e | 234 | |
JRM1986 | 24:4b180348826e | 235 | // switch positions of the indexes adding the new head position |
JRM1986 | 24:4b180348826e | 236 | |
JRM1986 | 24:4b180348826e | 237 | if(i > 0) { |
JRM1986 | 24:4b180348826e | 238 | |
JRM1986 | 24:4b180348826e | 239 | g_odd_array_x[i] = g_even_array_x[i-1]; |
JRM1986 | 24:4b180348826e | 240 | g_odd_array_y[i] = g_even_array_y[i-1]; |
JRM1986 | 24:4b180348826e | 241 | |
JRM1986 | 24:4b180348826e | 242 | } |
JRM1986 | 24:4b180348826e | 243 | |
JRM1986 | 24:4b180348826e | 244 | // when snake has no length position 0 is the new position |
JRM1986 | 24:4b180348826e | 245 | |
JRM1986 | 24:4b180348826e | 246 | else if(i == 0) { |
JRM1986 | 24:4b180348826e | 247 | |
JRM1986 | 24:4b180348826e | 248 | g_odd_array_x[0] = pos.x; |
JRM1986 | 24:4b180348826e | 249 | g_odd_array_y[0] = pos.y; |
JRM1986 | 24:4b180348826e | 250 | |
JRM1986 | 24:4b180348826e | 251 | } |
JRM1986 | 24:4b180348826e | 252 | |
JRM1986 | 24:4b180348826e | 253 | } |
JRM1986 | 24:4b180348826e | 254 | |
JRM1986 | 24:4b180348826e | 255 | } |
JRM1986 | 24:4b180348826e | 256 | |
JRM1986 | 24:4b180348826e | 257 | void SnakeEngine::set_even_array(Gamepad &pad) |
JRM1986 | 24:4b180348826e | 258 | { |
JRM1986 | 24:4b180348826e | 259 | |
JRM1986 | 24:4b180348826e | 260 | int i; |
JRM1986 | 24:4b180348826e | 261 | int length = g_tl; |
JRM1986 | 24:4b180348826e | 262 | |
JRM1986 | 24:4b180348826e | 263 | extern int g_odd_array_x[100]; |
JRM1986 | 24:4b180348826e | 264 | extern int g_odd_array_y[100]; |
JRM1986 | 24:4b180348826e | 265 | |
JRM1986 | 24:4b180348826e | 266 | Vector2D pos = _snake.get_snake_position(); |
JRM1986 | 24:4b180348826e | 267 | |
JRM1986 | 24:4b180348826e | 268 | // sets the first value in the array to the new position |
JRM1986 | 24:4b180348826e | 269 | |
JRM1986 | 24:4b180348826e | 270 | g_odd_array_x[0] = pos.x; |
JRM1986 | 24:4b180348826e | 271 | g_odd_array_y[0] = pos.y; |
JRM1986 | 24:4b180348826e | 272 | |
JRM1986 | 24:4b180348826e | 273 | // starting from the last element in the arrays work back to zero |
JRM1986 | 24:4b180348826e | 274 | |
JRM1986 | 24:4b180348826e | 275 | for(i = length; i >= 0; --i) { |
JRM1986 | 24:4b180348826e | 276 | |
JRM1986 | 24:4b180348826e | 277 | // switch positions of the indexes adding the new head position |
JRM1986 | 24:4b180348826e | 278 | |
JRM1986 | 24:4b180348826e | 279 | if(i > 0) { |
JRM1986 | 24:4b180348826e | 280 | |
JRM1986 | 24:4b180348826e | 281 | g_even_array_x[i] = g_odd_array_x[i-1]; |
JRM1986 | 24:4b180348826e | 282 | g_even_array_y[i] = g_odd_array_y[i-1]; |
JRM1986 | 24:4b180348826e | 283 | |
JRM1986 | 24:4b180348826e | 284 | } |
JRM1986 | 24:4b180348826e | 285 | |
JRM1986 | 24:4b180348826e | 286 | // when snake has no length position 0 is the new position |
JRM1986 | 24:4b180348826e | 287 | |
JRM1986 | 24:4b180348826e | 288 | else if(i == 0) { |
JRM1986 | 24:4b180348826e | 289 | |
JRM1986 | 24:4b180348826e | 290 | g_even_array_x[0] = pos.x; |
JRM1986 | 24:4b180348826e | 291 | g_even_array_y[0] = pos.y; |
JRM1986 | 24:4b180348826e | 292 | |
JRM1986 | 24:4b180348826e | 293 | } |
JRM1986 | 24:4b180348826e | 294 | |
JRM1986 | 24:4b180348826e | 295 | } |
JRM1986 | 24:4b180348826e | 296 | |
JRM1986 | 24:4b180348826e | 297 | } |
JRM1986 | 24:4b180348826e | 298 | |
JRM1986 | 24:4b180348826e | 299 | |
JRM1986 | 24:4b180348826e | 300 | void SnakeEngine::set_tail_array(Gamepad &pad) |
JRM1986 | 24:4b180348826e | 301 | { |
JRM1986 | 24:4b180348826e | 302 | |
JRM1986 | 24:4b180348826e | 303 | int c = g_engine_fc % 2; |
JRM1986 | 24:4b180348826e | 304 | |
JRM1986 | 24:4b180348826e | 305 | // when the frame counter is odd set odd array |
JRM1986 | 24:4b180348826e | 306 | |
JRM1986 | 24:4b180348826e | 307 | if(c == 1) { |
JRM1986 | 24:4b180348826e | 308 | |
JRM1986 | 24:4b180348826e | 309 | set_odd_array(pad); |
JRM1986 | 24:4b180348826e | 310 | |
JRM1986 | 24:4b180348826e | 311 | } |
JRM1986 | 24:4b180348826e | 312 | |
JRM1986 | 24:4b180348826e | 313 | // when the frame counter is even set even array |
JRM1986 | 24:4b180348826e | 314 | |
JRM1986 | 24:4b180348826e | 315 | else if (c == 0) { |
JRM1986 | 24:4b180348826e | 316 | |
JRM1986 | 24:4b180348826e | 317 | set_even_array(pad); |
JRM1986 | 24:4b180348826e | 318 | |
JRM1986 | 24:4b180348826e | 319 | } |
JRM1986 | 24:4b180348826e | 320 | |
JRM1986 | 24:4b180348826e | 321 | } |
JRM1986 | 24:4b180348826e | 322 | |
JRM1986 | 25:f03439ee32c6 | 323 | int SnakeEngine::food_spawning_time(int frame_decrementer) |
JRM1986 | 25:f03439ee32c6 | 324 | { |
JRM1986 | 25:f03439ee32c6 | 325 | |
JRM1986 | 25:f03439ee32c6 | 326 | int c = 0; |
JRM1986 | 26:23301f48c1ed | 327 | |
JRM1986 | 26:23301f48c1ed | 328 | // c is equal to the tail length - an incrementer such that c is never more |
JRM1986 | 26:23301f48c1ed | 329 | // than 0 and n = frame decremtner away from 0 such that the logic |
JRM1986 | 26:23301f48c1ed | 330 | // c == 0 works |
JRM1986 | 26:23301f48c1ed | 331 | |
JRM1986 | 25:f03439ee32c6 | 332 | c = g_tl - g_n; |
JRM1986 | 25:f03439ee32c6 | 333 | |
JRM1986 | 25:f03439ee32c6 | 334 | //printf("C = %i", c); |
JRM1986 | 25:f03439ee32c6 | 335 | |
JRM1986 | 26:23301f48c1ed | 336 | // the frame periodity must be more than 100 to ensure the game reamains playable |
JRM1986 | 26:23301f48c1ed | 337 | |
JRM1986 | 25:f03439ee32c6 | 338 | if((c == 0) && (g_ft > 100)) { |
JRM1986 | 25:f03439ee32c6 | 339 | |
JRM1986 | 25:f03439ee32c6 | 340 | g_n = g_n + frame_decrementer; |
JRM1986 | 25:f03439ee32c6 | 341 | |
JRM1986 | 25:f03439ee32c6 | 342 | g_ft -= 10; |
JRM1986 | 25:f03439ee32c6 | 343 | |
JRM1986 | 25:f03439ee32c6 | 344 | frame_decrementer += frame_decrementer; |
JRM1986 | 25:f03439ee32c6 | 345 | |
JRM1986 | 25:f03439ee32c6 | 346 | } |
JRM1986 | 25:f03439ee32c6 | 347 | |
JRM1986 | 25:f03439ee32c6 | 348 | return g_ft; |
JRM1986 | 25:f03439ee32c6 | 349 | |
JRM1986 | 25:f03439ee32c6 | 350 | } |
JRM1986 | 25:f03439ee32c6 | 351 | |
JRM1986 | 25:f03439ee32c6 | 352 | void SnakeEngine::increase_speed(int resetter, float speed_decrementer) |
JRM1986 | 25:f03439ee32c6 | 353 | { |
JRM1986 | 25:f03439ee32c6 | 354 | |
JRM1986 | 25:f03439ee32c6 | 355 | int c = 0; |
JRM1986 | 25:f03439ee32c6 | 356 | c = g_tl - g_n; |
JRM1986 | 25:f03439ee32c6 | 357 | if(g_tl == 0) { |
JRM1986 | 25:f03439ee32c6 | 358 | |
JRM1986 | 25:f03439ee32c6 | 359 | g_speed = 0.4; |
JRM1986 | 16:85ca9feccf3f | 360 | |
JRM1986 | 25:f03439ee32c6 | 361 | } |
JRM1986 | 25:f03439ee32c6 | 362 | |
JRM1986 | 25:f03439ee32c6 | 363 | if(c == 0) { |
JRM1986 | 25:f03439ee32c6 | 364 | |
JRM1986 | 25:f03439ee32c6 | 365 | g_n = g_n + resetter; |
JRM1986 | 25:f03439ee32c6 | 366 | |
JRM1986 | 25:f03439ee32c6 | 367 | g_speed = g_speed/speed_decrementer; |
JRM1986 | 25:f03439ee32c6 | 368 | |
JRM1986 | 25:f03439ee32c6 | 369 | resetter += resetter; |
JRM1986 | 25:f03439ee32c6 | 370 | |
JRM1986 | 25:f03439ee32c6 | 371 | } |
JRM1986 | 25:f03439ee32c6 | 372 | |
JRM1986 | 25:f03439ee32c6 | 373 | } |
JRM1986 | 25:f03439ee32c6 | 374 |