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
SnakeEngine.h
00001 #ifndef SNAKEENGINE_H 00002 #define SNAKEENGINE_H 00003 00004 #include "mbed.h" 00005 #include "Gamepad.h" 00006 #include "N5110.h" 00007 #include "Food.h" 00008 #include "Snake.h" 00009 00010 00011 /** SnakeEngine Class 00012 * @brief Class that initialises and defines the game charectoristics ready for the main file 00013 * @author Joshua R. Marshall 00014 * @date Feb, 2018 00015 */ 00016 00017 static Vector2D g_tp; 00018 void g_frame_time(int frame_time); 00019 00020 00021 static int g_tl; // tail length 00022 static int g_engine_fc; // frame counter for draw tail functions 00023 static int g_ft; // frame counter for decreasing time between food spawning 00024 static int g_n; // ensures spawning of food occurs periodically 00025 static bool g_wall; // wall collision detected 00026 00027 /** Returns the speed 00028 */ 00029 static double g_speed; 00030 00031 static int g_odd_array_x[100]; // array to store odd values for x component of tail segments 00032 static int g_odd_array_y[100]; // array to store odd values for y component of tail segments 00033 static int g_even_array_x[100]; // array to store even values for x component of tail segments 00034 static int g_even_array_y[100]; // array to store even values for y component of tail segments 00035 00036 00037 class SnakeEngine 00038 { 00039 00040 public: 00041 00042 SnakeEngine(); // constructor 00043 ~SnakeEngine(); // destructor 00044 00045 00046 /** Initialises the directions for the snake and the position for the food 00047 * @param Input direction from pad 00048 * @param Current direction of snake 00049 * @param pos_x x position 00050 * @param pos_y y position 00051 * @param sets initial number of frames between food spawning 00052 */ 00053 00054 void init(Direction in, Direction cur, int pos_x, int pos_y, int n_frames); 00055 00056 /** Updates the Food and Snake classes from the Gamepad 00057 */ 00058 00059 void update(Gamepad &pad); 00060 00061 /** Draws the game to the lcd */ 00062 00063 void draw(N5110 &lcd); 00064 00065 /** Draws the tail segments on the lcd */ 00066 00067 void draw_tail(N5110 &lcd); 00068 00069 /** Gets the updated direction from the Gamepad and stores it in a struct 00070 */ 00071 00072 void get_input(Gamepad &pad); 00073 00074 /** Check if snake has eaten food 00075 */ 00076 00077 bool detect_food_collision(Gamepad &pad); 00078 00079 /** Checks if snake has hit wall 00080 */ 00081 bool detect_wall_collision(Gamepad &pad); 00082 00083 /** Checks if snake has hit it's own tail 00084 */ 00085 00086 bool detect_tail_collision(); 00087 00088 /** Increments the length variable for tail when food is eaten 00089 * @param Checks if food and head have collided (been eaten) 00090 */ 00091 00092 void set_tail_length(bool collision_detected); 00093 00094 /** Gets the updated length 00095 * @return returns the length of the tail 00096 */ 00097 00098 int get_tail_length(); 00099 00100 /** Creates an updated array of tail segments 00101 * @details When the engine_frame counter is odd it sets the odd_array, 00102 when the engine counter is even it sets the even_array. 00103 */ 00104 00105 void set_tail_array(Gamepad &pad); 00106 00107 /** Updates the odd array with new positions 00108 * @details For the odd array it initially sets index 0 of even array to the 00109 new position, then makes each odd index equal to the even 00110 index -1 starting from the the bottom of the array finally adding 00111 the new position to index 0 00112 */ 00113 00114 void set_odd_array(Gamepad &pad); 00115 00116 /** Updates the even array with new positions 00117 * @details For the even array it initially sets index 0 of odd array to the 00118 new position, then makes each even index equal to the odd 00119 index -1 starting from the the bottom of the array finally adding 00120 the new position to index 0 00121 */ 00122 00123 void set_even_array(Gamepad &pad); 00124 00125 /** Decreses the amount of frames between spawning of food 00126 * @param determines the interval and amount of tail segments are reached 00127 before the time between spawning reduces 00128 */ 00129 00130 int food_spawning_time(int frame_decrementer); 00131 00132 /** Changes the frame rate increasing speed 00133 * @param determines much food is eaten before increasing the speed 00134 * @param defines the increase in speed 00135 */ 00136 00137 void increase_speed(int resetter, float speed_decrementer); 00138 00139 00140 private: 00141 00142 00143 Food _food; 00144 00145 Snake _snake; 00146 00147 //Direction _snake_current_direction; 00148 int _snake_pos_x; 00149 int _snake_pos_y; 00150 00151 Direction _in; 00152 Direction _cur; 00153 00154 bool _collision; 00155 int _n_frames; 00156 00157 int _tail_x; 00158 int _tail_y; 00159 00160 int _number_food_frames; 00161 void _wall_col_isr(); 00162 00163 00164 }; 00165 #endif
Generated on Wed Aug 3 2022 05:55:53 by
1.7.2