Snake Game

Dependencies:   N5110 mbed

Fork of ProjectSnake by William Smith

Committer:
el14ws
Date:
Thu May 05 13:37:52 2016 +0000
Revision:
2:3389133a2223
FINAL VERSION

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14ws 2:3389133a2223 1
el14ws 2:3389133a2223 2 /**@file main.h
el14ws 2:3389133a2223 3 @brief Header file containing functions prototypes,defines and global variables.
el14ws 2:3389133a2223 4 @author William J Smith
el14ws 2:3389133a2223 5 @date May 2016
el14ws 2:3389133a2223 6 */
el14ws 2:3389133a2223 7
el14ws 2:3389133a2223 8 #include "mbed.h"
el14ws 2:3389133a2223 9 #include "N5110.h"
el14ws 2:3389133a2223 10 #define DIRECTION_TOLERANCE 0.45// change this to alter tolerance of joystick direction
el14ws 2:3389133a2223 11
el14ws 2:3389133a2223 12 DigitalIn button(PTB11);/**@namespace Joy Stick Button Digital in*/
el14ws 2:3389133a2223 13 DigitalOut LED(PTC2);/**@namespace LED Digital output from mbed pin*/
el14ws 2:3389133a2223 14 AnalogIn xPot(PTB2);/**@namespace Joystick potentiometer connection*/
el14ws 2:3389133a2223 15 AnalogIn yPot(PTB3);/**@namespace Joystick potentiometer connection*/
el14ws 2:3389133a2223 16 Ticker pollJoystick;/**@namespace timer to regularly read the joystick*/
el14ws 2:3389133a2223 17 // Serial for debug
el14ws 2:3389133a2223 18 Serial serial(USBTX,USBRX);// Serial for debug
el14ws 2:3389133a2223 19
el14ws 2:3389133a2223 20 // create enumerated type (0,1,2,3 etc. for direction)
el14ws 2:3389133a2223 21 // could be extended for diagonals etc.
el14ws 2:3389133a2223 22 enum DirectionName {
el14ws 2:3389133a2223 23 UP,
el14ws 2:3389133a2223 24 DOWN,
el14ws 2:3389133a2223 25 LEFT,
el14ws 2:3389133a2223 26 RIGHT,
el14ws 2:3389133a2223 27 CENTRE,
el14ws 2:3389133a2223 28 UNKNOWN
el14ws 2:3389133a2223 29 };
el14ws 2:3389133a2223 30 typedef struct JoyStick Joystick;// struct for Joystick
el14ws 2:3389133a2223 31 struct JoyStick {
el14ws 2:3389133a2223 32 float x; /** current x value*/
el14ws 2:3389133a2223 33 float x0; /** 'centred' x value*/
el14ws 2:3389133a2223 34 float y; /** current y value*/
el14ws 2:3389133a2223 35 float y0; /** 'centred' y value*/
el14ws 2:3389133a2223 36 int button; /** button state (assume pull-down used, so 1 = pressed, 0 = unpressed)*/
el14ws 2:3389133a2223 37 DirectionName direction; /** current direction*/
el14ws 2:3389133a2223 38 };
el14ws 2:3389133a2223 39 Joystick joystick;/** create struct variable*/
el14ws 2:3389133a2223 40 int printFlag = 0;
el14ws 2:3389133a2223 41
el14ws 2:3389133a2223 42 /**@namespace VCC, SCE, RST, D/C, MOSI, SCLK, LED*/
el14ws 2:3389133a2223 43 N5110 lcd( PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3);
el14ws 2:3389133a2223 44 DigitalIn buttonA(PTB18);/**@namespace Digital input for button on PCB*/
el14ws 2:3389133a2223 45 int ni = 84; /*!<variable for matrixs of N5110*/
el14ws 2:3389133a2223 46 int x = 0;/*!<x Value of snake head */
el14ws 2:3389133a2223 47 int x1 = 0;/*!< x Value of snake Body including x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24*/
el14ws 2:3389133a2223 48 int x2 = 0;
el14ws 2:3389133a2223 49 int x3 = 0;
el14ws 2:3389133a2223 50 int x4 = 0;
el14ws 2:3389133a2223 51 int x5 = 0;
el14ws 2:3389133a2223 52 int x6 = 0;
el14ws 2:3389133a2223 53 int x7 = 0;
el14ws 2:3389133a2223 54 int x8 = 0;
el14ws 2:3389133a2223 55 int x9 = 0;
el14ws 2:3389133a2223 56 int x10 = 0;
el14ws 2:3389133a2223 57 int x11 = 0;
el14ws 2:3389133a2223 58 int x12 = 0;
el14ws 2:3389133a2223 59 int x13 = 0;
el14ws 2:3389133a2223 60 int x14 = 0;
el14ws 2:3389133a2223 61 int x15 = 0;
el14ws 2:3389133a2223 62 int x16 = 0;
el14ws 2:3389133a2223 63 int x17 = 0;
el14ws 2:3389133a2223 64 int x18 = 0;
el14ws 2:3389133a2223 65 int x19 = 0;
el14ws 2:3389133a2223 66 int x20 = 0;
el14ws 2:3389133a2223 67 int x21 = 0;
el14ws 2:3389133a2223 68 int x22 = 0;
el14ws 2:3389133a2223 69 int x23 = 0;
el14ws 2:3389133a2223 70 int x24 = 0;
el14ws 2:3389133a2223 71 int y = 0;/*!< y Value of snake Head*/
el14ws 2:3389133a2223 72 int y1 = 1;/*!< y Value of snake Body including y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 y16 y17 y18 y19 y20 y21 y22 y23 y24*/
el14ws 2:3389133a2223 73 int y2 = 2;
el14ws 2:3389133a2223 74 int y3 = 3;
el14ws 2:3389133a2223 75 int y4 = 4;
el14ws 2:3389133a2223 76 int y5 = 5;
el14ws 2:3389133a2223 77 int y6 = 6;
el14ws 2:3389133a2223 78 int y7 = 7;
el14ws 2:3389133a2223 79 int y8 = 8;
el14ws 2:3389133a2223 80 int y9 = 9;
el14ws 2:3389133a2223 81 int y10 = 10;
el14ws 2:3389133a2223 82 int y11 = 11;
el14ws 2:3389133a2223 83 int y12 = 12;
el14ws 2:3389133a2223 84 int y13 = 13;
el14ws 2:3389133a2223 85 int y14 = 14;
el14ws 2:3389133a2223 86 int y15 = 15;
el14ws 2:3389133a2223 87 int y16 = 16;
el14ws 2:3389133a2223 88 int y17 = 17;
el14ws 2:3389133a2223 89 int y18 = 18;
el14ws 2:3389133a2223 90 int y19 = 19;
el14ws 2:3389133a2223 91 int y20 = 20;
el14ws 2:3389133a2223 92 int y21 = 21;
el14ws 2:3389133a2223 93 int y22 = 22;
el14ws 2:3389133a2223 94 int y23 = 23;
el14ws 2:3389133a2223 95 int y24 = 24;
el14ws 2:3389133a2223 96 int change = 1;/*!< function to change the direction the snakes head is travelling*/
el14ws 2:3389133a2223 97 int n[84][48];/*!< Array for nokia 5110 screen*/
el14ws 2:3389133a2223 98 int array[48][84];/*!< Array for nokia 5110 screen for snake to travel in*/
el14ws 2:3389133a2223 99 int nj = 48;/*!< Variable for checkerboard function*/
el14ws 2:3389133a2223 100 int cells[84][48];/*!< Variable for checkerboard function*/
el14ws 2:3389133a2223 101 int Score = 0;/*!< Variable for the score that changed everytime food is eaten by the snake*/
el14ws 2:3389133a2223 102 int randX;/*!< x Variable that is randomly selected within the array to generate food in a random space*/
el14ws 2:3389133a2223 103 int randY;/*!< y Variable that is randomly selected within the array to generate food in a random space*/
el14ws 2:3389133a2223 104 char str[64];/*!< Character that enables you to print the score number on the screen */
el14ws 2:3389133a2223 105 int pause=0; /*!< Variable for pause function*/
el14ws 2:3389133a2223 106
el14ws 2:3389133a2223 107 void Menu(); /*!< Void for Menu function*/
el14ws 2:3389133a2223 108 void Welcomescreen(); /*!< Void for Welcome screen function which appears when device turned on*/
el14ws 2:3389133a2223 109 void clearCells(); /*!< Void to switch all pixels on the screen to OFF */
el14ws 2:3389133a2223 110 void checkerBoard(); /*!< Void to Make checker baord effect over */
el14ws 2:3389133a2223 111 void DrawArray(); /*!<Sets the pixel depending on whether 0 or higher than 1*/
el14ws 2:3389133a2223 112 void ClearArray();/*!<Checks over each individual pixel and if its 0 it will clear*/
el14ws 2:3389133a2223 113 void GenerateFood();/*!<Generates food at random places*/
el14ws 2:3389133a2223 114 void MakeSnake(); /*!<sets the pixels for the snake*/
el14ws 2:3389133a2223 115 void SnakeGame(); /*!<Game Rules*/
el14ws 2:3389133a2223 116 void MoveSnake(); /*!<moving the snake using the joy stick*/
el14ws 2:3389133a2223 117 void calibrateJoystick();/*!void to calibrate the joystick*/
el14ws 2:3389133a2223 118 void updateJoystick();/*!<Updates joystick within set amount of time*/
el14ws 2:3389133a2223 119 void SnakeGrowing();/*!<Function to make snake grow when it eats the food*/
el14ws 2:3389133a2223 120 void EndGame();/*!<Functions ends game when Snake hits itself or the wall*/
el14ws 2:3389133a2223 121 void calibrateJoystick();/*!void to calibrate the joystick*/
el14ws 2:3389133a2223 122 void updateJoystick();/*!void to calibrate the joystick*/
el14ws 2:3389133a2223 123 void GameOver();/*!void that runs Game over screen and enables user to go back to the beginning*/
el14ws 2:3389133a2223 124 void Winner();/*!void to show WINNER screen and enables user to go back to the beginning*/