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@16:68b9460d4c76, 2016-05-04 (annotated)
- Committer:
- meurigp
- Date:
- Wed May 04 15:16:12 2016 +0000
- Revision:
- 16:68b9460d4c76
- Parent:
- 15:a5590211888c
- Child:
- 17:4e6f0f7f22fb
fsm working, need to initialise game accordingly
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 | 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 | 16:68b9460d4c76 | 30 | InterruptIn RB(PTE24); |
meurigp | 16:68b9460d4c76 | 31 | InterruptIn LB(PTE25); |
meurigp | 11:f8478bc749e0 | 32 | |
meurigp | 11:f8478bc749e0 | 33 | /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading)) |
meurigp | 11:f8478bc749e0 | 34 | enum CurrentDirection { |
meurigp | 11:f8478bc749e0 | 35 | up, |
meurigp | 11:f8478bc749e0 | 36 | down, |
meurigp | 11:f8478bc749e0 | 37 | left, |
meurigp | 11:f8478bc749e0 | 38 | right, |
meurigp | 11:f8478bc749e0 | 39 | centre, |
meurigp | 11:f8478bc749e0 | 40 | }; |
meurigp | 11:f8478bc749e0 | 41 | CurrentDirection currentDirection = centre; /// intialise direction at beginning |
meurigp | 11:f8478bc749e0 | 42 | |
meurigp | 14:56e355c5cfc9 | 43 | /// create enumerated type (0,1,2 etc. for different game modes on the menu) |
meurigp | 14:56e355c5cfc9 | 44 | enum GameType { |
meurigp | 16:68b9460d4c76 | 45 | classicMode, |
meurigp | 16:68b9460d4c76 | 46 | infiniteMode, |
meurigp | 16:68b9460d4c76 | 47 | hardMode, |
meurigp | 14:56e355c5cfc9 | 48 | }; |
meurigp | 16:68b9460d4c76 | 49 | GameType gameType = classicMode; |
meurigp | 14:56e355c5cfc9 | 50 | |
meurigp | 16:68b9460d4c76 | 51 | volatile int game_timer_flag = 0; /*!< flag for game timer isr */ |
meurigp | 16:68b9460d4c76 | 52 | volatile int rb_flag = 0; /*!< flag for right button isr */ |
meurigp | 16:68b9460d4c76 | 53 | volatile int lb_flag = 0; /*!< flag for left button isr */ |
meurigp | 11:f8478bc749e0 | 54 | int randomX = rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */ |
meurigp | 11:f8478bc749e0 | 55 | int randomY = rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */ |
meurigp | 11:f8478bc749e0 | 56 | int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */ |
meurigp | 11:f8478bc749e0 | 57 | int randomYoddEven = randomY%2; /*!< distinguish whether randomY is odd or even */ |
meurigp | 11:f8478bc749e0 | 58 | int snakeTailX[100]; /*!< array for X coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 59 | int snakeTailY[100]; /*!< array for Y coordinate of each snake segment */ |
meurigp | 11:f8478bc749e0 | 60 | int snakeTailLength = 3; /*!< length of snake, intialised to 3 */ |
meurigp | 11:f8478bc749e0 | 61 | int score = 0; /*!< score for current round */ |
meurigp | 11:f8478bc749e0 | 62 | int top_score = 0; /*!< top score read and write from the SD card */ |
meurigp | 11:f8478bc749e0 | 63 | int fruitValue = 10; /*!< value of the fruit */ |
meurigp | 14:56e355c5cfc9 | 64 | int i = 40; /*!< x origin of snake head, intialised at 41 */ |
meurigp | 14:56e355c5cfc9 | 65 | int j = 22; /*!< y origin of snake head, intialised at 23 */ |
meurigp | 11:f8478bc749e0 | 66 | int prev_i; /*!< integer to store previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 67 | int prev_j; /*!< integer to store previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 68 | int prev2_i; /*!< integer to store previous, previous value of x/i */ |
meurigp | 11:f8478bc749e0 | 69 | int prev2_j; /*!< integer to store previous, previous value of y/j */ |
meurigp | 11:f8478bc749e0 | 70 | bool gamePlaying = false; /*!< bool to store whether the game is in play or not */ |
meurigp | 12:825a402d230f | 71 | int pauseCount; /*!< counts how many times the player has paused */ |
meurigp | 11:f8478bc749e0 | 72 | |
meurigp | 11:f8478bc749e0 | 73 | |
meurigp | 11:f8478bc749e0 | 74 | /** |
meurigp | 11:f8478bc749e0 | 75 | Displays new fruit |
meurigp | 11:f8478bc749e0 | 76 | */ |
meurigp | 11:f8478bc749e0 | 77 | void generateFood(); |
meurigp | 11:f8478bc749e0 | 78 | /** |
meurigp | 11:f8478bc749e0 | 79 | Generates new random coordinates for the fruit |
meurigp | 11:f8478bc749e0 | 80 | */ |
meurigp | 11:f8478bc749e0 | 81 | void newFruitXY(); |
meurigp | 11:f8478bc749e0 | 82 | /** |
meurigp | 13:08159ea3d556 | 83 | Receives input form joystick and updates the current direction |
meurigp | 11:f8478bc749e0 | 84 | */ |
meurigp | 11:f8478bc749e0 | 85 | void moveSnake(); |
meurigp | 11:f8478bc749e0 | 86 | /** |
meurigp | 11:f8478bc749e0 | 87 | Restricted boundaries |
meurigp | 11:f8478bc749e0 | 88 | */ |
meurigp | 11:f8478bc749e0 | 89 | void hardWall(); |
meurigp | 11:f8478bc749e0 | 90 | /** |
meurigp | 11:f8478bc749e0 | 91 | Map with obstacles in the way |
meurigp | 11:f8478bc749e0 | 92 | */ |
meurigp | 11:f8478bc749e0 | 93 | void specialMap(); |
meurigp | 11:f8478bc749e0 | 94 | /** |
meurigp | 11:f8478bc749e0 | 95 | Infinite boundaries |
meurigp | 11:f8478bc749e0 | 96 | */ |
meurigp | 11:f8478bc749e0 | 97 | void wrapAround(); |
meurigp | 11:f8478bc749e0 | 98 | /** |
meurigp | 11:f8478bc749e0 | 99 | Calculates score |
meurigp | 11:f8478bc749e0 | 100 | */ |
meurigp | 11:f8478bc749e0 | 101 | void scoreCalculation(); |
meurigp | 11:f8478bc749e0 | 102 | /** |
meurigp | 11:f8478bc749e0 | 103 | Displayes scores when player dies |
meurigp | 11:f8478bc749e0 | 104 | */ |
meurigp | 11:f8478bc749e0 | 105 | void gameOver(); |
meurigp | 11:f8478bc749e0 | 106 | /** |
meurigp | 11:f8478bc749e0 | 107 | Initilaises the snake so it's moving from left to right to begin with |
meurigp | 11:f8478bc749e0 | 108 | */ |
meurigp | 11:f8478bc749e0 | 109 | void initSnakeTail(); |
meurigp | 11:f8478bc749e0 | 110 | /** |
meurigp | 11:f8478bc749e0 | 111 | Splash screen for the intro of the game |
meurigp | 11:f8478bc749e0 | 112 | */ |
meurigp | 11:f8478bc749e0 | 113 | void snakeIntro(); |
meurigp | 12:825a402d230f | 114 | /** |
meurigp | 12:825a402d230f | 115 | Game paused function |
meurigp | 12:825a402d230f | 116 | */ |
meurigp | 12:825a402d230f | 117 | void gamePaused(); |
meurigp | 13:08159ea3d556 | 118 | /** |
meurigp | 13:08159ea3d556 | 119 | Contains all logic for the snake game and displays accordingly |
meurigp | 13:08159ea3d556 | 120 | */ |
meurigp | 13:08159ea3d556 | 121 | void gameLogic(); |
meurigp | 11:f8478bc749e0 | 122 | |
meurigp | 11:f8478bc749e0 | 123 | |
meurigp | 11:f8478bc749e0 | 124 | #endif |