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
main.cpp@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 | #include "mbed.h" |
sdizdarevic | 0:5bdb67970267 | 2 | #include "Snakelib.h" |
sdizdarevic | 0:5bdb67970267 | 3 | #include "Helper.h" |
sdizdarevic | 0:5bdb67970267 | 4 | Serial PC (USBTX, USBRX); |
sdizdarevic | 0:5bdb67970267 | 5 | Ticker T; |
sdizdarevic | 0:5bdb67970267 | 6 | |
sdizdarevic | 0:5bdb67970267 | 7 | //Very important to connect! Snakelib class parametters from N5110 lcd (VCC,SCE, RST,D/C, MOSI,SCLK,LED) and the last 3 from joystick |
sdizdarevic | 0:5bdb67970267 | 8 | |
sdizdarevic | 0:5bdb67970267 | 9 | Snakelib Snake (PTB2, PTD0, PTB9, PTB3, PTD2, PTD1, PTD0, PTB11, PTC11, PTC10); |
sdizdarevic | 0:5bdb67970267 | 10 | void Start() |
sdizdarevic | 0:5bdb67970267 | 11 | { |
sdizdarevic | 0:5bdb67970267 | 12 | if (!PC.readable()) return; |
sdizdarevic | 0:5bdb67970267 | 13 | char C = PC.getc(); |
sdizdarevic | 0:5bdb67970267 | 14 | while (!IsOurChar (C)) C = PC.getc(); |
sdizdarevic | 0:5bdb67970267 | 15 | C = ToUpper (C); |
sdizdarevic | 0:5bdb67970267 | 16 | static char Buff[4] = "000"; |
sdizdarevic | 0:5bdb67970267 | 17 | if (C == 'B' || C == 'Z' || C == 'H') |
sdizdarevic | 0:5bdb67970267 | 18 | { |
sdizdarevic | 0:5bdb67970267 | 19 | int N = -7; |
sdizdarevic | 0:5bdb67970267 | 20 | if (C == 'B') //B01 - B10 |
sdizdarevic | 0:5bdb67970267 | 21 | { |
sdizdarevic | 0:5bdb67970267 | 22 | Buff[0] = PC.getc(); |
sdizdarevic | 0:5bdb67970267 | 23 | Buff[1] = PC.getc(); |
sdizdarevic | 0:5bdb67970267 | 24 | N = ToInt (Buff); |
sdizdarevic | 0:5bdb67970267 | 25 | if (N == -7) return; |
sdizdarevic | 0:5bdb67970267 | 26 | Snake.SetSnakeSpeed (N); |
sdizdarevic | 0:5bdb67970267 | 27 | PC.printf ("\nSnake speed changed to %d\n", N); |
sdizdarevic | 0:5bdb67970267 | 28 | return; |
sdizdarevic | 0:5bdb67970267 | 29 | } |
sdizdarevic | 0:5bdb67970267 | 30 | Buff[0] = PC.getc(); |
sdizdarevic | 0:5bdb67970267 | 31 | Buff[1] = 0; |
sdizdarevic | 0:5bdb67970267 | 32 | if ((N = ToInt (Buff)) == -7) return; |
sdizdarevic | 0:5bdb67970267 | 33 | if (C == 'Z') {Snake.SetSnakeType (N); PC.printf ("\nSnake type changed to %d\n", N);} |
sdizdarevic | 0:5bdb67970267 | 34 | else {Snake.SetFoodType (N); PC.printf ("\nSnake food changed to %d\n", N);} |
sdizdarevic | 0:5bdb67970267 | 35 | } |
sdizdarevic | 0:5bdb67970267 | 36 | else if (C == 'P') |
sdizdarevic | 0:5bdb67970267 | 37 | { |
sdizdarevic | 0:5bdb67970267 | 38 | if (Snake.Paused()) Snake.ResumeGame(), PC.printf ("\nContinuing...\n"); |
sdizdarevic | 0:5bdb67970267 | 39 | else Snake.PauseGame(), PC.printf ("\nPause...\n"); |
sdizdarevic | 0:5bdb67970267 | 40 | } |
sdizdarevic | 0:5bdb67970267 | 41 | else if (C == 'R') Snake.Restart(), PC.printf ("\nRestarted!\n"); |
sdizdarevic | 0:5bdb67970267 | 42 | } |
sdizdarevic | 0:5bdb67970267 | 43 | int main () |
sdizdarevic | 0:5bdb67970267 | 44 | { |
sdizdarevic | 0:5bdb67970267 | 45 | |
sdizdarevic | 0:5bdb67970267 | 46 | PC.printf ("\nManual:\n\n-P -> Pause/Continue\n-Z# -> Change snake type. Allowed values Z1-Z6\n"); |
sdizdarevic | 0:5bdb67970267 | 47 | PC.printf ("-H# -> Change snake food. Allowed values H1-H5\n-R -> Restart game\n"); |
sdizdarevic | 0:5bdb67970267 | 48 | PC.printf ("-B# -> Change snake speed. Allowed B01-B10\n"); |
sdizdarevic | 0:5bdb67970267 | 49 | PC.printf ("\nTemp settings: B6, Z1, H1\nTo start playing, use joystick.\n\n \n\n"); |
sdizdarevic | 0:5bdb67970267 | 50 | T.attach (&Start, 0.01); |
sdizdarevic | 0:5bdb67970267 | 51 | Snake.START(); |
sdizdarevic | 0:5bdb67970267 | 52 | while (7); |
sdizdarevic | 0:5bdb67970267 | 53 | } |