meurig phillips snake game - accidentally published to my account instead of to the group!
Dependencies: Joystick N5110 SDFileSystem beep fsmMenu mbed
Fork of SnakeProjectRev1 by
main.h@14:56e355c5cfc9, 2016-05-01 (annotated)
- Committer:
- meurigp
- Date:
- Sun May 01 16:50:37 2016 +0000
- Revision:
- 14:56e355c5cfc9
- Parent:
- 13:08159ea3d556
- Child:
- 15:a5590211888c
gameTicker implemented, double pause glitch to be fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
meurigp | 10:7820b46476ea | 1 | /** |
meurigp | 10:7820b46476ea | 2 | @file main.h |
meurigp | 11:f8478bc749e0 | 3 | @brief Header file containing functions prototypes, defines and global variables for the snake game |
meurigp | 10:7820b46476ea | 4 | @brief Revision 1.0. |
meurigp | 10:7820b46476ea | 5 | @author Meurig Phillips |
meurigp | 10:7820b46476ea | 6 | @date April 2016 |
meurigp | 10:7820b46476ea | 7 | */ |
meurigp | 10:7820b46476ea | 8 | |
meurigp | 10:7820b46476ea | 9 | #ifndef MAIN_H |
meurigp | 10:7820b46476ea | 10 | #define MAIN_H |
meurigp | 10:7820b46476ea | 11 | |
meurigp | 10:7820b46476ea | 12 | #include "mbed.h" |
meurigp | 10:7820b46476ea | 13 | |
meurigp | 10:7820b46476ea | 14 | /** |
meurigp | 10:7820b46476ea | 15 | @namespace greenLed |
meurigp | 10:7820b46476ea | 16 | @brief GPIO output for green LED |
meurigp | 10:7820b46476ea | 17 | @namespace redLed |
meurigp | 10:7820b46476ea | 18 | @brief GPIO output for red LED |
meurigp | 14:56e355c5cfc9 | 19 | @namespace pot |
meurigp | 14:56e355c5cfc9 | 20 | @brief GPIO input for the POT |
meurigp | 11:f8478bc749e0 | 21 | @namespace buzzer |
meurigp | 11:f8478bc749e0 | 22 | @brief GPIO output for buzzer |
meurigp | 10:7820b46476ea | 23 | */ |
meurigp | 11:f8478bc749e0 | 24 | |
meurigp | 10:7820b46476ea | 25 | DigitalOut greenLed(PTC2); |
meurigp | 11:f8478bc749e0 | 26 | DigitalOut redLed(PTA2); |
meurigp | 14:56e355c5cfc9 | 27 | AnalogIn pot(PTB10); |
meurigp | 11:f8478bc749e0 | 28 | //Beep buzzer(PTA1); |
meurigp | 11:f8478bc749e0 | 29 | |
meurigp | 11:f8478bc749e0 | 30 | /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading)) |
meurigp | 11:f8478bc749e0 | 31 | enum CurrentDirection { |
meurigp | 11:f8478bc749e0 | 32 | up, |
meurigp | 11:f8478bc749e0 | 33 | down, |
meurigp | 11:f8478bc749e0 | 34 | left, |
meurigp | 11:f8478bc749e0 | 35 | right, |
meurigp | 11:f8478bc749e0 | 36 | centre, |
meurigp | 11:f8478bc749e0 | 37 | }; |
meurigp | 11:f8478bc749e0 | 38 | CurrentDirection currentDirection = centre; /// intialise direction at beginning |
meurigp | 11:f8478bc749e0 | 39 | |
meurigp | 14:56e355c5cfc9 | 40 | /// create enumerated type (0,1,2 etc. for different game modes on the menu) |
meurigp | 14:56e355c5cfc9 | 41 | enum GameType { |
meurigp | 14:56e355c5cfc9 | 42 | classic, |
meurigp | 14:56e355c5cfc9 | 43 | infiniteMap, |
meurigp | 14:56e355c5cfc9 | 44 | hardMap, |
meurigp | 14:56e355c5cfc9 | 45 | }; |
meurigp | 14:56e355c5cfc9 | 46 | |
meurigp | 14:56e355c5cfc9 | 47 | |
meurigp | 11:f8478bc749e0 | 48 | int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */ |
meurigp | 11:f8478bc749e0 | 49 | int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */ |
meurigp | 11:f8478bc749e0 | 50 | int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */ |
meurigp | 11:f8478bc749e0 | 51 | int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */ |
meurigp | 11:f8478bc749e0 | 52 | int snakeTailX[100]; /*!< array for X coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 53 | int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 54 | int snakeTailLength = 3; /*!< length of snake, intialised to 3 */ |
meurigp | 11:f8478bc749e0 | 55 | int score = 0; /*!< score for current round */ |
meurigp | 11:f8478bc749e0 | 56 | int top_score = 0; /*!< top score read and write from the SD card */ |
meurigp | 11:f8478bc749e0 | 57 | int fruitValue = 10; /*!< value of the fruit */ |
meurigp | 14:56e355c5cfc9 | 58 | int i = 40; /*!< x origin of snake head, intialised at 41 */ |
meurigp | 14:56e355c5cfc9 | 59 | int j = 22; /*!< y origin of snake head, intialised at 23 */ |
meurigp | 11:f8478bc749e0 | 60 | int prev_i; /*!< integer to store previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 61 | int prev_j; /*!< integer to store previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 62 | int prev2_i; /*!< integer to store previous, previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 63 | int prev2_j; /*!< integer to store previous, previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 64 | bool gamePlaying = false; /*!< bool to store whether the game is in play or not */ |
meurigp | 12:825a402d230f | 65 | int pauseCount; /*!< counts how many times the player has paused */ |
meurigp | 11:f8478bc749e0 | 66 | |
meurigp | 11:f8478bc749e0 | 67 | |
meurigp | 11:f8478bc749e0 | 68 | /** |
meurigp | 11:f8478bc749e0 | 69 | Displays new fruit |
meurigp | 11:f8478bc749e0 | 70 | */ |
meurigp | 11:f8478bc749e0 | 71 | void generateFood(); |
meurigp | 11:f8478bc749e0 | 72 | /** |
meurigp | 11:f8478bc749e0 | 73 | Generates new random coordinates for the fruit |
meurigp | 11:f8478bc749e0 | 74 | */ |
meurigp | 11:f8478bc749e0 | 75 | void newFruitXY(); |
meurigp | 11:f8478bc749e0 | 76 | /** |
meurigp | 13:08159ea3d556 | 77 | Receives input form joystick and updates the current direction |
meurigp | 11:f8478bc749e0 | 78 | */ |
meurigp | 11:f8478bc749e0 | 79 | void moveSnake(); |
meurigp | 11:f8478bc749e0 | 80 | /** |
meurigp | 11:f8478bc749e0 | 81 | Restricted boundaries |
meurigp | 11:f8478bc749e0 | 82 | */ |
meurigp | 11:f8478bc749e0 | 83 | void hardWall(); |
meurigp | 11:f8478bc749e0 | 84 | /** |
meurigp | 11:f8478bc749e0 | 85 | Map with obstacles in the way |
meurigp | 11:f8478bc749e0 | 86 | */ |
meurigp | 11:f8478bc749e0 | 87 | void specialMap(); |
meurigp | 11:f8478bc749e0 | 88 | /** |
meurigp | 11:f8478bc749e0 | 89 | Infinite boundaries |
meurigp | 11:f8478bc749e0 | 90 | */ |
meurigp | 11:f8478bc749e0 | 91 | void wrapAround(); |
meurigp | 11:f8478bc749e0 | 92 | /** |
meurigp | 11:f8478bc749e0 | 93 | Calculates score |
meurigp | 11:f8478bc749e0 | 94 | */ |
meurigp | 11:f8478bc749e0 | 95 | void scoreCalculation(); |
meurigp | 11:f8478bc749e0 | 96 | /** |
meurigp | 11:f8478bc749e0 | 97 | Displayes scores when player dies |
meurigp | 11:f8478bc749e0 | 98 | */ |
meurigp | 11:f8478bc749e0 | 99 | void gameOver(); |
meurigp | 11:f8478bc749e0 | 100 | /** |
meurigp | 11:f8478bc749e0 | 101 | Initilaises the snake so it's moving from left to right to begin with |
meurigp | 11:f8478bc749e0 | 102 | */ |
meurigp | 11:f8478bc749e0 | 103 | void initSnakeTail(); |
meurigp | 11:f8478bc749e0 | 104 | /** |
meurigp | 11:f8478bc749e0 | 105 | Splash screen for the intro of the game |
meurigp | 11:f8478bc749e0 | 106 | */ |
meurigp | 11:f8478bc749e0 | 107 | void snakeIntro(); |
meurigp | 12:825a402d230f | 108 | /** |
meurigp | 12:825a402d230f | 109 | Game paused function |
meurigp | 12:825a402d230f | 110 | */ |
meurigp | 12:825a402d230f | 111 | void gamePaused(); |
meurigp | 13:08159ea3d556 | 112 | /** |
meurigp | 13:08159ea3d556 | 113 | Contains all logic for the snake game and displays accordingly |
meurigp | 13:08159ea3d556 | 114 | */ |
meurigp | 13:08159ea3d556 | 115 | void gameLogic(); |
meurigp | 11:f8478bc749e0 | 116 | |
meurigp | 11:f8478bc749e0 | 117 | |
meurigp | 11:f8478bc749e0 | 118 | #endif |