![](/media/cache/group/freescale_logo.jpg.50x50_q85.jpg)
Snake game on nokia N5110 LCD
Dependencies: mbed
Snake game on nokia N5110 LCD and Keyes Syos Joystick. You control snake using joystick. Start/pause game ba using button on joystick or by pressing p on your keyboard (serial communication). More info on my blog: http://sdizdarevic.com/post/94147065625/frdm-k64f-project
Snakelib.h@0:5bdb67970267, 2014-08-08 (annotated)
- Committer:
- sdizdarevic
- Date:
- Fri Aug 08 06:44:50 2014 +0000
- Revision:
- 0:5bdb67970267
Check connections
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sdizdarevic | 0:5bdb67970267 | 1 | #ifndef E_SNAKE_H |
sdizdarevic | 0:5bdb67970267 | 2 | #define E_SNAKE_H |
sdizdarevic | 0:5bdb67970267 | 3 | #include <cstdlib> |
sdizdarevic | 0:5bdb67970267 | 4 | #include "mbed.h" |
sdizdarevic | 0:5bdb67970267 | 5 | #include "N5110.h" |
sdizdarevic | 0:5bdb67970267 | 6 | #include "Helper.h" |
sdizdarevic | 0:5bdb67970267 | 7 | class Snakelib |
sdizdarevic | 0:5bdb67970267 | 8 | { |
sdizdarevic | 0:5bdb67970267 | 9 | static const bool SnakeTypes [2][6][3][3]; |
sdizdarevic | 0:5bdb67970267 | 10 | static const bool SnakeFood [5][3][3]; |
sdizdarevic | 0:5bdb67970267 | 11 | static const int Pos [2][3][3]; |
sdizdarevic | 0:5bdb67970267 | 12 | short Snake [438][2]; |
sdizdarevic | 0:5bdb67970267 | 13 | static const double Speeds [10]; |
sdizdarevic | 0:5bdb67970267 | 14 | static const unsigned char Points[10]; |
sdizdarevic | 0:5bdb67970267 | 15 | AnalogIn VX, VY; |
sdizdarevic | 0:5bdb67970267 | 16 | InterruptIn SW; |
sdizdarevic | 0:5bdb67970267 | 17 | int HScore, Score; |
sdizdarevic | 0:5bdb67970267 | 18 | int SnakeLen, Speed; |
sdizdarevic | 0:5bdb67970267 | 19 | int SnakeType, FoodType; |
sdizdarevic | 0:5bdb67970267 | 20 | int eX, eY, pX, pY, Dir; |
sdizdarevic | 0:5bdb67970267 | 21 | bool Moved, Pause, Draw, Started; |
sdizdarevic | 0:5bdb67970267 | 22 | N5110 DISP; |
sdizdarevic | 0:5bdb67970267 | 23 | DigitalOut ENBL; |
sdizdarevic | 0:5bdb67970267 | 24 | Ticker TJoy, TMove; |
sdizdarevic | 0:5bdb67970267 | 25 | Timer Dbnc; |
sdizdarevic | 0:5bdb67970267 | 26 | Ticker T; |
sdizdarevic | 0:5bdb67970267 | 27 | void SetBlock (int Block = 0, int X = 1, int Y = 1) |
sdizdarevic | 0:5bdb67970267 | 28 | { |
sdizdarevic | 0:5bdb67970267 | 29 | for (int a = 0; a < 3; ++a) |
sdizdarevic | 0:5bdb67970267 | 30 | for (int b = 0; b < 3; ++b) |
sdizdarevic | 0:5bdb67970267 | 31 | if (Block == 2 && SnakeFood [FoodType][a][b]) |
sdizdarevic | 0:5bdb67970267 | 32 | DISP.setPixel (X + Pos[0][a][b], Y + Pos[1][a][b]); |
sdizdarevic | 0:5bdb67970267 | 33 | else if (Block < 2 && SnakeTypes [Block][SnakeType][a][b]) |
sdizdarevic | 0:5bdb67970267 | 34 | DISP.setPixel (X + Pos[0][a][b], Y + Pos[1][a][b]); |
sdizdarevic | 0:5bdb67970267 | 35 | } |
sdizdarevic | 0:5bdb67970267 | 36 | int Rand (int Min = 1, int Max = 83) |
sdizdarevic | 0:5bdb67970267 | 37 | { |
sdizdarevic | 0:5bdb67970267 | 38 | static bool First = true; |
sdizdarevic | 0:5bdb67970267 | 39 | if (First) srand (time (NULL)), First = false; |
sdizdarevic | 0:5bdb67970267 | 40 | while (7) |
sdizdarevic | 0:5bdb67970267 | 41 | { |
sdizdarevic | 0:5bdb67970267 | 42 | int S = rand () % (Max - Min + 1) + Min; |
sdizdarevic | 0:5bdb67970267 | 43 | if (!((S - 1) % 3)) return S; |
sdizdarevic | 0:5bdb67970267 | 44 | } |
sdizdarevic | 0:5bdb67970267 | 45 | } |
sdizdarevic | 0:5bdb67970267 | 46 | void GameOver () |
sdizdarevic | 0:5bdb67970267 | 47 | { |
sdizdarevic | 0:5bdb67970267 | 48 | |
sdizdarevic | 0:5bdb67970267 | 49 | //FlashB(); |
sdizdarevic | 0:5bdb67970267 | 50 | wait (2); |
sdizdarevic | 0:5bdb67970267 | 51 | DISP.clear(); |
sdizdarevic | 0:5bdb67970267 | 52 | DISP.setXYAddress (0, 0); |
sdizdarevic | 0:5bdb67970267 | 53 | DISP.printString ("END!", 29, 0); |
sdizdarevic | 0:5bdb67970267 | 54 | DISP.printString ("Score: ", 10, 2); |
sdizdarevic | 0:5bdb67970267 | 55 | char Buff[20]; |
sdizdarevic | 0:5bdb67970267 | 56 | ToString (Buff, Score); |
sdizdarevic | 0:5bdb67970267 | 57 | DISP.printString (Buff, 50, 2); |
sdizdarevic | 0:5bdb67970267 | 58 | DISP.printString ("Best: ", 10, 4); |
sdizdarevic | 0:5bdb67970267 | 59 | ToString (Buff, HScore); |
sdizdarevic | 0:5bdb67970267 | 60 | DISP.printString (Buff, 50, 4); |
sdizdarevic | 0:5bdb67970267 | 61 | eX = eY = 0; // |
sdizdarevic | 0:5bdb67970267 | 62 | Started = false; |
sdizdarevic | 0:5bdb67970267 | 63 | wait (3); |
sdizdarevic | 0:5bdb67970267 | 64 | Restart(); |
sdizdarevic | 0:5bdb67970267 | 65 | } |
sdizdarevic | 0:5bdb67970267 | 66 | void Prekuco() |
sdizdarevic | 0:5bdb67970267 | 67 | { |
sdizdarevic | 0:5bdb67970267 | 68 | DISP.clear(); |
sdizdarevic | 0:5bdb67970267 | 69 | DISP.setXYAddress (0, 0); |
sdizdarevic | 0:5bdb67970267 | 70 | DISP.printString ("Well done!!!!!", 0, 0); |
sdizdarevic | 0:5bdb67970267 | 71 | DISP.printString ("Congrats", 0, 2); |
sdizdarevic | 0:5bdb67970267 | 72 | eX = eY = 0; |
sdizdarevic | 0:5bdb67970267 | 73 | Started = false; |
sdizdarevic | 0:5bdb67970267 | 74 | wait (3); |
sdizdarevic | 0:5bdb67970267 | 75 | Restart(); |
sdizdarevic | 0:5bdb67970267 | 76 | } |
sdizdarevic | 0:5bdb67970267 | 77 | void HitFood () |
sdizdarevic | 0:5bdb67970267 | 78 | { |
sdizdarevic | 0:5bdb67970267 | 79 | if (Snake [0][0] == pX && Snake [0][1] == pY) |
sdizdarevic | 0:5bdb67970267 | 80 | { |
sdizdarevic | 0:5bdb67970267 | 81 | Score += Points[Speed - 1]; |
sdizdarevic | 0:5bdb67970267 | 82 | AddSnakeBody(); |
sdizdarevic | 0:5bdb67970267 | 83 | RandFood(pX, pY); |
sdizdarevic | 0:5bdb67970267 | 84 | } |
sdizdarevic | 0:5bdb67970267 | 85 | } |
sdizdarevic | 0:5bdb67970267 | 86 | void HitWall() |
sdizdarevic | 0:5bdb67970267 | 87 | { |
sdizdarevic | 0:5bdb67970267 | 88 | if (Snake[0][0] + 1 > 83 || Snake[0][0] - 1 < 0 || Snake[0][1] + 1 > 47 || Snake[0][1] - 1 < 0) |
sdizdarevic | 0:5bdb67970267 | 89 | { |
sdizdarevic | 0:5bdb67970267 | 90 | if (Score > HScore) |
sdizdarevic | 0:5bdb67970267 | 91 | HScore = Score; |
sdizdarevic | 0:5bdb67970267 | 92 | Started = false;; |
sdizdarevic | 0:5bdb67970267 | 93 | GameOver(); |
sdizdarevic | 0:5bdb67970267 | 94 | return; |
sdizdarevic | 0:5bdb67970267 | 95 | } |
sdizdarevic | 0:5bdb67970267 | 96 | } |
sdizdarevic | 0:5bdb67970267 | 97 | void HitSelf() |
sdizdarevic | 0:5bdb67970267 | 98 | { |
sdizdarevic | 0:5bdb67970267 | 99 | for (int a = 1; a <= SnakeLen; ++a) |
sdizdarevic | 0:5bdb67970267 | 100 | if (Snake[0][0] == Snake[a][0] && Snake[0][1] == Snake[a][1]) |
sdizdarevic | 0:5bdb67970267 | 101 | { |
sdizdarevic | 0:5bdb67970267 | 102 | if (Score > HScore) |
sdizdarevic | 0:5bdb67970267 | 103 | HScore = Score; |
sdizdarevic | 0:5bdb67970267 | 104 | GameOver(); |
sdizdarevic | 0:5bdb67970267 | 105 | return; |
sdizdarevic | 0:5bdb67970267 | 106 | } |
sdizdarevic | 0:5bdb67970267 | 107 | } |
sdizdarevic | 0:5bdb67970267 | 108 | void Start() |
sdizdarevic | 0:5bdb67970267 | 109 | { |
sdizdarevic | 0:5bdb67970267 | 110 | Snake[0][0] = Rand(20, 70); |
sdizdarevic | 0:5bdb67970267 | 111 | Snake[0][1] = Rand(10, 35); |
sdizdarevic | 0:5bdb67970267 | 112 | RandFood(pX, pY); |
sdizdarevic | 0:5bdb67970267 | 113 | AddSnakeBody(); |
sdizdarevic | 0:5bdb67970267 | 114 | AddSnakeBody(); |
sdizdarevic | 0:5bdb67970267 | 115 | Refresh(); |
sdizdarevic | 0:5bdb67970267 | 116 | } |
sdizdarevic | 0:5bdb67970267 | 117 | void FlashB () |
sdizdarevic | 0:5bdb67970267 | 118 | { |
sdizdarevic | 0:5bdb67970267 | 119 | for (int z = 0; z < 5; ++z) |
sdizdarevic | 0:5bdb67970267 | 120 | { |
sdizdarevic | 0:5bdb67970267 | 121 | DISP.clear(); |
sdizdarevic | 0:5bdb67970267 | 122 | DISP.setXYAddress (0, 0); |
sdizdarevic | 0:5bdb67970267 | 123 | DISP.refresh(); |
sdizdarevic | 0:5bdb67970267 | 124 | wait (0.5); |
sdizdarevic | 0:5bdb67970267 | 125 | DrawWall(); |
sdizdarevic | 0:5bdb67970267 | 126 | for (int a = 0; a <= SnakeLen; ++a) |
sdizdarevic | 0:5bdb67970267 | 127 | SetBlock(a == 0 ? 0 : 1, Snake[a][0], Snake[a][1]); |
sdizdarevic | 0:5bdb67970267 | 128 | SetBlock(2, pX, pY); |
sdizdarevic | 0:5bdb67970267 | 129 | DISP.refresh(); |
sdizdarevic | 0:5bdb67970267 | 130 | } |
sdizdarevic | 0:5bdb67970267 | 131 | } |
sdizdarevic | 0:5bdb67970267 | 132 | void MoveSnake () |
sdizdarevic | 0:5bdb67970267 | 133 | { |
sdizdarevic | 0:5bdb67970267 | 134 | if (Pause || !Started) return; |
sdizdarevic | 0:5bdb67970267 | 135 | for (int a = SnakeLen; a >= 1; --a) |
sdizdarevic | 0:5bdb67970267 | 136 | { |
sdizdarevic | 0:5bdb67970267 | 137 | Snake[a][0] = Snake[a - 1][0]; |
sdizdarevic | 0:5bdb67970267 | 138 | Snake[a][1] = Snake[a - 1][1]; |
sdizdarevic | 0:5bdb67970267 | 139 | } |
sdizdarevic | 0:5bdb67970267 | 140 | Snake[0][0] += eX; |
sdizdarevic | 0:5bdb67970267 | 141 | Snake[0][1] += eY; |
sdizdarevic | 0:5bdb67970267 | 142 | Moved = true; |
sdizdarevic | 0:5bdb67970267 | 143 | Refresh(); |
sdizdarevic | 0:5bdb67970267 | 144 | } |
sdizdarevic | 0:5bdb67970267 | 145 | void AddSnakeBody() |
sdizdarevic | 0:5bdb67970267 | 146 | { |
sdizdarevic | 0:5bdb67970267 | 147 | SnakeLen++; |
sdizdarevic | 0:5bdb67970267 | 148 | int X = 0, Y = 0; |
sdizdarevic | 0:5bdb67970267 | 149 | if (Dir == 0) X = 3, Y = 0; |
sdizdarevic | 0:5bdb67970267 | 150 | if (Dir == 1) X = 0, Y = 3; |
sdizdarevic | 0:5bdb67970267 | 151 | if (Dir == 2) X = -3, Y = 0; |
sdizdarevic | 0:5bdb67970267 | 152 | if (Dir == 3) X = 0, Y = -3; |
sdizdarevic | 0:5bdb67970267 | 153 | Snake [SnakeLen][0] = Snake[SnakeLen - 1][0] + X; |
sdizdarevic | 0:5bdb67970267 | 154 | Snake [SnakeLen][1] = Snake[SnakeLen - 1][1] + Y; |
sdizdarevic | 0:5bdb67970267 | 155 | } |
sdizdarevic | 0:5bdb67970267 | 156 | void RandFood(int &X, int &Y) |
sdizdarevic | 0:5bdb67970267 | 157 | { |
sdizdarevic | 0:5bdb67970267 | 158 | while (7) |
sdizdarevic | 0:5bdb67970267 | 159 | { |
sdizdarevic | 0:5bdb67970267 | 160 | int A = Rand(1, 83); |
sdizdarevic | 0:5bdb67970267 | 161 | int B = Rand(1, 47); |
sdizdarevic | 0:5bdb67970267 | 162 | bool S = false; |
sdizdarevic | 0:5bdb67970267 | 163 | for (int I = 0; I <= SnakeLen; ++I) |
sdizdarevic | 0:5bdb67970267 | 164 | if (Snake[I][0] == A && Snake[I][1] == B) |
sdizdarevic | 0:5bdb67970267 | 165 | S = true; |
sdizdarevic | 0:5bdb67970267 | 166 | if (!S) {X = A; Y = B; return;} |
sdizdarevic | 0:5bdb67970267 | 167 | } |
sdizdarevic | 0:5bdb67970267 | 168 | } |
sdizdarevic | 0:5bdb67970267 | 169 | void DrawWall () |
sdizdarevic | 0:5bdb67970267 | 170 | { |
sdizdarevic | 0:5bdb67970267 | 171 | for (int a = 0; a < 84; ++a) |
sdizdarevic | 0:5bdb67970267 | 172 | for (int b = 0; b < 48; ++b) |
sdizdarevic | 0:5bdb67970267 | 173 | if (!a || !b || a == 83 || b == 47) |
sdizdarevic | 0:5bdb67970267 | 174 | DISP.setPixel (a, b); |
sdizdarevic | 0:5bdb67970267 | 175 | } |
sdizdarevic | 0:5bdb67970267 | 176 | void Refresh () |
sdizdarevic | 0:5bdb67970267 | 177 | { |
sdizdarevic | 0:5bdb67970267 | 178 | if (!Draw) return; |
sdizdarevic | 0:5bdb67970267 | 179 | HitFood(); |
sdizdarevic | 0:5bdb67970267 | 180 | HitWall(); |
sdizdarevic | 0:5bdb67970267 | 181 | HitSelf(); |
sdizdarevic | 0:5bdb67970267 | 182 | DISP.clear(); |
sdizdarevic | 0:5bdb67970267 | 183 | DISP.setXYAddress (0, 0); |
sdizdarevic | 0:5bdb67970267 | 184 | DrawWall(); |
sdizdarevic | 0:5bdb67970267 | 185 | for (int a = 0; a <= SnakeLen; ++a) |
sdizdarevic | 0:5bdb67970267 | 186 | SetBlock(a == 0 ? 0 : 1, Snake[a][0], Snake[a][1]); |
sdizdarevic | 0:5bdb67970267 | 187 | SetBlock(2, pX, pY); |
sdizdarevic | 0:5bdb67970267 | 188 | DISP.refresh(); |
sdizdarevic | 0:5bdb67970267 | 189 | if (SnakeLen > 437) Prekuco(); |
sdizdarevic | 0:5bdb67970267 | 190 | } |
sdizdarevic | 0:5bdb67970267 | 191 | void Moving () |
sdizdarevic | 0:5bdb67970267 | 192 | { |
sdizdarevic | 0:5bdb67970267 | 193 | if (!Moved) return; |
sdizdarevic | 0:5bdb67970267 | 194 | double vx = VX, vy = VY; |
sdizdarevic | 0:5bdb67970267 | 195 | if (vx < 1./3.) |
sdizdarevic | 0:5bdb67970267 | 196 | { |
sdizdarevic | 0:5bdb67970267 | 197 | if (Dir == 2) return; |
sdizdarevic | 0:5bdb67970267 | 198 | eX = -3; eY = 0; |
sdizdarevic | 0:5bdb67970267 | 199 | Dir = 0; |
sdizdarevic | 0:5bdb67970267 | 200 | Moved = false; |
sdizdarevic | 0:5bdb67970267 | 201 | } |
sdizdarevic | 0:5bdb67970267 | 202 | else if (vx > 2./3.) |
sdizdarevic | 0:5bdb67970267 | 203 | { |
sdizdarevic | 0:5bdb67970267 | 204 | if (Dir == 0) return; |
sdizdarevic | 0:5bdb67970267 | 205 | eX = 3; eY = 0; |
sdizdarevic | 0:5bdb67970267 | 206 | Dir = 2; |
sdizdarevic | 0:5bdb67970267 | 207 | Moved = false; |
sdizdarevic | 0:5bdb67970267 | 208 | } |
sdizdarevic | 0:5bdb67970267 | 209 | if (vy < 1./3.) |
sdizdarevic | 0:5bdb67970267 | 210 | { |
sdizdarevic | 0:5bdb67970267 | 211 | if (Dir == 1) return; |
sdizdarevic | 0:5bdb67970267 | 212 | eX = 0; eY = -3; |
sdizdarevic | 0:5bdb67970267 | 213 | Dir = 3; |
sdizdarevic | 0:5bdb67970267 | 214 | Moved = false; |
sdizdarevic | 0:5bdb67970267 | 215 | } |
sdizdarevic | 0:5bdb67970267 | 216 | else if (vy > 2./3.) |
sdizdarevic | 0:5bdb67970267 | 217 | { |
sdizdarevic | 0:5bdb67970267 | 218 | if (Dir == 3) return; |
sdizdarevic | 0:5bdb67970267 | 219 | eX = 0; eY = 3; |
sdizdarevic | 0:5bdb67970267 | 220 | Dir = 1; |
sdizdarevic | 0:5bdb67970267 | 221 | Moved = false; |
sdizdarevic | 0:5bdb67970267 | 222 | } |
sdizdarevic | 0:5bdb67970267 | 223 | } |
sdizdarevic | 0:5bdb67970267 | 224 | void Pressed () |
sdizdarevic | 0:5bdb67970267 | 225 | { |
sdizdarevic | 0:5bdb67970267 | 226 | if (Dbnc.read() > .2) |
sdizdarevic | 0:5bdb67970267 | 227 | { |
sdizdarevic | 0:5bdb67970267 | 228 | if (!Started) |
sdizdarevic | 0:5bdb67970267 | 229 | { |
sdizdarevic | 0:5bdb67970267 | 230 | Started = true; |
sdizdarevic | 0:5bdb67970267 | 231 | TMove.attach (this, &Snakelib::MoveSnake, Speeds[Speed - 1]); |
sdizdarevic | 0:5bdb67970267 | 232 | } |
sdizdarevic | 0:5bdb67970267 | 233 | else |
sdizdarevic | 0:5bdb67970267 | 234 | { |
sdizdarevic | 0:5bdb67970267 | 235 | Pause = !Pause; |
sdizdarevic | 0:5bdb67970267 | 236 | Draw = !Pause; |
sdizdarevic | 0:5bdb67970267 | 237 | } |
sdizdarevic | 0:5bdb67970267 | 238 | } |
sdizdarevic | 0:5bdb67970267 | 239 | Dbnc.reset(); |
sdizdarevic | 0:5bdb67970267 | 240 | } |
sdizdarevic | 0:5bdb67970267 | 241 | public: |
sdizdarevic | 0:5bdb67970267 | 242 | Snakelib (PinName P1, PinName P2, PinName P3, PinName P4, PinName P5, |
sdizdarevic | 0:5bdb67970267 | 243 | PinName P6, PinName P7, PinName BX, PinName BY, PinName BS) |
sdizdarevic | 0:5bdb67970267 | 244 | : VX (BX), VY (BY), SW (BS), HScore (0), Score (0), SnakeLen (0), Speed (6), SnakeType (0), |
sdizdarevic | 0:5bdb67970267 | 245 | FoodType (0), eX (3), eY (0), pX(0), pY (0), Dir (2), Moved(false), Pause (false), Draw (true), Started (false), |
sdizdarevic | 0:5bdb67970267 | 246 | DISP (P1, P2, P3, P4, P5, P6, P7), ENBL (LED_RED) |
sdizdarevic | 0:5bdb67970267 | 247 | { |
sdizdarevic | 0:5bdb67970267 | 248 | |
sdizdarevic | 0:5bdb67970267 | 249 | } |
sdizdarevic | 0:5bdb67970267 | 250 | void START() |
sdizdarevic | 0:5bdb67970267 | 251 | { |
sdizdarevic | 0:5bdb67970267 | 252 | ENBL = 1; |
sdizdarevic | 0:5bdb67970267 | 253 | Dbnc.start(); |
sdizdarevic | 0:5bdb67970267 | 254 | SW.mode (PullUp); |
sdizdarevic | 0:5bdb67970267 | 255 | DISP.init(); |
sdizdarevic | 0:5bdb67970267 | 256 | DISP.setXYAddress (0, 0); |
sdizdarevic | 0:5bdb67970267 | 257 | DISP.clear(); |
sdizdarevic | 0:5bdb67970267 | 258 | TJoy.attach (this, &Snakelib::Moving, 0.01); |
sdizdarevic | 0:5bdb67970267 | 259 | DISP.printString ("- SNAKE -", 0, 0); |
sdizdarevic | 0:5bdb67970267 | 260 | DISP.printString ("K64F", 0, 3); |
sdizdarevic | 0:5bdb67970267 | 261 | DISP.printString ("Freescale", 0, 4); |
sdizdarevic | 0:5bdb67970267 | 262 | wait (2); |
sdizdarevic | 0:5bdb67970267 | 263 | SW.rise (this, &Snakelib::Pressed); |
sdizdarevic | 0:5bdb67970267 | 264 | Start(); |
sdizdarevic | 0:5bdb67970267 | 265 | |
sdizdarevic | 0:5bdb67970267 | 266 | |
sdizdarevic | 0:5bdb67970267 | 267 | } |
sdizdarevic | 0:5bdb67970267 | 268 | int SnakeSpeed () const {return Speed;} |
sdizdarevic | 0:5bdb67970267 | 269 | void SetSnakeSpeed (unsigned char S) |
sdizdarevic | 0:5bdb67970267 | 270 | { |
sdizdarevic | 0:5bdb67970267 | 271 | if (S == Speed) return; |
sdizdarevic | 0:5bdb67970267 | 272 | Speed = S; |
sdizdarevic | 0:5bdb67970267 | 273 | Speed = Speed < 1 ? 1 : Speed > 10 ? 10 : Speed; |
sdizdarevic | 0:5bdb67970267 | 274 | TMove.detach(); |
sdizdarevic | 0:5bdb67970267 | 275 | wait_ms (2); |
sdizdarevic | 0:5bdb67970267 | 276 | TMove.attach (this, &Snakelib::MoveSnake, Speeds[Speed - 1]); |
sdizdarevic | 0:5bdb67970267 | 277 | } |
sdizdarevic | 0:5bdb67970267 | 278 | int GetSnakeType () const {return SnakeType + 1;} |
sdizdarevic | 0:5bdb67970267 | 279 | void SetSnakeType (unsigned char S) |
sdizdarevic | 0:5bdb67970267 | 280 | { |
sdizdarevic | 0:5bdb67970267 | 281 | if (S == SnakeType) return; |
sdizdarevic | 0:5bdb67970267 | 282 | int ST = S < 1 ? 1 : S > 6 ? 6 : S; |
sdizdarevic | 0:5bdb67970267 | 283 | SnakeType = ST - 1; |
sdizdarevic | 0:5bdb67970267 | 284 | Refresh(); |
sdizdarevic | 0:5bdb67970267 | 285 | } |
sdizdarevic | 0:5bdb67970267 | 286 | int GetFoodType () const {return FoodType + 1;} |
sdizdarevic | 0:5bdb67970267 | 287 | void SetFoodType (unsigned char S) |
sdizdarevic | 0:5bdb67970267 | 288 | { |
sdizdarevic | 0:5bdb67970267 | 289 | if (S == FoodType) return; |
sdizdarevic | 0:5bdb67970267 | 290 | int FT = S < 1 ? 1 : S > 5 ? 5 : S; |
sdizdarevic | 0:5bdb67970267 | 291 | FoodType = FT - 1; |
sdizdarevic | 0:5bdb67970267 | 292 | Refresh(); |
sdizdarevic | 0:5bdb67970267 | 293 | } |
sdizdarevic | 0:5bdb67970267 | 294 | void PauseGame () {Pause = true; Draw = false;} |
sdizdarevic | 0:5bdb67970267 | 295 | void ResumeGame () {Pause = false; Draw = true;} |
sdizdarevic | 0:5bdb67970267 | 296 | bool Paused () const {return Pause;} |
sdizdarevic | 0:5bdb67970267 | 297 | void Restart() |
sdizdarevic | 0:5bdb67970267 | 298 | { |
sdizdarevic | 0:5bdb67970267 | 299 | TMove.detach(); |
sdizdarevic | 0:5bdb67970267 | 300 | SnakeLen = eY = Score = 0; |
sdizdarevic | 0:5bdb67970267 | 301 | eX = 3; |
sdizdarevic | 0:5bdb67970267 | 302 | Dir = 2; |
sdizdarevic | 0:5bdb67970267 | 303 | Started = false; |
sdizdarevic | 0:5bdb67970267 | 304 | Start(); |
sdizdarevic | 0:5bdb67970267 | 305 | } |
sdizdarevic | 0:5bdb67970267 | 306 | }; |
sdizdarevic | 0:5bdb67970267 | 307 | const bool Snakelib::SnakeTypes [2][6][3][3] = |
sdizdarevic | 0:5bdb67970267 | 308 | { |
sdizdarevic | 0:5bdb67970267 | 309 | { |
sdizdarevic | 0:5bdb67970267 | 310 | {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 311 | {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 312 | {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, |
sdizdarevic | 0:5bdb67970267 | 313 | {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}, |
sdizdarevic | 0:5bdb67970267 | 314 | {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 315 | {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}} |
sdizdarevic | 0:5bdb67970267 | 316 | }, |
sdizdarevic | 0:5bdb67970267 | 317 | { |
sdizdarevic | 0:5bdb67970267 | 318 | {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 319 | {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 320 | {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}, |
sdizdarevic | 0:5bdb67970267 | 321 | {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, |
sdizdarevic | 0:5bdb67970267 | 322 | {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 323 | {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}} |
sdizdarevic | 0:5bdb67970267 | 324 | } |
sdizdarevic | 0:5bdb67970267 | 325 | }; |
sdizdarevic | 0:5bdb67970267 | 326 | const bool Snakelib::SnakeFood [5][3][3] = |
sdizdarevic | 0:5bdb67970267 | 327 | { |
sdizdarevic | 0:5bdb67970267 | 328 | {{1, 0, 1}, {0, 1, 0}, {1, 0, 1}}, |
sdizdarevic | 0:5bdb67970267 | 329 | {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 330 | {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, |
sdizdarevic | 0:5bdb67970267 | 331 | {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, |
sdizdarevic | 0:5bdb67970267 | 332 | {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}} |
sdizdarevic | 0:5bdb67970267 | 333 | }; |
sdizdarevic | 0:5bdb67970267 | 334 | const int Snakelib::Pos [2][3][3] = |
sdizdarevic | 0:5bdb67970267 | 335 | { |
sdizdarevic | 0:5bdb67970267 | 336 | {{-1, 0, 1}, {-1, 0, 1}, {-1, 0, 1}}, |
sdizdarevic | 0:5bdb67970267 | 337 | {{1, 1, 1}, {0, 0, 0}, {-1, -1, -1}} |
sdizdarevic | 0:5bdb67970267 | 338 | }; |
sdizdarevic | 0:5bdb67970267 | 339 | const double Snakelib::Speeds [10] = {1., .8, .7, .5, .32, .25, .15, .1, .075, .050}; |
sdizdarevic | 0:5bdb67970267 | 340 | const unsigned char Snakelib::Points[10] = {1, 1, 1, 2, 3, 4, 5, 6, 8, 10}; |
sdizdarevic | 0:5bdb67970267 | 341 | #endif //E_SNAKE_H |