Small problem while making snake game

I’m making a snake game where you can control the snake using the mouse, so if you’re above the snake’s head it should go up etc. I don’t know how I can make this, I’ve tried switching around if statements but to no avail, I hope (and appreciate :)) that someone could assist me. Here’s the code:

int snakeGrootte=100; 
int maxlengte=500;
int kopx[]=new int[maxlengte]; 
int kopy[]=new int[maxlengte];
int i;

int xsnelheid, ysnelheid;

void setup () {
  size (1000, 1000);
  kopx[0] = 250;
  kopy[0] = 250;
  xsnelheid=0;    
  ysnelheid=0;
}

void draw () {
  background (0);
  for (int i =0; i < snakeGrootte; i++) {
    fill(255, 255, 0);
    rectMode(CENTER);
    rect(kopx[i], kopy[i], 20, 20);
  }
  kopx[0]=kopx[0]+xsnelheid;
  kopy[0]=kopy[0]+ysnelheid;

  for (int i = snakeGrootte; i > 0; i--) {
    kopx[i] = kopx[i-1];
    kopy[i] = kopy[i-1];
  }}
  
void mousePressed()
{
  if(mouseX > kopx[1]) {
     xsnelheid = 2;
     ysnelheid = 0;
  }
  else if(mouseX < kopx[1]) {
     xsnelheid = -2;
     ysnelheid = 0;
  }
  else if(mouseY < kopy[1]) {
    xsnelheid = 0;
    ysnelheid = -2;
  }
  else if(mouseY > kopy[1]) {
   xsnelheid = 0;
   ysnelheid = 2;
  }
}

try to check for mouseX AND mouseY every if

The movement is still janky, do you have any other suggestion? Appreciate the help.

when you post your entire code so we can see it?

That’s the entire code so far, if you throw it in the IDE it should display a snake :slight_smile:

I don’t understand, when you wrote “the movement is still janky”, did you change your code with checking mouseX and mouseY?

So you have a new version. Can you post it?

Gotta go

int snakeGrootte=100; 
int maxlengte=1011;
int kopx[]=new int[maxlengte]; 
int kopy[]=new int[maxlengte];
int i;

int xspeed, ysnelheid;

void setup () {
  size (1000, 1000);
  kopx[0] = 250;
  kopy[0] = 250;
  xspeed=0;    
  ysnelheid=0;
}

void draw () {
  background (0);
  for (int i =0; i < snakeGrootte; i++) {
    fill(100, 100, 255);
    rectMode(CENTER);
    rect(kopx[i], kopy[i], 30, 20);
  }
  kopx[0]=kopx[0]+xspeed;
  kopy[0]=kopy[0]+ysnelheid;

  for (int i = snakeGrootte; i > 0; i--) {
    kopx[i] = kopx[i-1];
    kopy[i] = kopy[i-1];
  }}
  
void mousePressed()
{
  if(mouseX > kopx[1]) {
     xspeed = 2;
     ysnelheid = 0;
  }
  else if(mouseX < kopx[1]) {
     xspeed = -2;
     ysnelheid = 0;
  }
  else if(mouseX < kopy[1]) {
    xspeed = 0;
    ysnelheid = -2;
  }
  else if(mouseX > kopy[1]) {
   xspeed = 0;
   ysnelheid = 2;
  }
  else if(mouseY > kopx[1]) {
   xspeed = 0;
   ysnelheid = 2;
  }
  else if(mouseY < kopx[1]) {
   xspeed = -2;
   ysnelheid =0;
  }
  else if(mouseY < kopy[1]) {
   xspeed = 0;
   ysnelheid = -2;
  }
  else if(mouseY > kopy[1]) {
   xspeed = 0;
   ysnelheid = 2;
  }
}

Ah, no, I meant that you keep 4 ifs, but that you check mouseX and mouseY in each if

Sorry for the misunderstanding

int snakeGrootte=100; 
int maxlengte=1011;
int kopx[]=new int[maxlengte]; 
int kopy[]=new int[maxlengte];
int i;

int xsnelheid, ysnelheid;

void setup () {
  size (1000, 1000);
  kopx[0] = 250;
  kopy[0] = 250;
  xsnelheid=0;    
  ysnelheid=0;
}

void draw () {
  background (0);
  for (int i =0; i < snakeGrootte; i++) {
    fill(100, 100, 255);
    rectMode(CENTER);
    rect(kopx[i], kopy[i], 30, 20);
  }
  kopx[0]=kopx[0]+xsnelheid;
  kopy[0]=kopy[0]+ysnelheid;

  for (int i = snakeGrootte; i > 0; i--) {
    kopx[i] = kopx[i-1];
    kopy[i] = kopy[i-1];
  }}
  
void mousePressed()
{
  if(mouseX > kopx[1]) {
    ysnelheid = 0;
    xsnelheid = 2;
  }
  if(mouseY < kopy[1]) {
   ysnelheid = -2;
   xsnelheid = 0;
  }
  if(mouseX < kopx[1]) {
   ysnelheid = 0;
   xsnelheid = -2;
}
}

So far so good, the snake doesn’t go down the y-axis unfortunately, if I add the fourth statement, the game glitches out and does not work anymore. I’m so close to solving this issue haha

I’m gonna bump this post, hope anyone could help me out. Thanks in advance

What happens?

You totally didn’t follow my advice

here is an example

You have to click exactly left or right from the head of the snake to make it turn.

Not all checks are performed, I was lazy

(you might want to check whether the snake goes back into itself (I mean, when it goes left, it shouldn’t be allowed to go right immediately))

Chrisir

int snakeGrootte=100; 
int maxlengte=1011;
int kopx[]=new int[maxlengte]; 
int kopy[]=new int[maxlengte];
int i;

int xsnelheid, ysnelheid;

void setup () {
  size (1000, 1000);
  kopx[0] = 250;
  kopy[0] = 250;
  xsnelheid=0;    
  ysnelheid=0;
}

void draw () {
  background (0);
  for (int i =0; i < snakeGrootte; i++) {
    fill(100, 100, 255);
    rectMode(CENTER);
    rect(kopx[i], kopy[i], 
      30, 20);
  }
  kopx[0]=kopx[0]+xsnelheid;
  kopy[0]=kopy[0]+ysnelheid;

  for (int i = snakeGrootte; i > 0; i--) {
    kopx[i] = kopx[i-1];
    kopy[i] = kopy[i-1];
  }
}

void mousePressed() {
  //
  if (mouseX > kopx[1]-15 && mouseX < kopx[1]+15 && mouseY >  kopy[1] - 15) {
    xsnelheid = 0;   // DN 
    ysnelheid = 2;
    println("Here 1 ");
    return;
  }

  if (mouseX > kopx[1]-15  && (mouseY > kopy[1] - 10)   ) {
    xsnelheid = 2; // RT 
    ysnelheid = 0;
  } else if (mouseX < kopx[1]+15  && (mouseY > kopy[1] - 10)   ) {
    xsnelheid = -2;  // LT
    ysnelheid = 0;
  } else if (mouseY < kopy[1]+15  && mouseX > kopx[1] - 15) {
    xsnelheid = 0;   // UP
    ysnelheid = -2;
  } else {
    println("None");
  }
  //
}// func 
//