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
Diff: main.h
- Revision:
- 11:f8478bc749e0
- Parent:
- 10:7820b46476ea
- Child:
- 12:825a402d230f
--- a/main.h Sun May 01 11:11:19 2016 +0000 +++ b/main.h Sun May 01 12:15:09 2016 +0000 @@ -1,6 +1,6 @@ /** @file main.h -@brief Header file containing functions prototypes, defines and global variables. +@brief Header file containing functions prototypes, defines and global variables for the snake game @brief Revision 1.0. @author Meurig Phillips @date April 2016 @@ -12,19 +12,91 @@ #include "mbed.h" /** -@namespace button -@brief GPIO input for joystick button -@namespace xPot -@brief GPIO input for joystick x potentiometer value -@namespace yPot -@brief GPIO input for joystick y potentiometer value @namespace greenLed @brief GPIO output for green LED @namespace redLed @brief GPIO output for red LED +@namespace buzzer +@brief GPIO output for buzzer */ -DigitalIn button(PTB18); -AnalogIn xPot(PTB2); -AnalogIn yPot(PTB3); + DigitalOut greenLed(PTC2); -DigitalOut redLed(PTA2); \ No newline at end of file +DigitalOut redLed(PTA2); +//Beep buzzer(PTA1); + +Ticker pollJoystick; /// timer to regularly read the joystick +Serial serial(USBTX,USBRX); /// serial communication for debugging +Ticker gameTicker; /// timer to update game at regular intervals + +/// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading)) +enum CurrentDirection { + up, + down, + left, + right, + centre, +}; +CurrentDirection currentDirection = centre; /// intialise direction at beginning + +int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */ +int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */ +int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */ +int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */ +int snakeTailX[100]; /*!< array for X coordinate of each snake segment */ +int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */ +int snakeTailLength = 3; /*!< length of snake, intialised to 3 */ +int score = 0; /*!< score for current round */ +int top_score = 0; /*!< top score read and write from the SD card */ +int fruitValue = 10; /*!< value of the fruit */ +int i = 41; /*!< x origin of snake head, intialised at 41 */ +int j = 23; /*!< y origin of snake head, intialised at 23 */ +int prev_i; /*!< integer to store previous value of x/i */ +int prev_j; /*!< integer to store previous value of y/j */ +int prev2_i; /*!< integer to store previous, previous value of x/i */ +int prev2_j; /*!< integer to store previous, previous value of y/j */ +bool gamePlaying = false; /*!< bool to store whether the game is in play or not */ + + +/** +Displays new fruit +*/ +void generateFood(); +/** +Generates new random coordinates for the fruit +*/ +void newFruitXY(); +/** +Main in game function, involves the input, logic and display +*/ +void moveSnake(); +/** +Restricted boundaries +*/ +void hardWall(); +/** +Map with obstacles in the way +*/ +void specialMap(); +/** +Infinite boundaries +*/ +void wrapAround(); +/** +Calculates score +*/ +void scoreCalculation(); +/** +Displayes scores when player dies +*/ +void gameOver(); +/** +Initilaises the snake so it's moving from left to right to begin with +*/ +void initSnakeTail(); +/** +Splash screen for the intro of the game +*/ +void snakeIntro(); + + +#endif \ No newline at end of file