Racing Cars game using N5110 LCD and thumb Joystick
Dependencies: N5110 PowerControl beep mbed
main.h
- Committer:
- el13gs
- Date:
- 2015-05-08
- Revision:
- 6:289f237b8d90
- Parent:
- 5:243718c3cd8b
- Child:
- 7:edf0f1fcb16b
File content as of revision 6:289f237b8d90:
/** @file main.h @brief Header File containing function prototypes,defines and global variables. @author Giorgos Savvides SID:200805533 @date May 2015 */ #include "N5110.h" #include "PowerControl.h" #include "EthernetPowerControl.h" #include "beep.h" #define DIRECTION_TOLERANCE 0.05 /** @namespace Joystick button @brief Digital Input for button status */ DigitalIn button(p17); /** @namespace Reset Button @brief Digital Input for Reset Button Status */ InterruptIn reset(p25); /** @namespace Reset Button @brief Interrupt for Start Button Status */ InterruptIn start(p24); /** @namespace AnalogIn y-position @brief AnalogIn input to read potentiometer value */ AnalogIn yPot(p18); /** @namespace AnalogIn x-position @brief AnalogIn input to read potentiometer value */ AnalogIn xPot(p19); /** @namespace Buzzer @brief Buzzer object of Beep.h class to control the buzzer component */ Beep buzzer(p21); /** @namespace LCD @brief LCD object of N5110.h class, sets the LCD pin connections */ N5110 lcd(p7,p8,p9,p10,p11,p13,p26); /** @namespace pollJoystick @brief timer that reads Joystick value every certain amount of time */ Ticker pollJoystick; /** @namespace Player Timer @brief timer that reads Player's movements every certain amount of time */ Ticker timer; /** List of Directions within the game enum class */ enum DirectionName { UP, DOWN, LEFT, RIGHT, CENTRE, UNKNOWN }; /** A structure that includes joystick properties */ typedef struct JoyStick Joystick; struct JoyStick { float x; /** current x value */ float x0; /** 'centred' x value */ float y; /** current y value */ float y0; /** 'centred' y value */ int button; /** button state (assume pull-down used, so 1 = pressed, 0 = unpressed) */ DirectionName direction; /** current direction */ }; /** create Joystick struct variable */ Joystick joystick; /**flag pointers when interrupt comes in */ int printFlag = 0; int startButtonFlag=0; int resetButtonFlag=0; int gamePlays=1; int sounds=1; int coinAppear=1; int optionsPointer=1; /**initial enemies Y positions (p,q,c) */ int p=-20; int q=-100; int c=0; int j=0; /** enemies X positions (p,q,c)*/ int enemy1x=46; int enemy2x=6; int enemy3x=26; /** initial player's Car position and its dimensions(w,h) */ int x=26; /** x-position*/ int v=30; /** y-position*/ int w=8; /** width*/ int h=12; /** height*/ /**initial Coin Xposition */ int xPos=6; /** initial Game States, Number of lives, Number of coins, Round Number */ int lives=5; int coins=0; int round=1; /** brightness Initial Values, brightnessDisplay is the value displayed to the user */ float brightness=0.9; int brightnessDisplay=9; /** table array that will store cell conditions (either 0,1,2) */ int table[84][48]; /** enemies acceleration initial value */ int a=4; /* FUNCTION DECLERATIONS */ /** Initialise GameScreen * Draws the lanes of the road, that are fixed. * This function is called when the player moves on the screen and pixels need to be reset */ void gameInitial(); /** Initialise Table Array * It initialises the array used in the game by setting all the array cells value equal to 0 */ void initTable(); /** Calibrate Joystick Function * read default positions of the joystick to calibrate later readings */ void calibrateJoystick(); /** Update Joystick Function * read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) */ void updateJoystick(); /** Clear Rectangle Function * This function clears the rectangle * @param x - x-coordinate of top left point * @param v - y-coordinate of top left point * @param w - width of the Rectangle * @param h - height of the Rectangle * */ void clearRect(int x,int v,int w,int h); //clear position /** MovePlayer Function * This function is being called every 0.1 sec, checks the direction of the joystick * and moves the Player by clearing and drawing Rectangle */ void movePlayer(); /** Loose Function * This function is being called when the player loose (i.e touches an enemy) */ void loose(); /** Check Player Function * Function can be used to check if the Player touched an enemy * or if he has touched a coin. Depending on the situation calls the * appropriate function and implements some certain tasks */ void checkPlayerPos();//Constantly checking player's position /** Enemy Move Functions * 3 Enemy Functions, each one is responsible to move the Enemy downwards * The functions use an integer that defines the y position of the enemy * As this integer is increasing, rectangle is being drawn and cleared continuously * This integer variable restarts from a random value after reaching a specific point */ void enemy1MovesDown(); void enemy2MovesDown(); void enemy3MovesDown(); /** Coin Moves Function * Function that causes a coin to move downwards * It calls the drawCoin and clearCoin functions so the coin is continuously moving down when it appears */ void coinMoves(); /** Draw Coin Function * Function that draws a coin on the screen * This is a circle with a 'c' symbol at the centre of the circle * It calls the drawCircle function and setPixel function to draw the coin * It also sets the array cells value equal to 2, indicating there is a coin * @param c - the y coordinate of the centre of the coin */ void drawCoin(int c); /** Clear Coin Function * Function that clears the coin that was drawn previously * This is a circle with a 'c' symbol at the centre of the circle * It calls the clearCircle function and clearPixel function to clear the coin * It also clears the array cells (set their values to 0), indicating there is nothing in these cells * @param c - the y coordinate of the centre of the coin */ void clearCoin(int c); /** Set Circle Cells Function * Function is responsible to set the values of the Table Array cells within a Circle range equal to 2 * This is done to identify where the coin is at a certain time * @param x0-the x coordinate of the Circle's centre * @param y0-the y coordinate of the Circle's centre * @param radius-radius of the circle */ void setCircleCells(int x0,int y0,int radius); //Set selected Circle cells to 1 /** Clear Circle Cells Function * Function that clears the values of the Table Array cells within a Circle (set them equal to 0) * @param x0-the x coordinate of the Circle's centre * @param y0-the y coordinate of the Circle's centre * @param radius-radius of the circle */ void clearCircleCells(int x0,int y0,int radius);//Set selected Circle cells to 0 /** Set Rectangle Cells Function * Function that cells the values of the Table Array cells within a Rectanlge Shape equal to 1 * This is done for all enemies moving downwards, thus we can check where the enemies are located * @param x-the x-direction of the Rectangle's top left corner * @param v-the y-direction of the Rectangle's top right corner * @param w-the width of the Rectangle * @param h-the height of the Rectangle */ void setRectCells(int x,int v,int w,int h); //set Cells range to 1 /** Clear Cells Function * Clears the pixel and sets the array table cells value equal to 0 , from 0 to x and from 0 to y * @param x-x coordinate of the array to clear * @param y-y coordinate of the array to clear */ void clearCells(int x,int y); //set Cells range to 0 /** Start Button Pressed Function * Flips the Start Button Flag indicator showing that button was pressed */ void startButtonPressed();//Function that checks for an Interupt at pin 24 void resetButtonPressed();//Function that checks for an Interupt at pin 25 void setLineCells(int x0,int y0,int x1,int y1,int type); void clearLineCells(int x0,int y0,int x1,int y1,int type); extern "C" void mbed_reset(); void gameInitialise();