submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Mon May 06 05:26:28 2019 +0000
Revision:
23:fd0339ec480c
Parent:
19:035c85f95681
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
694617778 18:2ca7bd135b68 1 #ifndef SNAKE_H
694617778 18:2ca7bd135b68 2 #define SNAKE_H
694617778 18:2ca7bd135b68 3
694617778 18:2ca7bd135b68 4 #include "mbed.h"
694617778 18:2ca7bd135b68 5 #include "N5110.h"
694617778 18:2ca7bd135b68 6 #include "Gamepad.h"
694617778 18:2ca7bd135b68 7
694617778 18:2ca7bd135b68 8 /** Snake class
694617778 18:2ca7bd135b68 9
694617778 18:2ca7bd135b68 10 @brief Class for the sanke game
694617778 18:2ca7bd135b68 11 @brief It includes the sanke,ball,socres and collision_check
694617778 18:2ca7bd135b68 12
694617778 18:2ca7bd135b68 13 @author Qi Minghong
694617778 18:2ca7bd135b68 14
694617778 18:2ca7bd135b68 15 @date April 2019
694617778 18:2ca7bd135b68 16
694617778 18:2ca7bd135b68 17 @code
694617778 18:2ca7bd135b68 18 ///////// pre-processor directives ////////
694617778 18:2ca7bd135b68 19 #include "mbed.h"
694617778 18:2ca7bd135b68 20 #include "Gamepad.h"
694617778 18:2ca7bd135b68 21 #include "N5110.h"
694617778 18:2ca7bd135b68 22 #include "Snake.h"
694617778 18:2ca7bd135b68 23 #include "Engine.h"
694617778 18:2ca7bd135b68 24 #include "Finger.h"
694617778 18:2ca7bd135b68 25
694617778 18:2ca7bd135b68 26 /////////////// objects ///////////////
694617778 18:2ca7bd135b68 27 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
694617778 18:2ca7bd135b68 28 Gamepad pad;
694617778 18:2ca7bd135b68 29 Snake snake;
694617778 18:2ca7bd135b68 30 Engine engine;
694617778 18:2ca7bd135b68 31 Finger finger;
694617778 18:2ca7bd135b68 32
694617778 18:2ca7bd135b68 33 ///////////// prototypes ///////////////
694617778 18:2ca7bd135b68 34 void init();
694617778 18:2ca7bd135b68 35 void run();
694617778 18:2ca7bd135b68 36 void over();
694617778 18:2ca7bd135b68 37
694617778 18:2ca7bd135b68 38 ///////////// functions ////////////////
694617778 18:2ca7bd135b68 39 int main()
694617778 18:2ca7bd135b68 40 {
694617778 18:2ca7bd135b68 41 int fps = 8; // frames per second
694617778 18:2ca7bd135b68 42 snake.hscore = 0;
694617778 18:2ca7bd135b68 43 lcd.init();
694617778 18:2ca7bd135b68 44 while(1){
694617778 18:2ca7bd135b68 45 init();
694617778 18:2ca7bd135b68 46 pad.tone(900.0,0.5);
694617778 18:2ca7bd135b68 47 engine.welcome(pad,lcd); // show welcome display, waiting for the user to start
694617778 18:2ca7bd135b68 48 engine.menu(pad,lcd); // show the select display, waiting for the user to select
694617778 18:2ca7bd135b68 49 // choose game and game loop
694617778 18:2ca7bd135b68 50 if(engine.game == 0){
694617778 18:2ca7bd135b68 51 while (snake.over == 0) {
694617778 18:2ca7bd135b68 52 run(); // run the snake game
694617778 18:2ca7bd135b68 53 while (pad.check_event(Gamepad::START_PRESSED) == true){
694617778 18:2ca7bd135b68 54 engine.pause(pad,lcd); // pause the game
694617778 18:2ca7bd135b68 55 }
694617778 18:2ca7bd135b68 56 wait(engine.p/fps); // and wait for one frame period
694617778 18:2ca7bd135b68 57 }
694617778 18:2ca7bd135b68 58 over(); // show gameover display, waiting for the user to restart
694617778 18:2ca7bd135b68 59 }else if(engine.game == 1){
694617778 18:2ca7bd135b68 60 finger.run(lcd,pad); // run the finger-geuss game
694617778 18:2ca7bd135b68 61 }
694617778 18:2ca7bd135b68 62 }
694617778 18:2ca7bd135b68 63 }
694617778 18:2ca7bd135b68 64
694617778 18:2ca7bd135b68 65 // this function draws each frame on the LCD
694617778 18:2ca7bd135b68 66 void run()
694617778 18:2ca7bd135b68 67 {
694617778 18:2ca7bd135b68 68 // clear screen, re-draw and refresh
694617778 18:2ca7bd135b68 69 lcd.clear();
694617778 18:2ca7bd135b68 70 int length = snake.get_length();
694617778 18:2ca7bd135b68 71 int direction = engine.get_direction(pad);
694617778 18:2ca7bd135b68 72 snake.check_eat(pad);
694617778 18:2ca7bd135b68 73 snake.draw(lcd,length);
694617778 18:2ca7bd135b68 74 snake.update(direction,length);
694617778 18:2ca7bd135b68 75 snake.check_over(lcd);
694617778 18:2ca7bd135b68 76 lcd.refresh();
694617778 18:2ca7bd135b68 77 }
694617778 18:2ca7bd135b68 78
694617778 18:2ca7bd135b68 79 // initialies all classes and libraries
694617778 18:2ca7bd135b68 80 void init()
694617778 18:2ca7bd135b68 81 {
694617778 18:2ca7bd135b68 82 // need to initialise LCD and Gamepad
694617778 18:2ca7bd135b68 83 pad.init();
694617778 18:2ca7bd135b68 84 snake.init(2,3);
694617778 18:2ca7bd135b68 85 engine.init();
694617778 18:2ca7bd135b68 86 finger.init();
694617778 18:2ca7bd135b68 87 lcd.clear();
694617778 18:2ca7bd135b68 88 }
694617778 18:2ca7bd135b68 89
694617778 18:2ca7bd135b68 90 //gameover screen
694617778 18:2ca7bd135b68 91 void over()
694617778 18:2ca7bd135b68 92 {
694617778 18:2ca7bd135b68 93 int s = snake.get_score(); // get the scores to show at the gameover display
694617778 18:2ca7bd135b68 94 int hs = snake.hscore;
694617778 18:2ca7bd135b68 95 engine.gameover(pad,lcd,s,hs);
694617778 18:2ca7bd135b68 96
694617778 18:2ca7bd135b68 97 }
694617778 18:2ca7bd135b68 98 @endcode
694617778 18:2ca7bd135b68 99 */
694617778 18:2ca7bd135b68 100
694617778 18:2ca7bd135b68 101 class Snake
694617778 18:2ca7bd135b68 102 {
694617778 18:2ca7bd135b68 103
694617778 18:2ca7bd135b68 104 public:
694617778 18:2ca7bd135b68 105 /** Constructor */
694617778 18:2ca7bd135b68 106 Snake();
694617778 18:2ca7bd135b68 107 /** Destructor */
694617778 18:2ca7bd135b68 108 ~Snake();
694617778 18:2ca7bd135b68 109 /** Get the score
694617778 18:2ca7bd135b68 110 *
694617778 18:2ca7bd135b68 111 * This function gets the score of user.
694617778 18:2ca7bd135b68 112 */
694617778 18:2ca7bd135b68 113 int get_score();
694617778 18:2ca7bd135b68 114 /** Get the length
694617778 18:2ca7bd135b68 115 *
694617778 18:2ca7bd135b68 116 * This function gets the length of the sanke.
694617778 18:2ca7bd135b68 117 */
694617778 18:2ca7bd135b68 118 int get_length();
694617778 18:2ca7bd135b68 119 int over;
694617778 18:2ca7bd135b68 120 int hscore;
694617778 18:2ca7bd135b68 121 /** Initialise the components of snake
694617778 18:2ca7bd135b68 122 *
694617778 18:2ca7bd135b68 123 * This function innitialises the the components of snake.
694617778 18:2ca7bd135b68 124 * @param size - the size of the snake
694617778 18:2ca7bd135b68 125 * @param length - the length of the snake
694617778 18:2ca7bd135b68 126 */
694617778 18:2ca7bd135b68 127 void init(int size,int length);
694617778 18:2ca7bd135b68 128 /** Update the data
694617778 18:2ca7bd135b68 129 *
694617778 18:2ca7bd135b68 130 * This function gets the new position of the snake.
694617778 19:035c85f95681 131 * @param direction - the direction of the snake
694617778 18:2ca7bd135b68 132 * @param length - the length of the snake
694617778 18:2ca7bd135b68 133 */
694617778 18:2ca7bd135b68 134 void update(int direction, int length);
694617778 18:2ca7bd135b68 135 /** Draw the snake and ball
694617778 18:2ca7bd135b68 136 *
694617778 18:2ca7bd135b68 137 * This function draws the snake and ball.
694617778 18:2ca7bd135b68 138 * @param lcd - N5110 library
694617778 18:2ca7bd135b68 139 * @param length - the length of the snake
694617778 18:2ca7bd135b68 140 */
694617778 18:2ca7bd135b68 141 void draw(N5110 &lcd, int length);
694617778 18:2ca7bd135b68 142 /** Check eat
694617778 18:2ca7bd135b68 143 *
694617778 18:2ca7bd135b68 144 * This function checks whether the snake touthes the ball.
694617778 18:2ca7bd135b68 145 * @param pad - Gamepad library
694617778 18:2ca7bd135b68 146 */
694617778 18:2ca7bd135b68 147 void check_eat(Gamepad &pad);
694617778 18:2ca7bd135b68 148 /** Check over
694617778 18:2ca7bd135b68 149 *
694617778 18:2ca7bd135b68 150 * This function checks whether the snake runs into the wall or itself.
694617778 18:2ca7bd135b68 151 * @param lcd - N5110 library
694617778 18:2ca7bd135b68 152 */
694617778 18:2ca7bd135b68 153 void check_over(N5110 &lcd);
694617778 18:2ca7bd135b68 154
694617778 18:2ca7bd135b68 155 private:
694617778 18:2ca7bd135b68 156 int _size;
694617778 18:2ca7bd135b68 157 int _x [100];
694617778 18:2ca7bd135b68 158 int _y [100];
694617778 18:2ca7bd135b68 159 int _score;
694617778 18:2ca7bd135b68 160 int _length;
694617778 18:2ca7bd135b68 161 int ball_x;
694617778 18:2ca7bd135b68 162 int ball_y;
694617778 18:2ca7bd135b68 163 void ball(int length);
694617778 18:2ca7bd135b68 164
694617778 18:2ca7bd135b68 165 };
694617778 18:2ca7bd135b68 166 #endif