Code for simple game of snake with four playing modes.

Dependencies:   N5110 PinDetect PowerControl mbed

Committer:
fiifimills
Date:
Mon May 11 01:51:37 2015 +0000
Revision:
12:fa654504b51f
Final Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fiifimills 12:fa654504b51f 1 /**
fiifimills 12:fa654504b51f 2 @file main.h
fiifimills 12:fa654504b51f 3 @brief Header file for Snake, containing functions prototypes and global variables.
fiifimills 12:fa654504b51f 4 @brief Revision 1.0.
fiifimills 12:fa654504b51f 5 @author Fiifi Mills
fiifimills 12:fa654504b51f 6 @date May 2015
fiifimills 12:fa654504b51f 7 */
fiifimills 12:fa654504b51f 8
fiifimills 12:fa654504b51f 9 #include "mbed.h"
fiifimills 12:fa654504b51f 10 #include "N5110.h"
fiifimills 12:fa654504b51f 11 #include "PinDetect.h"
fiifimills 12:fa654504b51f 12 #include "PowerControl/PowerControl.h"
fiifimills 12:fa654504b51f 13 #include "PowerControl/EthernetPowerControl.h"
fiifimills 12:fa654504b51f 14 #include <vector>
fiifimills 12:fa654504b51f 15 #include <stdlib.h> /* srand, rand */
fiifimills 12:fa654504b51f 16 #include <time.h>
fiifimills 12:fa654504b51f 17
fiifimills 12:fa654504b51f 18 /**
fiifimills 12:fa654504b51f 19 Enumerated type used for directions read from joystick
fiifimills 12:fa654504b51f 20 */
fiifimills 12:fa654504b51f 21
fiifimills 12:fa654504b51f 22 enum DirectionName {
fiifimills 12:fa654504b51f 23 UP,
fiifimills 12:fa654504b51f 24 DOWN,
fiifimills 12:fa654504b51f 25 LEFT,
fiifimills 12:fa654504b51f 26 RIGHT,
fiifimills 12:fa654504b51f 27 CENTRE,
fiifimills 12:fa654504b51f 28 UNKNOWN
fiifimills 12:fa654504b51f 29 };
fiifimills 12:fa654504b51f 30
fiifimills 12:fa654504b51f 31 DirectionName currentDirection; /*!< DirectionName variable used to refer to the joystick's current direction */
fiifimills 12:fa654504b51f 32 DirectionName previousDirection; /*!< DirectionName variable used to refer to the snake's previous direction */
fiifimills 12:fa654504b51f 33
fiifimills 12:fa654504b51f 34 /**
fiifimills 12:fa654504b51f 35 Enumerated type used to display different game menu screens
fiifimills 12:fa654504b51f 36 */
fiifimills 12:fa654504b51f 37
fiifimills 12:fa654504b51f 38 enum GameMenu {
fiifimills 12:fa654504b51f 39 STARTUP,
fiifimills 12:fa654504b51f 40 SELECTSPEED,
fiifimills 12:fa654504b51f 41 GAMEPLAY
fiifimills 12:fa654504b51f 42 };
fiifimills 12:fa654504b51f 43
fiifimills 12:fa654504b51f 44 GameMenu currentGameMenu; /*!< GameMenu variable used to refer to the current game menu being displayed */
fiifimills 12:fa654504b51f 45
fiifimills 12:fa654504b51f 46 /**
fiifimills 12:fa654504b51f 47 Struct used for creating coordinates
fiifimills 12:fa654504b51f 48 */
fiifimills 12:fa654504b51f 49
fiifimills 12:fa654504b51f 50 struct coordinate {
fiifimills 12:fa654504b51f 51 int x;
fiifimills 12:fa654504b51f 52 int y;
fiifimills 12:fa654504b51f 53 };
fiifimills 12:fa654504b51f 54
fiifimills 12:fa654504b51f 55
fiifimills 12:fa654504b51f 56 /**
fiifimills 12:fa654504b51f 57 Various flags used in the game for notification of specific events
fiifimills 12:fa654504b51f 58 */
fiifimills 12:fa654504b51f 59
fiifimills 12:fa654504b51f 60 int printFlag; /*!< print flag set in ISR */
fiifimills 12:fa654504b51f 61 int Aflag; /*!< Aflag set in ISR when button A pressed */
fiifimills 12:fa654504b51f 62 int Bflag; /*!< Bflag set in ISR when button B pressed */
fiifimills 12:fa654504b51f 63 int collisionFlag; /*!< collisionflag set in moveSnake() when snake collides with self */
fiifimills 12:fa654504b51f 64 int borderFlag; /*!< borderflag set when snake collides with border in insane mode */
fiifimills 12:fa654504b51f 65
fiifimills 12:fa654504b51f 66 /**
fiifimills 12:fa654504b51f 67 Global variables
fiifimills 12:fa654504b51f 68 */
fiifimills 12:fa654504b51f 69
fiifimills 12:fa654504b51f 70 int score; /*!< The score that the user has accumulated throughout the game */
fiifimills 12:fa654504b51f 71 int xCentre; /*!< The x coordinate of the centre of the selection circle used in the SELECTSPEED game menu */
fiifimills 12:fa654504b51f 72 int yCentre; /*!< The y coordinate of the centre of the selection circle used in the SELECTSPEED game menu */
fiifimills 12:fa654504b51f 73 bool gameOver; /*!< Boolean variable signifying whether or not the game is over */
fiifimills 12:fa654504b51f 74 float speed; /*!< The wait time used in the moveSnake function */
fiifimills 12:fa654504b51f 75
fiifimills 12:fa654504b51f 76
fiifimills 12:fa654504b51f 77 /**
fiifimills 12:fa654504b51f 78 Implements the actual game's functionality by moving the snake and checking when certain rules have been broken
fiifimills 12:fa654504b51f 79 */
fiifimills 12:fa654504b51f 80 void moveSnake();
fiifimills 12:fa654504b51f 81
fiifimills 12:fa654504b51f 82 /**
fiifimills 12:fa654504b51f 83 Moves the selection circle in the SELECTSPEED game menu either up or down depending on the direction in which the joystick is moved
fiifimills 12:fa654504b51f 84 */
fiifimills 12:fa654504b51f 85 void speedSelect();
fiifimills 12:fa654504b51f 86
fiifimills 12:fa654504b51f 87 /**
fiifimills 12:fa654504b51f 88 Vector that contains coordinate structs, represents the snake in game
fiifimills 12:fa654504b51f 89 */
fiifimills 12:fa654504b51f 90 std::vector<coordinate> Snake;
fiifimills 12:fa654504b51f 91