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 ll16j23s_test_docs
SnakeEngine/SnakeEngine.h@10:a2d643b3c782, 2020-05-26 (annotated)
- Committer:
- JoeShotton
- Date:
- Tue May 26 23:25:09 2020 +0000
- Revision:
- 10:a2d643b3c782
- Parent:
- 9:0571880085cc
Removed several bugs, started on thorough documentation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JoeShotton | 4:ea3fa51c4386 | 1 | #ifndef SNAKEENGINE_H |
| JoeShotton | 4:ea3fa51c4386 | 2 | #define SNAKEENGINE_H |
| JoeShotton | 3:fcd6d70e9694 | 3 | |
| JoeShotton | 3:fcd6d70e9694 | 4 | #include "mbed.h" |
| JoeShotton | 3:fcd6d70e9694 | 5 | #include "N5110.h" |
| JoeShotton | 3:fcd6d70e9694 | 6 | #include "Gamepad.h" |
| JoeShotton | 3:fcd6d70e9694 | 7 | #include "FXOS8700CQ.h" |
| JoeShotton | 3:fcd6d70e9694 | 8 | #include "SnakeBody.h" |
| JoeShotton | 3:fcd6d70e9694 | 9 | #include "Food.h" |
| JoeShotton | 3:fcd6d70e9694 | 10 | #include <vector> |
| JoeShotton | 3:fcd6d70e9694 | 11 | |
| JoeShotton | 10:a2d643b3c782 | 12 | /** SnakeEngine Class |
| JoeShotton | 10:a2d643b3c782 | 13 | @author Joseph Shotton |
| JoeShotton | 10:a2d643b3c782 | 14 | @brief Engine that controls the mechanics of the snake game |
| JoeShotton | 10:a2d643b3c782 | 15 | @date May 2020 |
| JoeShotton | 10:a2d643b3c782 | 16 | @version V1.0 |
| JoeShotton | 10:a2d643b3c782 | 17 | */ |
| JoeShotton | 10:a2d643b3c782 | 18 | |
| JoeShotton | 3:fcd6d70e9694 | 19 | class SnakeEngine |
| JoeShotton | 3:fcd6d70e9694 | 20 | { |
| JoeShotton | 3:fcd6d70e9694 | 21 | |
| JoeShotton | 3:fcd6d70e9694 | 22 | public: |
| JoeShotton | 3:fcd6d70e9694 | 23 | SnakeEngine(); |
| JoeShotton | 3:fcd6d70e9694 | 24 | ~SnakeEngine(); |
| JoeShotton | 4:ea3fa51c4386 | 25 | |
| JoeShotton | 7:dd84e0fab346 | 26 | |
| JoeShotton | 10:a2d643b3c782 | 27 | /** Initialises menu 1 |
| JoeShotton | 10:a2d643b3c782 | 28 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 29 | *@paran LCD |
| JoeShotton | 10:a2d643b3c782 | 30 | */ |
| JoeShotton | 9:0571880085cc | 31 | void menu1_init(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 32 | |
| JoeShotton | 7:dd84e0fab346 | 33 | |
| JoeShotton | 10:a2d643b3c782 | 34 | /** Controls selection of options in menu 1 |
| JoeShotton | 10:a2d643b3c782 | 35 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 36 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 37 | *@param Magnometer |
| JoeShotton | 10:a2d643b3c782 | 38 | */ |
| JoeShotton | 10:a2d643b3c782 | 39 | void menu1_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag); |
| JoeShotton | 10:a2d643b3c782 | 40 | |
| JoeShotton | 10:a2d643b3c782 | 41 | |
| JoeShotton | 10:a2d643b3c782 | 42 | /** Controls selection of options in menu 2 |
| JoeShotton | 10:a2d643b3c782 | 43 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 44 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 45 | *@param Magnometer |
| JoeShotton | 10:a2d643b3c782 | 46 | */ |
| JoeShotton | 10:a2d643b3c782 | 47 | void menu2_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag); |
| JoeShotton | 10:a2d643b3c782 | 48 | |
| JoeShotton | 10:a2d643b3c782 | 49 | |
| JoeShotton | 10:a2d643b3c782 | 50 | /** Runs game |
| JoeShotton | 10:a2d643b3c782 | 51 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 52 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 53 | */ |
| JoeShotton | 7:dd84e0fab346 | 54 | void game_run(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 7:dd84e0fab346 | 55 | |
| JoeShotton | 7:dd84e0fab346 | 56 | |
| JoeShotton | 10:a2d643b3c782 | 57 | /** Controls selection of options in death menu |
| JoeShotton | 10:a2d643b3c782 | 58 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 59 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 60 | *@param Magnometer |
| JoeShotton | 10:a2d643b3c782 | 61 | */ |
| JoeShotton | 10:a2d643b3c782 | 62 | void death_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag); |
| JoeShotton | 8:bcc3403d7e79 | 63 | |
| JoeShotton | 10:a2d643b3c782 | 64 | |
| JoeShotton | 10:a2d643b3c782 | 65 | /** Reads pot2 and controls contrast accordingly |
| JoeShotton | 10:a2d643b3c782 | 66 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 67 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 68 | */ |
| JoeShotton | 8:bcc3403d7e79 | 69 | void contrast(Gamepad &pad, N5110 &lcd_); |
| JoeShotton | 7:dd84e0fab346 | 70 | |
| JoeShotton | 7:dd84e0fab346 | 71 | int score; |
| JoeShotton | 10:a2d643b3c782 | 72 | int game_state; |
| JoeShotton | 3:fcd6d70e9694 | 73 | |
| JoeShotton | 10:a2d643b3c782 | 74 | |
| JoeShotton | 3:fcd6d70e9694 | 75 | private: |
| JoeShotton | 3:fcd6d70e9694 | 76 | |
| JoeShotton | 4:ea3fa51c4386 | 77 | SnakeBody _body; |
| JoeShotton | 4:ea3fa51c4386 | 78 | Food _food; |
| JoeShotton | 4:ea3fa51c4386 | 79 | |
| JoeShotton | 10:a2d643b3c782 | 80 | // game functions and variables |
| JoeShotton | 10:a2d643b3c782 | 81 | |
| JoeShotton | 10:a2d643b3c782 | 82 | |
| JoeShotton | 10:a2d643b3c782 | 83 | /** Initialises the game |
| JoeShotton | 10:a2d643b3c782 | 84 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 85 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 86 | *@param Magnometer |
| JoeShotton | 10:a2d643b3c782 | 87 | */ |
| JoeShotton | 10:a2d643b3c782 | 88 | void game_init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag); |
| JoeShotton | 10:a2d643b3c782 | 89 | |
| JoeShotton | 10:a2d643b3c782 | 90 | |
| JoeShotton | 10:a2d643b3c782 | 91 | /** Detects snake-food collisions and controls respawning of food/length increase etc |
| JoeShotton | 10:a2d643b3c782 | 92 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 93 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 94 | */ |
| JoeShotton | 10:a2d643b3c782 | 95 | void snake_food_collision(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 96 | |
| JoeShotton | 10:a2d643b3c782 | 97 | |
| JoeShotton | 10:a2d643b3c782 | 98 | /** Controls correct map to be displayed and correct snake-map collision functions |
| JoeShotton | 10:a2d643b3c782 | 99 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 100 | */ |
| JoeShotton | 8:bcc3403d7e79 | 101 | void map_run(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 102 | |
| JoeShotton | 10:a2d643b3c782 | 103 | |
| JoeShotton | 10:a2d643b3c782 | 104 | /** Draws map 2 (Ring) |
| JoeShotton | 10:a2d643b3c782 | 105 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 106 | */ |
| JoeShotton | 10:a2d643b3c782 | 107 | void map2_draw(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 108 | |
| JoeShotton | 10:a2d643b3c782 | 109 | |
| JoeShotton | 10:a2d643b3c782 | 110 | /** Draws map 3 (Cross) |
| JoeShotton | 10:a2d643b3c782 | 111 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 112 | */ |
| JoeShotton | 10:a2d643b3c782 | 113 | void map3_draw(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 114 | |
| JoeShotton | 10:a2d643b3c782 | 115 | |
| JoeShotton | 10:a2d643b3c782 | 116 | /** Draws map 4 (Lanes) |
| JoeShotton | 10:a2d643b3c782 | 117 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 118 | */ |
| JoeShotton | 10:a2d643b3c782 | 119 | void map4_draw(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 120 | |
| JoeShotton | 10:a2d643b3c782 | 121 | |
| JoeShotton | 10:a2d643b3c782 | 122 | /** Detects snake-map collision on map 2 |
| JoeShotton | 10:a2d643b3c782 | 123 | */ |
| JoeShotton | 9:0571880085cc | 124 | void snake_map2_collision(); |
| JoeShotton | 10:a2d643b3c782 | 125 | |
| JoeShotton | 10:a2d643b3c782 | 126 | |
| JoeShotton | 10:a2d643b3c782 | 127 | /** Detects snake-map collision on map 3 |
| JoeShotton | 10:a2d643b3c782 | 128 | */ |
| JoeShotton | 9:0571880085cc | 129 | void snake_map3_collision(); |
| JoeShotton | 10:a2d643b3c782 | 130 | |
| JoeShotton | 10:a2d643b3c782 | 131 | |
| JoeShotton | 10:a2d643b3c782 | 132 | /** Detects snake-map collision on map 4 |
| JoeShotton | 10:a2d643b3c782 | 133 | */ |
| JoeShotton | 9:0571880085cc | 134 | void snake_map4_collision(); |
| JoeShotton | 10:a2d643b3c782 | 135 | |
| JoeShotton | 10:a2d643b3c782 | 136 | bool _death; |
| JoeShotton | 10:a2d643b3c782 | 137 | int _map4_location; |
| JoeShotton | 10:a2d643b3c782 | 138 | float _angle; |
| JoeShotton | 10:a2d643b3c782 | 139 | |
| JoeShotton | 10:a2d643b3c782 | 140 | // menu/transition functions and variables |
| JoeShotton | 10:a2d643b3c782 | 141 | |
| JoeShotton | 10:a2d643b3c782 | 142 | /** Initialises menu 2 |
| JoeShotton | 10:a2d643b3c782 | 143 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 144 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 145 | */ |
| JoeShotton | 10:a2d643b3c782 | 146 | void menu2_init(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 147 | |
| JoeShotton | 10:a2d643b3c782 | 148 | |
| JoeShotton | 10:a2d643b3c782 | 149 | /** Initialises death menu |
| JoeShotton | 10:a2d643b3c782 | 150 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 151 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 152 | */ |
| JoeShotton | 10:a2d643b3c782 | 153 | void death_init(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 154 | |
| JoeShotton | 10:a2d643b3c782 | 155 | |
| JoeShotton | 10:a2d643b3c782 | 156 | /** Creates black transition animation |
| JoeShotton | 10:a2d643b3c782 | 157 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 158 | */ |
| JoeShotton | 10:a2d643b3c782 | 159 | void transition_black(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 160 | |
| JoeShotton | 10:a2d643b3c782 | 161 | |
| JoeShotton | 10:a2d643b3c782 | 162 | /** Creates white transition animation |
| JoeShotton | 10:a2d643b3c782 | 163 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 164 | */ |
| JoeShotton | 10:a2d643b3c782 | 165 | void transition_white(N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 166 | |
| JoeShotton | 10:a2d643b3c782 | 167 | |
| JoeShotton | 10:a2d643b3c782 | 168 | /** Draws black circle on selected line and clears other lines' circles |
| JoeShotton | 10:a2d643b3c782 | 169 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 170 | *@param Line that current option will be selected on. Range 1 - 5 |
| JoeShotton | 10:a2d643b3c782 | 171 | */ |
| JoeShotton | 10:a2d643b3c782 | 172 | void select_circles(N5110 &lcd, int line); |
| JoeShotton | 10:a2d643b3c782 | 173 | |
| JoeShotton | 10:a2d643b3c782 | 174 | |
| JoeShotton | 10:a2d643b3c782 | 175 | /** Generates short preview of selected map, then reinitialises menu 2 |
| JoeShotton | 10:a2d643b3c782 | 176 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 177 | *@param LCD |
| JoeShotton | 10:a2d643b3c782 | 178 | */ |
| JoeShotton | 10:a2d643b3c782 | 179 | void preview(Gamepad &pad, N5110 &lcd); |
| JoeShotton | 10:a2d643b3c782 | 180 | |
| JoeShotton | 10:a2d643b3c782 | 181 | |
| JoeShotton | 10:a2d643b3c782 | 182 | /** Resets relevant variables so game can be replayed |
| JoeShotton | 10:a2d643b3c782 | 183 | */ |
| JoeShotton | 10:a2d643b3c782 | 184 | void game_reset(); |
| JoeShotton | 10:a2d643b3c782 | 185 | |
| JoeShotton | 10:a2d643b3c782 | 186 | /** Triple flashes LEDs |
| JoeShotton | 10:a2d643b3c782 | 187 | *@param Gamepad |
| JoeShotton | 10:a2d643b3c782 | 188 | *@param Led numbers - will trigger both sides (ie both red LEDs). Range 1 - 3 |
| JoeShotton | 10:a2d643b3c782 | 189 | */ |
| JoeShotton | 9:0571880085cc | 190 | void menu_flash(Gamepad &pad, int led); |
| JoeShotton | 10:a2d643b3c782 | 191 | |
| JoeShotton | 7:dd84e0fab346 | 192 | int _menu_select; |
| JoeShotton | 9:0571880085cc | 193 | int _map_select; |
| JoeShotton | 10:a2d643b3c782 | 194 | double _pot2; |
| JoeShotton | 10:a2d643b3c782 | 195 | |
| JoeShotton | 3:fcd6d70e9694 | 196 | }; |
| JoeShotton | 3:fcd6d70e9694 | 197 | |
| JoeShotton | 3:fcd6d70e9694 | 198 | #endif |