# Small problem while making snake game

**URL:** https://discourse.processing.org/t/small-problem-while-making-snake-game/16990
**Category:** Coding Questions
**Created:** [January 9, 2020, 6:56pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990 "2020-01-09T18:56:13Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 9, 2020, 6:56pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/1 "2020-01-09T18:56:13Z")

</div>

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:

```auto
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;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 9, 2020, 7:34pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/2 "2020-01-09T19:34:32Z")

</div>

> [@RoebenV](#):
>
> if(mouseX \> kopx[1]) {

try to check for mouseX AND mouseY every if

---

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 9, 2020, 7:49pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/3 "2020-01-09T19:49:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 9, 2020, 8:15pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/4 "2020-01-09T20:15:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 9, 2020, 8:23pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/5 "2020-01-09T20:23:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 9, 2020, 8:25pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/6 "2020-01-09T20:25:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 9, 2020, 8:27pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/7 "2020-01-09T20:27:52Z")

</div>

```auto
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;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 9, 2020, 8:38pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/8 "2020-01-09T20:38:52Z")

</div>

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

Sorry for the misunderstanding

---

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 9, 2020, 9:07pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/9 "2020-01-09T21:07:18Z")

</div>

```auto
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

---

<div class="post-metadata">

### Author: ![RoebenV](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@RoebenV](https://discourse.processing.org/u/RoebenV)
#### Post date: [January 10, 2020, 10:32am UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/10 "2020-01-10T10:32:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 10, 2020, 9:20pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/11 "2020-01-10T21:20:36Z")

</div>

> [@RoebenV](#):
>
> if I add the fourth statement, the game glitches out and does not work anymore.

What happens?

You totally didn’t follow my advice

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 10, 2020, 9:35pm UTC](https://discourse.processing.org/t/small-problem-while-making-snake-game/16990/12 "2020-01-10T21:35:21Z")

</div>

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

```auto
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 
//

```
