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 Meurig Phillips

Committer:
meurigp
Date:
Wed May 04 12:33:52 2016 +0000
Revision:
15:a5590211888c
Parent:
14:56e355c5cfc9
Child:
16:68b9460d4c76
prior to implementing the fsm menu

Who changed what in which revision?

UserRevisionLine numberNew 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 15:a5590211888c 13 #include "beep.h"
meurigp 10:7820b46476ea 14
meurigp 10:7820b46476ea 15 /**
meurigp 10:7820b46476ea 16 @namespace greenLed
meurigp 10:7820b46476ea 17 @brief GPIO output for green LED
meurigp 10:7820b46476ea 18 @namespace redLed
meurigp 10:7820b46476ea 19 @brief GPIO output for red LED
meurigp 14:56e355c5cfc9 20 @namespace pot
meurigp 14:56e355c5cfc9 21 @brief GPIO input for the POT
meurigp 11:f8478bc749e0 22 @namespace buzzer
meurigp 11:f8478bc749e0 23 @brief GPIO output for buzzer
meurigp 10:7820b46476ea 24 */
meurigp 11:f8478bc749e0 25
meurigp 10:7820b46476ea 26 DigitalOut greenLed(PTC2);
meurigp 11:f8478bc749e0 27 DigitalOut redLed(PTA2);
meurigp 14:56e355c5cfc9 28 AnalogIn pot(PTB10);
meurigp 15:a5590211888c 29 Beep buzzer(PTA1);
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 14:56e355c5cfc9 41 /// create enumerated type (0,1,2 etc. for different game modes on the menu)
meurigp 14:56e355c5cfc9 42 enum GameType {
meurigp 14:56e355c5cfc9 43 classic,
meurigp 14:56e355c5cfc9 44 infiniteMap,
meurigp 14:56e355c5cfc9 45 hardMap,
meurigp 14:56e355c5cfc9 46 };
meurigp 14:56e355c5cfc9 47
meurigp 14:56e355c5cfc9 48
meurigp 11:f8478bc749e0 49 int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */
meurigp 11:f8478bc749e0 50 int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */
meurigp 11:f8478bc749e0 51 int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */
meurigp 11:f8478bc749e0 52 int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */
meurigp 11:f8478bc749e0 53 int snakeTailX[100]; /*!< array for X coordinate of each snake segment */
meurigp 11:f8478bc749e0 54 int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */
meurigp 11:f8478bc749e0 55 int snakeTailLength = 3; /*!< length of snake, intialised to 3 */
meurigp 11:f8478bc749e0 56 int score = 0; /*!< score for current round */
meurigp 11:f8478bc749e0 57 int top_score = 0; /*!< top score read and write from the SD card */
meurigp 11:f8478bc749e0 58 int fruitValue = 10; /*!< value of the fruit */
meurigp 14:56e355c5cfc9 59 int i = 40; /*!< x origin of snake head, intialised at 41 */
meurigp 14:56e355c5cfc9 60 int j = 22; /*!< y origin of snake head, intialised at 23 */
meurigp 11:f8478bc749e0 61 int prev_i; /*!< integer to store previous value of x/i */
meurigp 11:f8478bc749e0 62 int prev_j; /*!< integer to store previous value of y/j */
meurigp 11:f8478bc749e0 63 int prev2_i; /*!< integer to store previous, previous value of x/i */
meurigp 11:f8478bc749e0 64 int prev2_j; /*!< integer to store previous, previous value of y/j */
meurigp 11:f8478bc749e0 65 bool gamePlaying = false; /*!< bool to store whether the game is in play or not */
meurigp 12:825a402d230f 66 int pauseCount; /*!< counts how many times the player has paused */
meurigp 11:f8478bc749e0 67
meurigp 11:f8478bc749e0 68
meurigp 11:f8478bc749e0 69 /**
meurigp 11:f8478bc749e0 70 Displays new fruit
meurigp 11:f8478bc749e0 71 */
meurigp 11:f8478bc749e0 72 void generateFood();
meurigp 11:f8478bc749e0 73 /**
meurigp 11:f8478bc749e0 74 Generates new random coordinates for the fruit
meurigp 11:f8478bc749e0 75 */
meurigp 11:f8478bc749e0 76 void newFruitXY();
meurigp 11:f8478bc749e0 77 /**
meurigp 13:08159ea3d556 78 Receives input form joystick and updates the current direction
meurigp 11:f8478bc749e0 79 */
meurigp 11:f8478bc749e0 80 void moveSnake();
meurigp 11:f8478bc749e0 81 /**
meurigp 11:f8478bc749e0 82 Restricted boundaries
meurigp 11:f8478bc749e0 83 */
meurigp 11:f8478bc749e0 84 void hardWall();
meurigp 11:f8478bc749e0 85 /**
meurigp 11:f8478bc749e0 86 Map with obstacles in the way
meurigp 11:f8478bc749e0 87 */
meurigp 11:f8478bc749e0 88 void specialMap();
meurigp 11:f8478bc749e0 89 /**
meurigp 11:f8478bc749e0 90 Infinite boundaries
meurigp 11:f8478bc749e0 91 */
meurigp 11:f8478bc749e0 92 void wrapAround();
meurigp 11:f8478bc749e0 93 /**
meurigp 11:f8478bc749e0 94 Calculates score
meurigp 11:f8478bc749e0 95 */
meurigp 11:f8478bc749e0 96 void scoreCalculation();
meurigp 11:f8478bc749e0 97 /**
meurigp 11:f8478bc749e0 98 Displayes scores when player dies
meurigp 11:f8478bc749e0 99 */
meurigp 11:f8478bc749e0 100 void gameOver();
meurigp 11:f8478bc749e0 101 /**
meurigp 11:f8478bc749e0 102 Initilaises the snake so it's moving from left to right to begin with
meurigp 11:f8478bc749e0 103 */
meurigp 11:f8478bc749e0 104 void initSnakeTail();
meurigp 11:f8478bc749e0 105 /**
meurigp 11:f8478bc749e0 106 Splash screen for the intro of the game
meurigp 11:f8478bc749e0 107 */
meurigp 11:f8478bc749e0 108 void snakeIntro();
meurigp 12:825a402d230f 109 /**
meurigp 12:825a402d230f 110 Game paused function
meurigp 12:825a402d230f 111 */
meurigp 12:825a402d230f 112 void gamePaused();
meurigp 13:08159ea3d556 113 /**
meurigp 13:08159ea3d556 114 Contains all logic for the snake game and displays accordingly
meurigp 13:08159ea3d556 115 */
meurigp 13:08159ea3d556 116 void gameLogic();
meurigp 11:f8478bc749e0 117
meurigp 11:f8478bc749e0 118
meurigp 11:f8478bc749e0 119 #endif