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@13:08159ea3d556, 2016-05-01 (annotated)
- Committer:
- meurigp
- Date:
- Sun May 01 14:43:01 2016 +0000
- Revision:
- 13:08159ea3d556
- Parent:
- 12:825a402d230f
- Child:
- 14:56e355c5cfc9
moveSnake() cleaned up
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 | 11:f8478bc749e0 | 19 | @namespace buzzer |
meurigp | 11:f8478bc749e0 | 20 | @brief GPIO output for buzzer |
meurigp | 10:7820b46476ea | 21 | */ |
meurigp | 11:f8478bc749e0 | 22 | |
meurigp | 10:7820b46476ea | 23 | DigitalOut greenLed(PTC2); |
meurigp | 11:f8478bc749e0 | 24 | DigitalOut redLed(PTA2); |
meurigp | 11:f8478bc749e0 | 25 | //Beep buzzer(PTA1); |
meurigp | 11:f8478bc749e0 | 26 | |
meurigp | 11:f8478bc749e0 | 27 | /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading)) |
meurigp | 11:f8478bc749e0 | 28 | enum CurrentDirection { |
meurigp | 11:f8478bc749e0 | 29 | up, |
meurigp | 11:f8478bc749e0 | 30 | down, |
meurigp | 11:f8478bc749e0 | 31 | left, |
meurigp | 11:f8478bc749e0 | 32 | right, |
meurigp | 11:f8478bc749e0 | 33 | centre, |
meurigp | 11:f8478bc749e0 | 34 | }; |
meurigp | 11:f8478bc749e0 | 35 | CurrentDirection currentDirection = centre; /// intialise direction at beginning |
meurigp | 11:f8478bc749e0 | 36 | |
meurigp | 12:825a402d230f | 37 | float gameSpeed = 0.1; /*!< determines speed of game according to pot value */ |
meurigp | 11:f8478bc749e0 | 38 | int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */ |
meurigp | 11:f8478bc749e0 | 39 | int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */ |
meurigp | 11:f8478bc749e0 | 40 | int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */ |
meurigp | 11:f8478bc749e0 | 41 | int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */ |
meurigp | 11:f8478bc749e0 | 42 | int snakeTailX[100]; /*!< array for X coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 43 | int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 44 | int snakeTailLength = 3; /*!< length of snake, intialised to 3 */ |
meurigp | 11:f8478bc749e0 | 45 | int score = 0; /*!< score for current round */ |
meurigp | 11:f8478bc749e0 | 46 | int top_score = 0; /*!< top score read and write from the SD card */ |
meurigp | 11:f8478bc749e0 | 47 | int fruitValue = 10; /*!< value of the fruit */ |
meurigp | 11:f8478bc749e0 | 48 | int i = 41; /*!< x origin of snake head, intialised at 41 */ |
meurigp | 11:f8478bc749e0 | 49 | int j = 23; /*!< y origin of snake head, intialised at 23 */ |
meurigp | 11:f8478bc749e0 | 50 | int prev_i; /*!< integer to store previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 51 | int prev_j; /*!< integer to store previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 52 | int prev2_i; /*!< integer to store previous, previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 53 | int prev2_j; /*!< integer to store previous, previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 54 | bool gamePlaying = false; /*!< bool to store whether the game is in play or not */ |
meurigp | 12:825a402d230f | 55 | int pauseCount; /*!< counts how many times the player has paused */ |
meurigp | 11:f8478bc749e0 | 56 | |
meurigp | 11:f8478bc749e0 | 57 | |
meurigp | 11:f8478bc749e0 | 58 | /** |
meurigp | 11:f8478bc749e0 | 59 | Displays new fruit |
meurigp | 11:f8478bc749e0 | 60 | */ |
meurigp | 11:f8478bc749e0 | 61 | void generateFood(); |
meurigp | 11:f8478bc749e0 | 62 | /** |
meurigp | 11:f8478bc749e0 | 63 | Generates new random coordinates for the fruit |
meurigp | 11:f8478bc749e0 | 64 | */ |
meurigp | 11:f8478bc749e0 | 65 | void newFruitXY(); |
meurigp | 11:f8478bc749e0 | 66 | /** |
meurigp | 13:08159ea3d556 | 67 | Receives input form joystick and updates the current direction |
meurigp | 11:f8478bc749e0 | 68 | */ |
meurigp | 11:f8478bc749e0 | 69 | void moveSnake(); |
meurigp | 11:f8478bc749e0 | 70 | /** |
meurigp | 11:f8478bc749e0 | 71 | Restricted boundaries |
meurigp | 11:f8478bc749e0 | 72 | */ |
meurigp | 11:f8478bc749e0 | 73 | void hardWall(); |
meurigp | 11:f8478bc749e0 | 74 | /** |
meurigp | 11:f8478bc749e0 | 75 | Map with obstacles in the way |
meurigp | 11:f8478bc749e0 | 76 | */ |
meurigp | 11:f8478bc749e0 | 77 | void specialMap(); |
meurigp | 11:f8478bc749e0 | 78 | /** |
meurigp | 11:f8478bc749e0 | 79 | Infinite boundaries |
meurigp | 11:f8478bc749e0 | 80 | */ |
meurigp | 11:f8478bc749e0 | 81 | void wrapAround(); |
meurigp | 11:f8478bc749e0 | 82 | /** |
meurigp | 11:f8478bc749e0 | 83 | Calculates score |
meurigp | 11:f8478bc749e0 | 84 | */ |
meurigp | 11:f8478bc749e0 | 85 | void scoreCalculation(); |
meurigp | 11:f8478bc749e0 | 86 | /** |
meurigp | 11:f8478bc749e0 | 87 | Displayes scores when player dies |
meurigp | 11:f8478bc749e0 | 88 | */ |
meurigp | 11:f8478bc749e0 | 89 | void gameOver(); |
meurigp | 11:f8478bc749e0 | 90 | /** |
meurigp | 11:f8478bc749e0 | 91 | Initilaises the snake so it's moving from left to right to begin with |
meurigp | 11:f8478bc749e0 | 92 | */ |
meurigp | 11:f8478bc749e0 | 93 | void initSnakeTail(); |
meurigp | 11:f8478bc749e0 | 94 | /** |
meurigp | 11:f8478bc749e0 | 95 | Splash screen for the intro of the game |
meurigp | 11:f8478bc749e0 | 96 | */ |
meurigp | 11:f8478bc749e0 | 97 | void snakeIntro(); |
meurigp | 12:825a402d230f | 98 | /** |
meurigp | 12:825a402d230f | 99 | Game paused function |
meurigp | 12:825a402d230f | 100 | */ |
meurigp | 12:825a402d230f | 101 | void gamePaused(); |
meurigp | 13:08159ea3d556 | 102 | /** |
meurigp | 13:08159ea3d556 | 103 | Contains all logic for the snake game and displays accordingly |
meurigp | 13:08159ea3d556 | 104 | */ |
meurigp | 13:08159ea3d556 | 105 | void gameLogic(); |
meurigp | 11:f8478bc749e0 | 106 | |
meurigp | 11:f8478bc749e0 | 107 | |
meurigp | 11:f8478bc749e0 | 108 | #endif |