
Final Commit
Dependencies: mbed
SnakeEngine/SnakeEngine.h@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 | #ifndef SNAKEENGINE_H |
JRM1986 | 2:ea90cec2489a | 2 | #define SNAKEENGINE_H |
JRM1986 | 2:ea90cec2489a | 3 | |
JRM1986 | 2:ea90cec2489a | 4 | #include "mbed.h" |
JRM1986 | 3:50f01159c61d | 5 | #include "Gamepad.h" |
JRM1986 | 2:ea90cec2489a | 6 | #include "N5110.h" |
JRM1986 | 6:f3f508cea1c4 | 7 | #include "Food.h" |
JRM1986 | 6:f3f508cea1c4 | 8 | #include "Snake.h" |
JRM1986 | 2:ea90cec2489a | 9 | |
JRM1986 | 13:72bc2579e85e | 10 | |
JRM1986 | 2:ea90cec2489a | 11 | /** SnakeEngine Class |
JRM1986 | 1:c3fdbc4b1293 | 12 | * @brief Class that initialises and defines the game charectoristics ready for the main file |
JRM1986 | 1:c3fdbc4b1293 | 13 | * @author Joshua R. Marshall |
JRM1986 | 1:c3fdbc4b1293 | 14 | * @date Feb, 2018 |
JRM1986 | 2:ea90cec2489a | 15 | */ |
JRM1986 | 16:85ca9feccf3f | 16 | |
JRM1986 | 26:23301f48c1ed | 17 | static Vector2D g_tp; |
JRM1986 | 25:f03439ee32c6 | 18 | void g_frame_time(int frame_time); |
JRM1986 | 25:f03439ee32c6 | 19 | |
JRM1986 | 16:85ca9feccf3f | 20 | |
JRM1986 | 25:f03439ee32c6 | 21 | static int g_tl; // tail length |
JRM1986 | 25:f03439ee32c6 | 22 | static int g_engine_fc; // frame counter for draw tail functions |
JRM1986 | 25:f03439ee32c6 | 23 | static int g_ft; // frame counter for decreasing time between food spawning |
JRM1986 | 25:f03439ee32c6 | 24 | static int g_n; // ensures spawning of food occurs periodically |
JRM1986 | 25:f03439ee32c6 | 25 | static bool g_wall; // wall collision detected |
JRM1986 | 25:f03439ee32c6 | 26 | |
JRM1986 | 25:f03439ee32c6 | 27 | /** Returns the speed |
JRM1986 | 25:f03439ee32c6 | 28 | */ |
JRM1986 | 25:f03439ee32c6 | 29 | static double g_speed; |
JRM1986 | 25:f03439ee32c6 | 30 | |
JRM1986 | 25:f03439ee32c6 | 31 | static int g_odd_array_x[100]; // array to store odd values for x component of tail segments |
JRM1986 | 25:f03439ee32c6 | 32 | static int g_odd_array_y[100]; // array to store odd values for y component of tail segments |
JRM1986 | 25:f03439ee32c6 | 33 | static int g_even_array_x[100]; // array to store even values for x component of tail segments |
JRM1986 | 25:f03439ee32c6 | 34 | static int g_even_array_y[100]; // array to store even values for y component of tail segments |
JRM1986 | 16:85ca9feccf3f | 35 | |
JRM1986 | 16:85ca9feccf3f | 36 | |
JRM1986 | 2:ea90cec2489a | 37 | class SnakeEngine |
JRM1986 | 2:ea90cec2489a | 38 | { |
JRM1986 | 2:ea90cec2489a | 39 | |
JRM1986 | 2:ea90cec2489a | 40 | public: |
JRM1986 | 6:f3f508cea1c4 | 41 | |
JRM1986 | 2:ea90cec2489a | 42 | SnakeEngine(); // constructor |
JRM1986 | 2:ea90cec2489a | 43 | ~SnakeEngine(); // destructor |
JRM1986 | 6:f3f508cea1c4 | 44 | |
JRM1986 | 23:3081be418c89 | 45 | |
JRM1986 | 23:3081be418c89 | 46 | /** Initialises the directions for the snake and the position for the food |
JRM1986 | 23:3081be418c89 | 47 | * @param Input direction from pad |
JRM1986 | 23:3081be418c89 | 48 | * @param Current direction of snake |
JRM1986 | 23:3081be418c89 | 49 | * @param pos_x x position |
JRM1986 | 25:f03439ee32c6 | 50 | * @param pos_y y position |
JRM1986 | 25:f03439ee32c6 | 51 | * @param sets initial number of frames between food spawning |
JRM1986 | 23:3081be418c89 | 52 | */ |
JRM1986 | 23:3081be418c89 | 53 | |
JRM1986 | 25:f03439ee32c6 | 54 | void init(Direction in, Direction cur, int pos_x, int pos_y, int n_frames); |
JRM1986 | 23:3081be418c89 | 55 | |
JRM1986 | 23:3081be418c89 | 56 | /** Updates the Food and Snake classes from the Gamepad |
JRM1986 | 23:3081be418c89 | 57 | */ |
JRM1986 | 23:3081be418c89 | 58 | |
JRM1986 | 23:3081be418c89 | 59 | void update(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 60 | |
JRM1986 | 24:4b180348826e | 61 | /** Draws the game to the lcd */ |
JRM1986 | 24:4b180348826e | 62 | |
JRM1986 | 24:4b180348826e | 63 | void draw(N5110 &lcd); |
JRM1986 | 24:4b180348826e | 64 | |
JRM1986 | 24:4b180348826e | 65 | /** Draws the tail segments on the lcd */ |
JRM1986 | 24:4b180348826e | 66 | |
JRM1986 | 24:4b180348826e | 67 | void draw_tail(N5110 &lcd); |
JRM1986 | 24:4b180348826e | 68 | |
JRM1986 | 23:3081be418c89 | 69 | /** Gets the updated direction from the Gamepad and stores it in a struct |
JRM1986 | 23:3081be418c89 | 70 | */ |
JRM1986 | 23:3081be418c89 | 71 | |
JRM1986 | 23:3081be418c89 | 72 | void get_input(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 73 | |
JRM1986 | 23:3081be418c89 | 74 | /** Check if snake has eaten food |
JRM1986 | 23:3081be418c89 | 75 | */ |
JRM1986 | 6:f3f508cea1c4 | 76 | |
JRM1986 | 16:85ca9feccf3f | 77 | bool detect_food_collision(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 78 | |
JRM1986 | 23:3081be418c89 | 79 | /** Checks if snake has hit wall |
JRM1986 | 23:3081be418c89 | 80 | */ |
JRM1986 | 19:b437806e579b | 81 | bool detect_wall_collision(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 82 | |
JRM1986 | 23:3081be418c89 | 83 | /** Checks if snake has hit it's own tail |
JRM1986 | 23:3081be418c89 | 84 | */ |
JRM1986 | 23:3081be418c89 | 85 | |
JRM1986 | 16:85ca9feccf3f | 86 | bool detect_tail_collision(); |
JRM1986 | 16:85ca9feccf3f | 87 | |
JRM1986 | 23:3081be418c89 | 88 | /** Increments the length variable for tail when food is eaten |
JRM1986 | 23:3081be418c89 | 89 | * @param Checks if food and head have collided (been eaten) |
JRM1986 | 23:3081be418c89 | 90 | */ |
JRM1986 | 23:3081be418c89 | 91 | |
JRM1986 | 16:85ca9feccf3f | 92 | void set_tail_length(bool collision_detected); |
JRM1986 | 23:3081be418c89 | 93 | |
JRM1986 | 23:3081be418c89 | 94 | /** Gets the updated length |
JRM1986 | 23:3081be418c89 | 95 | * @return returns the length of the tail |
JRM1986 | 23:3081be418c89 | 96 | */ |
JRM1986 | 23:3081be418c89 | 97 | |
JRM1986 | 16:85ca9feccf3f | 98 | int get_tail_length(); |
JRM1986 | 23:3081be418c89 | 99 | |
JRM1986 | 23:3081be418c89 | 100 | /** Creates an updated array of tail segments |
JRM1986 | 23:3081be418c89 | 101 | * @details When the engine_frame counter is odd it sets the odd_array, |
JRM1986 | 23:3081be418c89 | 102 | when the engine counter is even it sets the even_array. |
JRM1986 | 23:3081be418c89 | 103 | */ |
JRM1986 | 16:85ca9feccf3f | 104 | |
JRM1986 | 23:3081be418c89 | 105 | void set_tail_array(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 106 | |
JRM1986 | 23:3081be418c89 | 107 | /** Updates the odd array with new positions |
JRM1986 | 23:3081be418c89 | 108 | * @details For the odd array it initially sets index 0 of even array to the |
JRM1986 | 23:3081be418c89 | 109 | new position, then makes each odd index equal to the even |
JRM1986 | 23:3081be418c89 | 110 | index -1 starting from the the bottom of the array finally adding |
JRM1986 | 23:3081be418c89 | 111 | the new position to index 0 |
JRM1986 | 23:3081be418c89 | 112 | */ |
JRM1986 | 23:3081be418c89 | 113 | |
JRM1986 | 17:94dd8a691d4a | 114 | void set_odd_array(Gamepad &pad); |
JRM1986 | 23:3081be418c89 | 115 | |
JRM1986 | 23:3081be418c89 | 116 | /** Updates the even array with new positions |
JRM1986 | 23:3081be418c89 | 117 | * @details For the even array it initially sets index 0 of odd array to the |
JRM1986 | 23:3081be418c89 | 118 | new position, then makes each even index equal to the odd |
JRM1986 | 23:3081be418c89 | 119 | index -1 starting from the the bottom of the array finally adding |
JRM1986 | 23:3081be418c89 | 120 | the new position to index 0 |
JRM1986 | 23:3081be418c89 | 121 | */ |
JRM1986 | 23:3081be418c89 | 122 | |
JRM1986 | 17:94dd8a691d4a | 123 | void set_even_array(Gamepad &pad); |
JRM1986 | 25:f03439ee32c6 | 124 | |
JRM1986 | 25:f03439ee32c6 | 125 | /** Decreses the amount of frames between spawning of food |
JRM1986 | 25:f03439ee32c6 | 126 | * @param determines the interval and amount of tail segments are reached |
JRM1986 | 25:f03439ee32c6 | 127 | before the time between spawning reduces |
JRM1986 | 25:f03439ee32c6 | 128 | */ |
JRM1986 | 25:f03439ee32c6 | 129 | |
JRM1986 | 25:f03439ee32c6 | 130 | int food_spawning_time(int frame_decrementer); |
JRM1986 | 25:f03439ee32c6 | 131 | |
JRM1986 | 25:f03439ee32c6 | 132 | /** Changes the frame rate increasing speed |
JRM1986 | 25:f03439ee32c6 | 133 | * @param determines much food is eaten before increasing the speed |
JRM1986 | 25:f03439ee32c6 | 134 | * @param defines the increase in speed |
JRM1986 | 25:f03439ee32c6 | 135 | */ |
JRM1986 | 25:f03439ee32c6 | 136 | |
JRM1986 | 25:f03439ee32c6 | 137 | void increase_speed(int resetter, float speed_decrementer); |
JRM1986 | 25:f03439ee32c6 | 138 | |
JRM1986 | 16:85ca9feccf3f | 139 | |
JRM1986 | 2:ea90cec2489a | 140 | private: |
JRM1986 | 2:ea90cec2489a | 141 | |
JRM1986 | 6:f3f508cea1c4 | 142 | |
JRM1986 | 6:f3f508cea1c4 | 143 | Food _food; |
JRM1986 | 6:f3f508cea1c4 | 144 | |
JRM1986 | 6:f3f508cea1c4 | 145 | Snake _snake; |
JRM1986 | 10:62d8cb7742c3 | 146 | |
JRM1986 | 17:94dd8a691d4a | 147 | //Direction _snake_current_direction; |
JRM1986 | 20:277e88a8185f | 148 | int _snake_pos_x; |
JRM1986 | 20:277e88a8185f | 149 | int _snake_pos_y; |
JRM1986 | 13:72bc2579e85e | 150 | |
JRM1986 | 13:72bc2579e85e | 151 | Direction _in; |
JRM1986 | 10:62d8cb7742c3 | 152 | Direction _cur; |
JRM1986 | 10:62d8cb7742c3 | 153 | |
JRM1986 | 18:406fc298a7c4 | 154 | bool _collision; |
JRM1986 | 25:f03439ee32c6 | 155 | int _n_frames; |
JRM1986 | 17:94dd8a691d4a | 156 | |
JRM1986 | 17:94dd8a691d4a | 157 | int _tail_x; |
JRM1986 | 17:94dd8a691d4a | 158 | int _tail_y; |
JRM1986 | 17:94dd8a691d4a | 159 | |
JRM1986 | 26:23301f48c1ed | 160 | int _number_food_frames; |
JRM1986 | 25:f03439ee32c6 | 161 | void _wall_col_isr(); |
JRM1986 | 25:f03439ee32c6 | 162 | |
JRM1986 | 2:ea90cec2489a | 163 | |
JRM1986 | 2:ea90cec2489a | 164 | }; |
JRM1986 | 2:ea90cec2489a | 165 | #endif |