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@11:f8478bc749e0, 2016-05-01 (annotated)
- Committer:
- meurigp
- Date:
- Sun May 01 12:15:09 2016 +0000
- Revision:
- 11:f8478bc749e0
- Parent:
- 10:7820b46476ea
- Child:
- 12:825a402d230f
Game works with header file implemented, some minor errors to be fixed within the header file;
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 | Ticker pollJoystick; /// timer to regularly read the joystick |
meurigp | 11:f8478bc749e0 | 28 | Serial serial(USBTX,USBRX); /// serial communication for debugging |
meurigp | 11:f8478bc749e0 | 29 | Ticker gameTicker; /// timer to update game at regular intervals |
meurigp | 11:f8478bc749e0 | 30 | |
meurigp | 11:f8478bc749e0 | 31 | /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading)) |
meurigp | 11:f8478bc749e0 | 32 | enum CurrentDirection { |
meurigp | 11:f8478bc749e0 | 33 | up, |
meurigp | 11:f8478bc749e0 | 34 | down, |
meurigp | 11:f8478bc749e0 | 35 | left, |
meurigp | 11:f8478bc749e0 | 36 | right, |
meurigp | 11:f8478bc749e0 | 37 | centre, |
meurigp | 11:f8478bc749e0 | 38 | }; |
meurigp | 11:f8478bc749e0 | 39 | CurrentDirection currentDirection = centre; /// intialise direction at beginning |
meurigp | 11:f8478bc749e0 | 40 | |
meurigp | 11:f8478bc749e0 | 41 | int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */ |
meurigp | 11:f8478bc749e0 | 42 | int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */ |
meurigp | 11:f8478bc749e0 | 43 | int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */ |
meurigp | 11:f8478bc749e0 | 44 | int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */ |
meurigp | 11:f8478bc749e0 | 45 | int snakeTailX[100]; /*!< array for X coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 46 | int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 47 | int snakeTailLength = 3; /*!< length of snake, intialised to 3 */ |
meurigp | 11:f8478bc749e0 | 48 | int score = 0; /*!< score for current round */ |
meurigp | 11:f8478bc749e0 | 49 | int top_score = 0; /*!< top score read and write from the SD card */ |
meurigp | 11:f8478bc749e0 | 50 | int fruitValue = 10; /*!< value of the fruit */ |
meurigp | 11:f8478bc749e0 | 51 | int i = 41; /*!< x origin of snake head, intialised at 41 */ |
meurigp | 11:f8478bc749e0 | 52 | int j = 23; /*!< y origin of snake head, intialised at 23 */ |
meurigp | 11:f8478bc749e0 | 53 | int prev_i; /*!< integer to store previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 54 | int prev_j; /*!< integer to store previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 55 | int prev2_i; /*!< integer to store previous, previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 56 | int prev2_j; /*!< integer to store previous, previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 57 | bool gamePlaying = false; /*!< bool to store whether the game is in play or not */ |
meurigp | 11:f8478bc749e0 | 58 | |
meurigp | 11:f8478bc749e0 | 59 | |
meurigp | 11:f8478bc749e0 | 60 | /** |
meurigp | 11:f8478bc749e0 | 61 | Displays new fruit |
meurigp | 11:f8478bc749e0 | 62 | */ |
meurigp | 11:f8478bc749e0 | 63 | void generateFood(); |
meurigp | 11:f8478bc749e0 | 64 | /** |
meurigp | 11:f8478bc749e0 | 65 | Generates new random coordinates for the fruit |
meurigp | 11:f8478bc749e0 | 66 | */ |
meurigp | 11:f8478bc749e0 | 67 | void newFruitXY(); |
meurigp | 11:f8478bc749e0 | 68 | /** |
meurigp | 11:f8478bc749e0 | 69 | Main in game function, involves the input, logic and display |
meurigp | 11:f8478bc749e0 | 70 | */ |
meurigp | 11:f8478bc749e0 | 71 | void moveSnake(); |
meurigp | 11:f8478bc749e0 | 72 | /** |
meurigp | 11:f8478bc749e0 | 73 | Restricted boundaries |
meurigp | 11:f8478bc749e0 | 74 | */ |
meurigp | 11:f8478bc749e0 | 75 | void hardWall(); |
meurigp | 11:f8478bc749e0 | 76 | /** |
meurigp | 11:f8478bc749e0 | 77 | Map with obstacles in the way |
meurigp | 11:f8478bc749e0 | 78 | */ |
meurigp | 11:f8478bc749e0 | 79 | void specialMap(); |
meurigp | 11:f8478bc749e0 | 80 | /** |
meurigp | 11:f8478bc749e0 | 81 | Infinite boundaries |
meurigp | 11:f8478bc749e0 | 82 | */ |
meurigp | 11:f8478bc749e0 | 83 | void wrapAround(); |
meurigp | 11:f8478bc749e0 | 84 | /** |
meurigp | 11:f8478bc749e0 | 85 | Calculates score |
meurigp | 11:f8478bc749e0 | 86 | */ |
meurigp | 11:f8478bc749e0 | 87 | void scoreCalculation(); |
meurigp | 11:f8478bc749e0 | 88 | /** |
meurigp | 11:f8478bc749e0 | 89 | Displayes scores when player dies |
meurigp | 11:f8478bc749e0 | 90 | */ |
meurigp | 11:f8478bc749e0 | 91 | void gameOver(); |
meurigp | 11:f8478bc749e0 | 92 | /** |
meurigp | 11:f8478bc749e0 | 93 | Initilaises the snake so it's moving from left to right to begin with |
meurigp | 11:f8478bc749e0 | 94 | */ |
meurigp | 11:f8478bc749e0 | 95 | void initSnakeTail(); |
meurigp | 11:f8478bc749e0 | 96 | /** |
meurigp | 11:f8478bc749e0 | 97 | Splash screen for the intro of the game |
meurigp | 11:f8478bc749e0 | 98 | */ |
meurigp | 11:f8478bc749e0 | 99 | void snakeIntro(); |
meurigp | 11:f8478bc749e0 | 100 | |
meurigp | 11:f8478bc749e0 | 101 | |
meurigp | 11:f8478bc749e0 | 102 | #endif |