Final Commit

Dependencies:   mbed

Committer:
JRM1986
Date:
Tue May 08 10:52:12 2018 +0000
Revision:
25:f03439ee32c6
Parent:
24:4b180348826e
Child:
26:23301f48c1ed
Tidying up 1;

Who changed what in which revision?

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