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:
- 2:d7b17623ba26
- Parent:
- 1:f682aeb462f1
- Child:
- 3:04ff8cd27dd1
diff -r f682aeb462f1 -r d7b17623ba26 main.cpp
--- a/main.cpp Tue Apr 28 12:27:00 2015 +0000
+++ b/main.cpp Thu Apr 30 15:54:35 2015 +0000
@@ -10,9 +10,11 @@
DigitalIn button(p19);
AnalogIn xPot(p15);
AnalogIn yPot(p16);
-
+//InterruptIn button(p19);
int i;
int j;
+int X;
+int Y;
int snakeX[100];
int snakeY[100];
@@ -47,7 +49,7 @@
Joystick joystick;
int printFlag = 0;
-
+//int buttonFlag = 0;
// function prototypes
//void setPixel(int x, int y);
//void clearPixel(int x, int y);
@@ -73,70 +75,91 @@
DirectionName snakeDirection;
-//void drawNut()
-//{
-// int X = rand()%84;
-// int Y = rand()%48;
-// lcd.drawCircle(X,Y,1,1);
-//}
+void initNut()
+{
+ nut.X=40;
+ nut.Y=20;
+ nut.yes=0;
+ }
+void drawNut()
+{
+ int X = rand()%84;
+ int Y = rand()%48;
+ lcd.drawCircle(X,Y,1,1);
+}
void moveSnake()
{
-
+ snake.snakeX[0]+=1;
+ wait(0.2);
+
+ /*
if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) {
+
if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
- snake.snakeX[0]-=1;
+ snake.direction= RIGHT;
+ snake.snakeX[0]+=1;
} else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
- snake.snakeX[0]+=1;
+ snake.direction=LEFT;
+ snake.snakeX[0]-=1;
} else if(joystick.direction == UP&&snake.direction!=DOWN) {
+ snake.direction=DOWN;
snake.snakeY[0]-=1;
} else if(joystick.direction == DOWN&&snake.direction!=UP) {
+ snake.direction=UP;
snake.snakeY[0]+=1;
}
}
+ */
+}
+
+void initSnake()
+{
+
+ snake.snakeX[0] = 10; //coordinate of head
+ snake.snakeY[0] = 10;
+ snake.snakeX[1] = 12;
+ snake.snakeY[1] = 10;
+ snake.snakeX[2] = 14;
+ snake.snakeY[2] = 10;
+ snake.snakeX[3] = 16;
+ snake.snakeY[3] = 10;
+ snake.node = 4; //node of snake
+
}
void drawSnake()
{
- snake.snakeX[0] = 10; //coordinate of head
- snake.snakeY[0] = 10;
- snake.snakeX[1] = 11;
- snake.snakeY[1] = 10;
- snake.snakeX[2] = 12;
- snake.snakeY[2] = 10;
- snake.snakeX[3] = 13;
- snake.snakeY[3] = 10;
- snake.node = 5; //node of snake
+ for(i=0; i<snake.node; i++) {
- for(i=0; i<snake.node-1; i++) {
-
- lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
+ lcd.drawRect(snake.snakeX[i],snake.snakeY[i],1,1,1);
+ //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
}
}
/*
-
*/
void startGmae()
{
- nut.yes = 1;
+ //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1);
+ nut.yes = 0;
snake.life = 0;
- snake.direction=LEFT;
+ //snake.direction=LEFT;
//if(joystick.direction == UNKNOWN)
//{
- if(nut.yes==1) { //set nut randomly
+ if(nut.yes==0) { //set nut randomly
nut.X = rand()%83;
nut.Y = rand()%47;
- nut.yes = 0;
- lcd.drawCircle(nut.X,nut.Y,1,1);
+ nut.yes = 1;
+ drawNut();
}
-
+
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]) {
@@ -207,6 +230,7 @@
joystick.y0 = yPot;
}
+
void updateJoystick()
{
// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
@@ -239,27 +263,27 @@
{
// get direction of joystick and update snake
if (joystick.direction == UP) {
- snake.snakeY[0]--;
+ snake.snakeX[0]--;
if(snake.snakeY[0]<1) {
snake.snakeY[0]=1;
}//will stop at the top edge
}
if (joystick.direction == DOWN) {
- snake.snakeY[0]++;
+ snake.snakeX[0]++;
if(snake.snakeY[0]>43) {
snake.snakeY[0]=43;
}//will stop at the bottom edge
}
if (joystick.direction == LEFT) {
- snake.snakeX[0]--;
+ snake.snakeY[0]--;
if(snake.snakeX[0]<1) {
snake.snakeX[0]=1;
}//will stop at the left edge
}
if (joystick.direction == RIGHT) {
- snake.snakeX[0]++;
+ snake.snakeY[0]++;
if(snake.snakeX[0]>87) {
snake.snakeX[0]=87;
}//will stop at the right edge
@@ -269,16 +293,18 @@
}
+
//main code
int main()
{
lcd.init();
lcd.setBrightness(0.5); // put LED backlight on 50%
lcd.printString("press to start",0,0);
-
calibrateJoystick(); // get centred values of joystick
pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
lcd.refresh();
+ initNut();
+ initSnake();
//for(i=20,i<83,i++){
// i=i;
@@ -288,18 +314,41 @@
//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();
- // drawNut();
+ drawNut();
moveSnake();
startGmae();
lcd.refresh();
- //wait(0.5);
- //if(snake.life==1) {
- // lcd.printString("lost",24,42);
- // break;
+ wait(0.5);
+
+ serial.printf(" hi");
}
+ //wait(0.5);
+ //if(snake.life==1) {
+ // lcd.printString("lost",24,42);
+ // break;
+
}