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.
Diff: main.cpp
- Revision:
- 3:04ff8cd27dd1
- Parent:
- 2:d7b17623ba26
- Child:
- 4:41acda9d68c7
--- a/main.cpp Thu Apr 30 15:54:35 2015 +0000 +++ b/main.cpp Thu Apr 30 20:34:29 2015 +0000 @@ -49,6 +49,7 @@ Joystick joystick; int printFlag = 0; +int nutFlag =1; //int buttonFlag = 0; // function prototypes //void setPixel(int x, int y); @@ -77,42 +78,46 @@ void initNut() { - nut.X=40; - nut.Y=20; - nut.yes=0; + nut.X=rand()%84; + nut.Y=rand()%48; + //nutFlag=1; } + void drawNut() { - int X = rand()%84; - int Y = rand()%48; - lcd.drawCircle(X,Y,1,1); + //int X = rand()%84; + //int Y = rand()%48; + if(nutFlag==1){ + lcd.drawCircle(nut.X,nut.Y,2,1); + } } void moveSnake() { - snake.snakeX[0]+=1; - wait(0.2); + + if(joystick.direction == UNKNOWN||joystick.direction == CENTRE){ + snake.snakeX[0]+=1; + } - /* - if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { + else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { - if(joystick.direction == RIGHT&&snake.direction!=LEFT) { + if(joystick.direction == DOWN&&snake.direction!=LEFT) { snake.direction= RIGHT; snake.snakeX[0]+=1; - - } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) { + + } else if(joystick.direction == UP&&snake.direction!=RIGHT) { snake.direction=LEFT; snake.snakeX[0]-=1; - } else if(joystick.direction == UP&&snake.direction!=DOWN) { + } else if(joystick.direction == LEFT&&snake.direction!=DOWN) { snake.direction=DOWN; snake.snakeY[0]-=1; - } else if(joystick.direction == DOWN&&snake.direction!=UP) { + } else if(joystick.direction == RIGHT&&snake.direction!=UP) { snake.direction=UP; snake.snakeY[0]+=1; } } - */ + } void initSnake() @@ -146,18 +151,21 @@ void startGmae() { //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1); - nut.yes = 0; + //nutFlag = 1; snake.life = 0; //snake.direction=LEFT; //if(joystick.direction == UNKNOWN) //{ - - if(nut.yes==0) { //set nut randomly + if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut + snake.node++; + nutFlag=0; // new nut appear + } + + if(nutFlag==0) { //set nut randomly nut.X = rand()%83; nut.Y = rand()%47; - nut.yes = 1; - drawNut(); + nutFlag = 1; } @@ -182,10 +190,7 @@ if(snake.life==1) { //break; } - if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut - snake.node++; - nut.yes=1; // new nut appear - } + //} } @@ -306,33 +311,8 @@ initNut(); initSnake(); - //for(i=20,i<83,i++){ - // i=i; - // j=j; - // wait(0.5); - // } - //infinite while loop while(1) { - - if (printFlag) { // if flag set, clear flag and print joystick values to serial port - printFlag = 0; - serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button); - - // check joystick direction - if (joystick.direction == UP) - serial.printf(" UP\n"); - if (joystick.direction == DOWN) - serial.printf(" DOWN\n"); - if (joystick.direction == LEFT) - serial.printf(" LEFT\n"); - if (joystick.direction == RIGHT) - serial.printf(" RIGHT\n"); - if (joystick.direction == CENTRE) - serial.printf(" CENTRE\n"); - if (joystick.direction == UNKNOWN) - serial.printf(" Unsupported direction\n"); - } lcd.clear(); updateSnake(); drawSnake(); @@ -340,9 +320,9 @@ moveSnake(); startGmae(); lcd.refresh(); - wait(0.5); + + wait(0.1); - serial.printf(" hi"); } //wait(0.5); //if(snake.life==1) {