submit

Dependencies:   mbed Gamepad N5110

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Finger.h Source File

Finger.h

00001 #ifndef FINGER_H
00002 #define FINGER_H
00003 
00004 #include "mbed.h"
00005 #include "N5110.h"
00006 #include "Gamepad.h"
00007 
00008 /** Finger class
00009 
00010 @brief Class for the finger-guess game
00011 @brief It was the game I initially wrote
00012 @brief However, I thought this game was too easy and then changed to the sanke game
00013 @brief But I still want to add this game for a extra scene
00014 
00015 @author Qi Minghong
00016 
00017 @date April 2019
00018 
00019 @code
00020 ///////// pre-processor directives ////////
00021 #include "mbed.h"
00022 #include "Gamepad.h"
00023 #include "N5110.h"
00024 #include "Snake.h"
00025 #include "Engine.h"
00026 #include "Finger.h"
00027 
00028 /////////////// objects ///////////////
00029 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00030 Gamepad pad;
00031 Snake snake;
00032 Engine engine;
00033 Finger finger;
00034 
00035 ///////////// prototypes ///////////////
00036 void init();
00037 void run();
00038 void over();
00039 
00040 ///////////// functions ////////////////
00041 int main()
00042 {
00043     int fps = 8;  // frames per second
00044     snake.hscore = 0;
00045     lcd.init();
00046     while(1){    
00047       init();
00048       pad.tone(900.0,0.5);
00049       engine.welcome(pad,lcd);  // show welcome display, waiting for the user to start
00050       engine.menu(pad,lcd);  // show the select display, waiting for the user to select
00051     // choose game and game loop
00052     if(engine.game == 0){
00053       while (snake.over == 0) {
00054         run(); // run the snake game
00055         while (pad.check_event(Gamepad::START_PRESSED) == true){
00056             engine.pause(pad,lcd); // pause the game
00057         }
00058         wait(engine.p/fps);  // and wait for one frame period
00059       }
00060      over();  // show gameover display, waiting for the user to restart
00061      }else if(engine.game == 1){
00062          finger.run(lcd,pad); // run the finger-geuss game
00063          }
00064     }
00065 }
00066 
00067 // this function draws each frame on the LCD
00068 void run()
00069 {
00070     // clear screen, re-draw and refresh
00071     lcd.clear();
00072     int length = snake.get_length();
00073     int direction = engine.get_direction(pad);
00074     snake.check_eat(pad);
00075     snake.draw(lcd,length);
00076     snake.update(direction,length);
00077     snake.check_over(lcd);
00078     lcd.refresh();
00079 }
00080 
00081 // initialies all classes and libraries
00082 void init()
00083 {
00084     // need to initialise LCD and Gamepad
00085     pad.init();    
00086     snake.init(2,3);
00087     engine.init();
00088     finger.init();
00089     lcd.clear();
00090 }
00091 
00092 //gameover screen
00093 void over()
00094 {
00095     int s = snake.get_score(); // get the scores to show at the gameover display
00096     int hs = snake.hscore;
00097     engine.gameover(pad,lcd,s,hs);
00098 
00099 }
00100 @endcode
00101 */ 
00102 
00103 class Finger
00104 {
00105 
00106 public:
00107     /** Constructor */
00108     Finger();
00109     /** Destructor */
00110     ~Finger();
00111     /** Initialise the components of snake
00112     *
00113     *   This function gets the length of the snake.
00114     */
00115     void init();
00116     /** Draw the finger and scores
00117     *
00118     *   This function draws the finger and scores.
00119     *   @param  lcd - N5110 library
00120     *   @param  length - the length of the snake
00121     */
00122     void draw(N5110 &lcd,Gamepad &pad);
00123     /** Run the game
00124     *
00125     *   This function check which wins and adds socres.
00126     *   @param  lcd - N5110 library
00127     *   @param  length - the length of the snake
00128     */
00129     void run(N5110 &lcd,Gamepad &pad);
00130 
00131 
00132 private:
00133     int _score1;
00134     int _score2;
00135     int _finger1;
00136     int _finger2;
00137     int c;
00138 
00139 };
00140 #endif