jordan harper
/
GameProject
main.cpp
- Committer:
- jordaahh
- Date:
- 2015-04-29
- Revision:
- 14:c00b7a943fb5
- Parent:
- 13:677d398423f7
- Child:
- 15:cf59cdffe546
File content as of revision 14:c00b7a943fb5:
/* Joystick Example code of how to read a joystick https://www.sparkfun.com/products/9032 Craig A. Evans 7 March 2015 */ #include "mbed.h" #include "N5110.h" // change this to alter tolerance of joystick direction #define DIRECTION_TOLERANCE 0.1 // connections for joystick DigitalIn button(p17); AnalogIn xPot(p15); AnalogIn yPot(p16); // LCD connections //vcc, sce, rst, dc, mosi, clk, led N5110 lcd(p7,p8,p9,p10,p11,p13,p26); // LCD dimentions int nx= 84; //screen is 84 pixels across int ny= 48; // by 48 pixels downwards int x = 0; // wall starts from the coordinates (0,0) int y = 0; int z = 30; int i=24; // initial coordinates for player object int j=42; // timer to regularly read the joystick Ticker pollJoystick; // Serial for debug Serial serial(USBTX,USBRX); // create enumerated type (0,1,2,3 etc. for direction) // could be extended for diagonals etc. enum DirectionName { UP, DOWN, LEFT, upLeft, downLeft, RIGHT, upRight, downRight, CENTRE, UNKNOWN }; // struct for Joystick 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 struct variable Joystick joystick; int printFlag = 0; // timer function for moving walls Ticker wallMovement; Ticker wallMovement2; Ticker wallMovement3; int FLAG = 0; // initial value for flags that are used for wall movements int FLAG2 = 0; int FLAG3 = 0; // timer function for menu scroll Ticker menuMovement; int menuFlag = 0; //initial value for flag that is used for menu scrolling int c = 0; // initial value for position of option in menu LocalFileSystem local("local"); // create local filesystem // functions for TimeAndDate void serialISR(); // ISR that is called when serial data is received void setTime(); // function to set the UNIX time int setTimeFlag = 0; // flag for ISR char rxString[16]; // buffer to store received string // Output to Power LED PwmOut power(p24); // Output to Buzzer PwmOut buz(p21); // initial case for brightness function int bright = 0; // initial cas for volume function int vol = 0; // initial value for u (used in songs) int u = 0; //frequency array for song float frequency[]= {880,880,0,0,880,880,0,880,880,0,1047,1047,0,0,440,440,0,0,440,440,0,440,440,0,587,587,0,0,0,0,0}; float frequency2[]= {600,600,400,400,200,0}; // function prototypes void calibrateJoystick(); void updateJoystick(); void clearCells(); void fallingWall(); void flagForWall(); void flagForWall2(); void flagForWall3(); void menu(); void playGame(); void TimeAndDate(); void writeDataToFile(int data); void BandVMenu(); void brightness(); void volume(); void tone(); void deadTone(); int main() { // set_time(0); // enter unix time then delete from main function to set time power = 1; lcd.init(); calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/15.0); // read joystick 10 times per second wallMovement.attach(&flagForWall,0.2); // call function flagForWall to make flag=1, every 0.1 seconds wallMovement2.attach(&flagForWall2,0.2); // call function flagForWall2 to make flag=1, every 0.1 seconds wallMovement3.attach(&flagForWall3,0.2); // call function flagForWall3 to make flag=1, every 0.1 seconds menu(); } // read default positions of the joystick to calibrate later readings void calibrateJoystick() { button.mode(PullDown); // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } void updateJoystick() { // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state joystick.button = button; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = CENTRE; } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = upRight; } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = downRight; } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = upLeft; } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = downLeft; } // set flag for printing printFlag = 1; } void clearCells() // turns all cells on the display off { // loop through cells, and clear for (int i = 0; i < nx ; i++) { for (int j = 0; j < ny ; j++) { lcd.clearPixel(i,j); } } lcd.refresh(); // must refresh to write buffer display } void flagForWall() // flag function for wmoving wall to call later { FLAG=1; } void flagForWall2() // flag function for wmoving wall to call later { FLAG2=1; } void flagForWall3() // flag function for moving wall to call later { FLAG3=1; } void menu () { int m = 0; //serial.attach(&serialISR); // attach serial ISR char buffer[14]; // buffer used to store time string char buffer2[14]; // buffer used to store time string while(1) { clearCells(); time_t seconds = time(NULL); // get current time // format time into a string (time and date) strftime(buffer, 14 , "%H:%M", localtime(&seconds)); strftime(buffer2, 14, "%d/%m", localtime(&seconds)); lcd.printString(buffer,26,4); // show time on lcd screen if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m++; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m--; } if(m > 2) { m = 0; } if(m < 0) { m = 2; } switch (m) { case 0 : lcd.printString("< Play Game >",0,2); wait(0.3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("< Settings >",0,2); wait(0.3); while(1) { if(button == 1) { clearCells(); BandVMenu(); break; } else { break; } } break; case 2 : lcd.printString("< HighScores >",0,2); wait(0.3); while(1) { if(button == 1) { clearCells(); lcd.printString("scoring",0,2); //enter function to commence here wait(3.0); break; } else { break; } } break; } } } void playGame() { int i=24; // initial coordinates for player object int j=42; int x = 0; // wall starts from the coordinates (0,0) int y = 0; int z = 30; int score = 0; int a = 0; int b = 47; int d = 30; int f = 0; int g = 0; int h = 24; int q = 83; int w = 0; int e = 24; lcd.printString("GO!",35,2); wait(1); while(1) { tone(); lcd.drawRect(i,j,2,2,0); clearCells(); lcd.drawLine(x,y,z,y,1); // draws a line at the top of the screen to symbolise a wall lcd.drawLine(z+10,y,83,y,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it if(FLAG==1) { FLAG=0; y++; // everytime the flag changes to one it is reset and the wall moves down by one pixel } if(y > 47) { // when the wall reaches the bottom a new wall is generated at the top and with another 10 pixel gap in a different place y = 0; z = rand()%74; score++; } if(( score > 1)&&(score < 5)) { lcd.drawLine(a,b,d,b,1); // draws a line at the top of the screen to symbolise a wall lcd.drawLine(d+10,b,83,b,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it if(FLAG3==1) { FLAG3=0; b--; // everytime the flag changes to one it is reset and the wall moves down by one pixel } } if(b < 0) { // when the wall reaches the bottom a new wall is generated at the top and with another 10 pixel gap in a different place b = 47; d = rand()%74; } if(( score > 5)&&(score < 9)) { lcd.drawLine(f,g,f,h,1); lcd.drawLine(f,h+10,f,47,1); if(FLAG2==1) { FLAG2=0; f++; // everytime the flag changes to one it is reset and the wall moves down by one pixel } } if( f > 83) { // when the wall reaches the bottom a new wall is generated at the top and with another 10 pixel gap in a different place f = 0; h = rand()%38; } if(( score > 10)&&(score < 15)) { lcd.drawLine(q,w,q,e,1); // draws a line at the top of the screen to symbolise a wall lcd.drawLine(q,e+10,q,47,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it if(FLAG3==1) { FLAG3=0; q--; // everytime the flag changes to one it is reset and the wall moves down by one pixel } } if(q < 0) { // when the wall reaches the bottom a new wall is generated at the top and with another 10 pixel gap in a different place q = 83; e = rand()%38; } if( score > 15) { lcd.drawLine(q,w,q,e,1); // draws a line at the top of the screen to symbolise a wall lcd.drawLine(q,e+10,q,47,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it if(FLAG3==1) { FLAG3=0; q--; // everytime the flag changes to one it is reset and the wall moves down by one pixel } } if( score > 20) { lcd.drawLine(a,b,d,b,1); // draws a line at the top of the screen to symbolise a wall lcd.drawLine(d+10,b,83,b,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it if(FLAG2==1) { FLAG2=0; b--; // everytime the flag changes to one it is reset and the wall moves down by one pixel } } if( j > 45) { // rules to prevent player object going off the screen j = 45; } if( i > 81) { i = 81; } if( j < 0) { j = 0; } if( i < 0) { i = 0; } if (printFlag) { // if flag set, clear flag and implicate joystick functions printFlag = 0; // check joystick direction if (joystick.direction == UP) { j--; } if (joystick.direction == DOWN) { j++; } if (joystick.direction == LEFT) { i++; } if (joystick.direction == RIGHT) { i--; } if (joystick.direction == upRight) { j--; i--; } if (joystick.direction == downRight) { j++; i--; } if (joystick.direction == upLeft) { j--; i++; } if (joystick.direction == downLeft) { j++; i++; } if (joystick.direction == CENTRE) { i=i; j=j; } if (lcd.getPixel(i,j-1)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } if (lcd.getPixel(i+1,j-1)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } if (lcd.getPixel(i+2,j-1)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } if (lcd.getPixel(i,j)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } if (lcd.getPixel(i+1,j)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } if (lcd.getPixel(i+2,j)) { writeDataToFile(score); // write current value to disk clearCells(); int m = 1; lcd.printString("game over",1,2); deadTone(); clearCells(); lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); while(1) { clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { m--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { m++; } if(m > 2) { m = 2; } if(m < 0) { m = 0; } switch (m) { case 0 : lcd.printString("YES",0,3); while(1) { if(button == 1) { clearCells(); playGame(); break; } else { break; } } break; case 1 : lcd.printString("Play again?",0,0); lcd.printString("Yes No",0,3); break; case 2 : lcd.printString("NO",70,3); while(1) { if(button == 1) { clearCells(); lcd.printString("Leaving Game",0,2); //enter function to commence here wait(1.0); menu(); break; } else { break; } } break; } } } } } } // Brightness and volume menu function void BandVMenu() { int t = 0; while(1) { wait(0.2); clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { t++; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { t--; } if(t > 1) { t = 0; } if(t < 0) { t = 1; } if(joystick.direction == DOWN) { clearCells(); lcd.printString("Returning",18,2); lcd.printString("to Menu",24,3); wait(0.5); menu(); } switch (t) { case 0 : lcd.printString("< Brightness >",0,2); while(1) { if(button == 1) { brightness(); break; } else { break; } } break; case 1 : lcd.printString("< Volume >",0,2); while(1) { if(button == 1) { volume(); break; } else { break; } } break; } } } void brightness() { while(1) { wait(0.2); clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { bright--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { bright++; } if(bright > 3) { bright = 3; } if(bright < -3) { bright = -3; } switch (bright) { case 0 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||||||",0,4); lcd.setBrightness(0.6); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 1 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||||||||",0,4); lcd.setBrightness(0.8); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 2 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||||||||||",0,4); lcd.setBrightness(0.9); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 3 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||||||||||||",0,4); lcd.setBrightness(1); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -1 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||||",0,4); lcd.setBrightness(0.4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -2 : lcd.printString(" Brightness ",0,2); lcd.printString(" ||",0,4); lcd.setBrightness(0.2); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -3 : lcd.printString(" Brightness ",0,2); lcd.setBrightness(0); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; } } } void volume() { while(1) { wait(0.2); clearCells(); if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { vol--; } if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { vol++; } if(vol > 3) { vol = 3; } if(vol < -3) { vol = -3; } switch (vol) { case 0 : lcd.printString(" Volume ",0,2); lcd.printString(" ||||||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 1 : lcd.printString(" Volume ",0,2); lcd.printString(" ||||||||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 2 : lcd.printString(" Volume ",0,2); lcd.printString(" ||||||||||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case 3 : lcd.printString(" Volume ",0,2); lcd.printString(" ||||||||||||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -1 : lcd.printString(" Volume ",0,2); lcd.printString(" ||||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -2 : lcd.printString(" Volume ",0,2); lcd.printString(" ||",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; case -3 : lcd.printString(" Volume ",0,2); lcd.printString(" MUTE",0,4); while(1) { if(button == 1) { BandVMenu(); break; } else { break; } } break; } } } void writeDataToFile(int data) { FILE *fp = fopen("/local/score.txt", "a"); // open 'score.txt' for appending // if the file doesn't exist it is created, if it exists, data is appended to the end fprintf(fp,"%i\n",data); // print string to file fclose(fp); // close file } void tone() { if(FLAG3==1) { FLAG3 = 0; buz.period(1/(frequency[u])); // set PWM period buz=0.7; u++; } if( u > 30) { u = 0; } } void deadTone() { int l = 0; while(1) { if(FLAG3==1) { FLAG3 = 0; buz.period(1/(frequency2[l])); // set PWM period buz=0.7; l++; } if( l > 6) { break; } } }