submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Mon May 06 05:26:28 2019 +0000
Revision:
23:fd0339ec480c
Parent:
18:2ca7bd135b68
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 FINGER_H
694617778 18:2ca7bd135b68 2 #define FINGER_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 /** Finger class
694617778 18:2ca7bd135b68 9
694617778 18:2ca7bd135b68 10 @brief Class for the finger-guess game
694617778 18:2ca7bd135b68 11 @brief It was the game I initially wrote
694617778 18:2ca7bd135b68 12 @brief However, I thought this game was too easy and then changed to the sanke game
694617778 18:2ca7bd135b68 13 @brief But I still want to add this game for a extra scene
694617778 18:2ca7bd135b68 14
694617778 18:2ca7bd135b68 15 @author Qi Minghong
694617778 18:2ca7bd135b68 16
694617778 18:2ca7bd135b68 17 @date April 2019
694617778 18:2ca7bd135b68 18
694617778 18:2ca7bd135b68 19 @code
694617778 18:2ca7bd135b68 20 ///////// pre-processor directives ////////
694617778 18:2ca7bd135b68 21 #include "mbed.h"
694617778 18:2ca7bd135b68 22 #include "Gamepad.h"
694617778 18:2ca7bd135b68 23 #include "N5110.h"
694617778 18:2ca7bd135b68 24 #include "Snake.h"
694617778 18:2ca7bd135b68 25 #include "Engine.h"
694617778 18:2ca7bd135b68 26 #include "Finger.h"
694617778 18:2ca7bd135b68 27
694617778 18:2ca7bd135b68 28 /////////////// objects ///////////////
694617778 18:2ca7bd135b68 29 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
694617778 18:2ca7bd135b68 30 Gamepad pad;
694617778 18:2ca7bd135b68 31 Snake snake;
694617778 18:2ca7bd135b68 32 Engine engine;
694617778 18:2ca7bd135b68 33 Finger finger;
694617778 18:2ca7bd135b68 34
694617778 18:2ca7bd135b68 35 ///////////// prototypes ///////////////
694617778 18:2ca7bd135b68 36 void init();
694617778 18:2ca7bd135b68 37 void run();
694617778 18:2ca7bd135b68 38 void over();
694617778 18:2ca7bd135b68 39
694617778 18:2ca7bd135b68 40 ///////////// functions ////////////////
694617778 18:2ca7bd135b68 41 int main()
694617778 18:2ca7bd135b68 42 {
694617778 18:2ca7bd135b68 43 int fps = 8; // frames per second
694617778 18:2ca7bd135b68 44 snake.hscore = 0;
694617778 18:2ca7bd135b68 45 lcd.init();
694617778 18:2ca7bd135b68 46 while(1){
694617778 18:2ca7bd135b68 47 init();
694617778 18:2ca7bd135b68 48 pad.tone(900.0,0.5);
694617778 18:2ca7bd135b68 49 engine.welcome(pad,lcd); // show welcome display, waiting for the user to start
694617778 18:2ca7bd135b68 50 engine.menu(pad,lcd); // show the select display, waiting for the user to select
694617778 18:2ca7bd135b68 51 // choose game and game loop
694617778 18:2ca7bd135b68 52 if(engine.game == 0){
694617778 18:2ca7bd135b68 53 while (snake.over == 0) {
694617778 18:2ca7bd135b68 54 run(); // run the snake game
694617778 18:2ca7bd135b68 55 while (pad.check_event(Gamepad::START_PRESSED) == true){
694617778 18:2ca7bd135b68 56 engine.pause(pad,lcd); // pause the game
694617778 18:2ca7bd135b68 57 }
694617778 18:2ca7bd135b68 58 wait(engine.p/fps); // and wait for one frame period
694617778 18:2ca7bd135b68 59 }
694617778 18:2ca7bd135b68 60 over(); // show gameover display, waiting for the user to restart
694617778 18:2ca7bd135b68 61 }else if(engine.game == 1){
694617778 18:2ca7bd135b68 62 finger.run(lcd,pad); // run the finger-geuss game
694617778 18:2ca7bd135b68 63 }
694617778 18:2ca7bd135b68 64 }
694617778 18:2ca7bd135b68 65 }
694617778 18:2ca7bd135b68 66
694617778 18:2ca7bd135b68 67 // this function draws each frame on the LCD
694617778 18:2ca7bd135b68 68 void run()
694617778 18:2ca7bd135b68 69 {
694617778 18:2ca7bd135b68 70 // clear screen, re-draw and refresh
694617778 18:2ca7bd135b68 71 lcd.clear();
694617778 18:2ca7bd135b68 72 int length = snake.get_length();
694617778 18:2ca7bd135b68 73 int direction = engine.get_direction(pad);
694617778 18:2ca7bd135b68 74 snake.check_eat(pad);
694617778 18:2ca7bd135b68 75 snake.draw(lcd,length);
694617778 18:2ca7bd135b68 76 snake.update(direction,length);
694617778 18:2ca7bd135b68 77 snake.check_over(lcd);
694617778 18:2ca7bd135b68 78 lcd.refresh();
694617778 18:2ca7bd135b68 79 }
694617778 18:2ca7bd135b68 80
694617778 18:2ca7bd135b68 81 // initialies all classes and libraries
694617778 18:2ca7bd135b68 82 void init()
694617778 18:2ca7bd135b68 83 {
694617778 18:2ca7bd135b68 84 // need to initialise LCD and Gamepad
694617778 18:2ca7bd135b68 85 pad.init();
694617778 18:2ca7bd135b68 86 snake.init(2,3);
694617778 18:2ca7bd135b68 87 engine.init();
694617778 18:2ca7bd135b68 88 finger.init();
694617778 18:2ca7bd135b68 89 lcd.clear();
694617778 18:2ca7bd135b68 90 }
694617778 18:2ca7bd135b68 91
694617778 18:2ca7bd135b68 92 //gameover screen
694617778 18:2ca7bd135b68 93 void over()
694617778 18:2ca7bd135b68 94 {
694617778 18:2ca7bd135b68 95 int s = snake.get_score(); // get the scores to show at the gameover display
694617778 18:2ca7bd135b68 96 int hs = snake.hscore;
694617778 18:2ca7bd135b68 97 engine.gameover(pad,lcd,s,hs);
694617778 18:2ca7bd135b68 98
694617778 18:2ca7bd135b68 99 }
694617778 18:2ca7bd135b68 100 @endcode
694617778 18:2ca7bd135b68 101 */
694617778 18:2ca7bd135b68 102
694617778 18:2ca7bd135b68 103 class Finger
694617778 18:2ca7bd135b68 104 {
694617778 18:2ca7bd135b68 105
694617778 18:2ca7bd135b68 106 public:
694617778 18:2ca7bd135b68 107 /** Constructor */
694617778 18:2ca7bd135b68 108 Finger();
694617778 18:2ca7bd135b68 109 /** Destructor */
694617778 18:2ca7bd135b68 110 ~Finger();
694617778 18:2ca7bd135b68 111 /** Initialise the components of snake
694617778 18:2ca7bd135b68 112 *
694617778 18:2ca7bd135b68 113 * This function gets the length of the snake.
694617778 18:2ca7bd135b68 114 */
694617778 18:2ca7bd135b68 115 void init();
694617778 18:2ca7bd135b68 116 /** Draw the finger and scores
694617778 18:2ca7bd135b68 117 *
694617778 18:2ca7bd135b68 118 * This function draws the finger and scores.
694617778 18:2ca7bd135b68 119 * @param lcd - N5110 library
694617778 18:2ca7bd135b68 120 * @param length - the length of the snake
694617778 18:2ca7bd135b68 121 */
694617778 18:2ca7bd135b68 122 void draw(N5110 &lcd,Gamepad &pad);
694617778 18:2ca7bd135b68 123 /** Run the game
694617778 18:2ca7bd135b68 124 *
694617778 18:2ca7bd135b68 125 * This function check which wins and adds socres.
694617778 18:2ca7bd135b68 126 * @param lcd - N5110 library
694617778 18:2ca7bd135b68 127 * @param length - the length of the snake
694617778 18:2ca7bd135b68 128 */
694617778 18:2ca7bd135b68 129 void run(N5110 &lcd,Gamepad &pad);
694617778 18:2ca7bd135b68 130
694617778 18:2ca7bd135b68 131
694617778 18:2ca7bd135b68 132 private:
694617778 18:2ca7bd135b68 133 int _score1;
694617778 18:2ca7bd135b68 134 int _score2;
694617778 18:2ca7bd135b68 135 int _finger1;
694617778 18:2ca7bd135b68 136 int _finger2;
694617778 18:2ca7bd135b68 137 int c;
694617778 18:2ca7bd135b68 138
694617778 18:2ca7bd135b68 139 };
694617778 18:2ca7bd135b68 140 #endif