Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- el14rd
- Date:
- 2015-05-02
- Revision:
- 5:d8a06e7c54fb
- Parent:
- 4:41acda9d68c7
- Child:
- 6:290173d4a22d
File content as of revision 5:d8a06e7c54fb:
#include "mbed.h" #include "N5110.h" #define DIRECTION_TOLERANCE 0.05 //pins in N5110 lcd(p7,p8,p9,p10,p11,p13,p26); // connections for joystick DigitalIn button(p17); //DigitalIn button1(p19); AnalogIn xPot(p15); AnalogIn yPot(p16); PwmOut buzzer(p21); PwmOut blacklight(p26); PwmOut led1(p24); PwmOut led2(p23); int i; int j; int X; int Y; char buffer[14]; int snakeX[100]; int snakeY[100]; // 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, RIGHT, 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 buttonFlag = 0; int printFlag = 0; int nutFlag =1; int lifeFlag =0; float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440}; float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //int buttonFlag = 0; // function prototypes //void setPixel(int x, int y); //void clearPixel(int x, int y); typedef struct Nut Nut; struct Nut { int X; int Y; int yes; }; Nut nut; typedef struct Snake Snake; struct Snake { int snakeX[100]; int snakeY[100]; int node; int life; DirectionName direction; }; Snake snake; DirectionName snakeDirection; void initNut() { nut.X=rand()%84; nut.Y=rand()%48; //nutFlag=1; } void drawNut() { //int X = rand()%84; //int Y = rand()%48; if(nutFlag==1){ lcd.drawCircle(nut.X,nut.Y,1,1); } } void moveSnake() { if(joystick.direction == CENTRE){ //snake.snakeX[0]+=1; joystick.direction = CENTRE; } //else if(joystick.button == button){ // snake.snakeX[i]=snake.snakeX[i]; // snake.snakeY[i]=snake.snakeY[i]; // } else if(joystick.direction == UNKNOWN){ joystick.direction = UNKNOWN; } else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) { snake.direction= RIGHT; //snake.snakeX[0]+=1; } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) { snake.direction=LEFT; //snake.snakeX[0]-=1; } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) { snake.direction=UP; //snake.snakeY[0]-=1; } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) { snake.direction=DOWN; //snake.snakeY[0]+=1; } } } void initSnake() { snake.snakeX[0] = 16; //coordinate of head snake.snakeY[0] = 10; snake.snakeX[1] = 14; snake.snakeY[1] = 10; snake.snakeX[2] = 12; snake.snakeY[2] = 10; snake.snakeX[3] = 10; snake.snakeY[3] = 10; snake.node = 6; //node of snake snake.direction = RIGHT; } void drawSnake() { for(i=0; i<snake.node-1; i++) { lcd.drawCircle(snake.snakeX[i],snake.snakeY[i],1,1); //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]); } } /* */ void startGmae() { //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1); //nutFlag = 1; //lifeFlag = 0; //snake.direction=LEFT; //if(joystick.direction == UNKNOWN) if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1)) { // if snake got nut snake.node++; //buzzer.period(1/659); // set PWM period //buzzer=0.1; // set duty cycle //wait(0.1); nutFlag=0; // new nut appear } if(nutFlag==0) { //set nut randomly nut.X = rand()%83; nut.Y = rand()%47; nutFlag = 1; } for(i=3; i<snake.node; i++) { //if snake collide itself snake die if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { lifeFlag=1; // break; } } for(i=snake.node-1; i>0; i--) { //move snake if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47){ snake.snakeX[i]=snake.snakeX[i-1]; snake.snakeY[i]=snake.snakeY[i-1]; } else{ snake.snakeX[i]=snake.snakeX[i]; snake.snakeY[i]=snake.snakeY[i]; } } if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { lifeFlag=1; // snake collide the corrider snake die //break; } //if(snake.life==1) { //break; //} } void printScore(){ lcd.printString("YourScoreis",0,0); int score=snake.node-5; int length = sprintf(buffer,"\n SCORE=%2d \n",score); } /* void buttonPressed(){ buttonFlag=1; } */ //while(joystick.direction == UNKNOWN) { //switch(snake.direction) //{ // case RIGHT:snake.X[0]+=1; // break; // case LEFT:snake.X[0]-=1; // break; // case UP:snake.Y[0]-=1; // break; // case DOWN:snake.Y[0]+=1; // break; // } //end of while(joystick.direction = UNKNOWN) // if(joystick.direction == UP&&snake.direction!=DOWN) { // snake.direction=UP; // } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) { // snake.direction= RIGHT; // } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) { // snake.direction=LEFT; // } else(joystick.direction == DOWN&&snake.direction!=UP) { // snake.direction=DOWN; // } // 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; //LED OFF } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; //LIGHT LED1 //snake.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; //LIGHT LED2 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; //LIGHT LED3 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else { joystick.direction = UNKNOWN; } // set flag for printing printFlag = 1; } void updateSnake() { // get direction of joystick and update snake if (snake.direction == LEFT) { snake.snakeX[0]--; //LED1 if(snake.snakeX[0]<1) { snake.snakeX[0]=1; }//will stop at the top edge } if (snake.direction == RIGHT) { snake.snakeX[0]++; //LED2 if(snake.snakeX[0]>83) { snake.snakeX[0]=83; }//will stop at the bottom edge } if (snake.direction == UP) { snake.snakeY[0]--; if(snake.snakeY[0]>47) { snake.snakeY[0]=47; }//will stop at the left edge } if (snake.direction == DOWN) { snake.snakeY[0]++; if(snake.snakeY[0]<1) { snake.snakeY[0]=1; }//will stop at the right edge } if(snake.direction == CENTRE){ snake.snakeX[0]+=1; } if(snake.direction == UNKNOWN){ snake.snakeX[0]+=1; } } //main code int main() { lcd.init(); lcd.setBrightness(0.5); // put LED backlight on 50% //button.rise(&buttonPressed); calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second lcd.refresh(); initNut(); initSnake(); //buzzer.period(0.020); // a 20ms period lcd.printString("press to start",0,0); //printScore(); //infinite while loop while(1) { //if(button){ lcd.clear(); lifeFlag=0; updateSnake(); drawSnake(); drawNut(); moveSnake(); startGmae(); if(lifeFlag==1){ lcd.clear(); lcd.refresh(); //lcd.printString("press to start",0,0); printScore(); lifeFlag=0; } lcd.refresh(); wait(0.1); } //else //lcd.printString("press to start",20,20); // } //wait(0.5); //if(snake.life==1) { // lcd.printString("lost",24,42); // break; }