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.
Fork of fsmSnake by
fsmSnakemain.h
00001 /** 00002 @file fsmSnakemain.h 00003 @file Header file containing functions prototypes,defines and gloabal variables. 00004 @Author Dean Griffin 00005 @date April 2016 00006 */ 00007 00008 #ifndef FSMSNAKE_H 00009 #define FSMSNAKE_H 00010 00011 #include "mbed.h" 00012 #include "N5110.h" 00013 00014 // change this to alter tolerance of joystick direction 00015 #define DIRECTION_TOLERANCE 0.05f 00016 00017 /** 00018 @namespace lcd 00019 @brief lcd object 00020 */ 00021 // VCC, SCE, RST, D/C, MOSI, SCLK, LED 00022 N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); // connections for screen 00023 00024 /** 00025 @namespace pc (USBTX USBRX); 00026 @brief serial for devu 00027 */ 00028 00029 /** 00030 @namespace button 00031 @brief Digital in for joystick button 00032 */ 00033 DigitalIn button(PTB11); 00034 /** 00035 @namespace xpot 00036 @brief analog in for joystick x co-ordinate 00037 */ 00038 AnalogIn xPot(PTB2); 00039 /** 00040 @namespace ypot 00041 @brief analog in for joystick y co-ordinate 00042 */ 00043 AnalogIn yPot(PTB3); 00044 /** 00045 @namespace greenLED 00046 @brief digital out for green game playing LED 00047 */ 00048 DigitalOut greenLED (PTD1); 00049 /** 00050 @namespace redLED 00051 @brief digital out for red gameover LED 00052 */ 00053 DigitalOut redLED(PTD0); 00054 /** 00055 @namespace g_button_flag 00056 @brief set flag in ISR 00057 */ 00058 volatile int g_button_flag = 0; 00059 /** 00060 @namespace g_timer_flag 00061 @brief set flag in ISR 00062 */ 00063 volatile int g_timer_flag = 0; 00064 /** 00065 @namespace menu_isr 00066 @brief update menu flag when called 00067 */ 00068 void menu_isr(); 00069 /** 00070 @namespace button_isr 00071 @brief update button flag when called 00072 */ 00073 void button_isr(); 00074 00075 00076 // set-up the on-board LEDs and switches 00077 void init_K64F(); 00078 00079 // set-up the on-board LEDs and switches 00080 void init_K64F(); 00081 00082 00083 00084 Ticker pollJoystick, checkJoystick, menuTicker, moveSnake, gameTime; 00085 /** 00086 flag for joystick ticker 00087 */ 00088 volatile int g_joystickTimer_flag = 0; 00089 /** 00090 flag for gametime ticker 00091 */ 00092 volatile int g_gameTimer_flag = 0; 00093 00094 /**brief serial out for debugging 00095 */ 00096 00097 /** 00098 @namespace pc 00099 @brief serial connection for mbed to pc 00100 */ 00101 Serial pc(USBTX,USBRX); 00102 00103 /** 00104 @namespace Directionname 00105 */ 00106 enum DirectionName { 00107 UP, 00108 DOWN, 00109 LEFT, 00110 RIGHT, 00111 CENTRE, 00112 UNKNOWN 00113 }; 00114 00115 /** 00116 @namespace snakeDirection 00117 */ 00118 enum snakeDirection { 00119 up, 00120 down, 00121 left, 00122 right, 00123 }; 00124 00125 /** 00126 @namespace joystick 00127 @brief creates strcut for joystick 00128 */ 00129 typedef struct JoyStick Joystick; 00130 struct JoyStick { 00131 float x; // current x value 00132 float x0; // 'centred' x value 00133 float y; // current y value 00134 float y0; // 'centred' y value 00135 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) 00136 DirectionName direction; // current direction 00137 }; 00138 // create struct variable 00139 Joystick joystick; 00140 00141 int printFlag = 0; 00142 00143 /** game functions 00144 */ 00145 00146 00147 /** menu function contains all of the game logic to be called in the main 00148 */ 00149 void menu(); 00150 /** reads joysticks x and y pots 00151 */ 00152 void calibrateJoystick(); 00153 00154 /** lets the joystick know which way its being pressed 00155 */ 00156 00157 void updateJoystick(); 00158 00159 /** joystick lets snake know which way to go 00160 */ 00161 00162 void checkDirection(); 00163 00164 /** initialises game settings 00165 */ 00166 void gameSetup(); 00167 00168 00169 /** determines the snakes direction of travel 00170 */ 00171 void nextMoveSnake(); 00172 00173 /** displays snake in next position 00174 */ 00175 00176 void printFood(); 00177 00178 /** check if the snake has ate the food, then produced next snack 00179 */ 00180 void generateFood(); 00181 00182 /** initialises the rules for and logic for classic snake mode 00183 */ 00184 void classic (); 00185 00186 /** initialises the rules for and logic for classic snake two mode 00187 */ 00188 void snake2(); 00189 00190 /** sets snake on game area 00191 */ 00192 00193 void newSnake(); 00194 00195 /** called by ticker to set next screen 00196 */ 00197 00198 void timer_isr(); 00199 00200 /** called by ticker to set snake in new position 00201 */ 00202 00203 void update_snake_isr(); 00204 00205 /** set joystick direction to snake direction 00206 */ 00207 00208 void newDirection(); 00209 00210 /** set hardwall for classic snake mode 00211 */ 00212 00213 void hardWall(); 00214 00215 /** allows snake to go through to other other side in maze and snake 2 mode 00216 */ 00217 00218 void wrapAround(); 00219 00220 /** function to make snake tail follow its head 00221 */ 00222 void snakeTail(); 00223 00224 /** lets player know its game over if the snake has died 00225 */ 00226 00227 void game_over(); 00228 /** 00229 */ 00230 00231 void gameTimer_ISR(); 00232 00233 /** sets rules and logic for maze mode 00234 */ 00235 00236 void maze(); 00237 00238 /** variable 00239 */ 00240 00241 int i; 00242 00243 /** store direction of snake 00244 */ 00245 00246 int snakeDirection; 00247 00248 /** array determining game areas x axis 00249 */ 00250 int width [84]; 00251 00252 /** array determining game areas y axis 00253 */ 00254 int height [48]; 00255 00256 /** snakes head on the x -axis 00257 */ 00258 00259 int sx = 48; 00260 00261 /** snakes head on the y-axis 00262 */ 00263 00264 int sy = 24; //set snake at centre 00265 00266 /** food on the x-axis 00267 */ 00268 00269 int fx; 00270 00271 /** food on the y-axis 00272 */ 00273 00274 int fy; 00275 00276 /** stores game score 00277 */ 00278 00279 int score = 0; 00280 00281 /** stores snake length 00282 */ 00283 00284 int tailL = 0; 00285 00286 /** array to store snake tail on x axis 00287 */ 00288 00289 int tailX [100]; 00290 00291 /** array to store snake tail on the y axis 00292 */ 00293 00294 int tailY [100]; 00295 00296 /** true when the game is running 00297 */ 00298 00299 bool gameOver = false; 00300 00301 #endif
Generated on Fri Aug 5 2022 08:44:17 by
1.7.2
