# I need help with my game (snake game)

**URL:** <https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 28, 2020, 8:24pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984 "2020-10-28T20:24:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://discourse.processing.org/u/umar)\
**Post date:** [October 28, 2020, 8:24pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/1 "2020-10-28T20:24:01Z")

</div>

It’s actually a snake game, but with 16 by 8 blocks window size, where yellow is for banana(+10 points), red is for apple(+5 points) and green block is for cameleon(-5 points). The problem I’m facing is that my fruits are never in the same coordinates as my snake’s head. So I’m unable to eat or score any point. Please help is anyone can. I have shared my code

int xbuttons = 60;  
int textLocX= 242;  
int num=4;  
int buttonsY [] = new int[num];  
int textLocY [] = new int[num];  
int index=0;  
int pagesize=0;  
int positi [] = new int[num];

int blocks=30;

int buttonC =#FFFF00;  
int buttonW = 400;  
int buttonH=50;

int numbx=16;  
int numby=8;

int angle=0;  
int snakesize=5;  
int time=0;  
int[] headx= new int[2500];  
int[] heady= new int[2500];  
int applex=int(random(1,numbx))\*width/numbx;  
int appley=int(random(1,numby))\*width/numby;  
int bananax=(round(random(48))+1)\*8;  
int bananay=(round(random(48))+1)\*8;

int cameleonx=(round(random(48))+1)\*8;  
int cameleony=(round(random(48))+1)\*8;  
boolean redo=true;  
boolean stopgame=false;  
int a=49;  
int bc=0;  
int score=0;

int blocksx=30;  
int blocksy=60;

void setup() {  
size(496, 496);  
background(0);  
mouseClicked();  
restart();

textAlign(CENTER);  
background(255);

for ( int i=1; i\<4; i++)  
{  
buttonsY[index]=50;  
buttonsY [index + i] +=buttonsY[index +i -1] +100;

```
positi[i]=buttonsY[i];

textLocY[index]=80;
textLocY [index + i] += textLocY[index +i -1] +100;

```

}  
}

void draw() {  
if (pagesize==0){  
Button1();  
Button2();  
Button3();  
Button4();  
headx[1]=8;  
heady[1]=9;

}

if (pagesize==1){

if (stopgame)  
{  
//do nothing because of game over (stop playing)  
}  
else  
{

```
//draw stationary stuff

```

time+=2;  
stroke(255);  
fill(255,0,0);  
stroke(255);  
rect(applex,appley,blocksx,blocksy);

fill(255,251,0);  
stroke(255);  
rect(bananax,bananay,blocksx,blocksy);

fill(0,255,0);  
stroke(255);  
rect(cameleonx,cameleony,blocksx,blocksy);

fill(0);  
stroke(0);  
rect(0,0,width,8);  
rect(0,height-8,width,8);  
rect(0,0,8,height);  
rect(width-8,0,8,height);

//my modulating time by 5, we create artificial frames each 5 frames  
//(otherwise the game would go WAY too fast!)  
if ((time % 50)==0)  
{  
travel();  
display();  
checkdead();  
score();  
}  
}  
}  
}

void mouseClicked() {  
if(mouseX\>=xbuttons && mouseX\<=buttonW && mouseY\>=buttonsY[3] && mouseY\>=buttonH) {  
println(“Mouse Clicked”);  
pagesize=1;  
background(255);  
stroke(0);  
for ( int i=8; i\<=width; i+=30){  
line(i,0,i,height);  
}

for ( int i=8; i\<=width; i+=60){  
line(0,i,width,i);  
}  
}  
}

void display()  
{  
if (pagesize==1){

if (headx[1]==cameleonx && heady[1]==cameleony)  
{

```
snakesize--;
score-=5;
redo=true;
while(redo)
{
  
  
  for(int i=1;i<snakesize;i++)
  {
    
    if (cameleonx==headx[i] && cameleony==heady[i])
    {
      redo=true;
      
    }
    else
    {
      redo=false;
      i=1000;
    }
  }
}

```

}

if (headx[1]==applex && heady[1]==appley)  
{

```
snakesize+=round(random(1)+1);
score+=5;
redo=true;
while(redo)
{
  applex=int(random(1,numbx))*width/numbx;
  appley=int(random(1,numby))*width/numby;
  
  for(int i=1;i<snakesize;i++)
  {
    
    if (applex==headx[i] && appley==heady[i])
    {
      redo=true;
      
    }
    else
    {
      redo=false;
      i=1000;
    }
  }
}

```

}

if (headx[1]==bananax && heady[1]==bananay)  
{  
snakesize+=round(random(1)+1);  
score+=10;  
redo=true;

```
  while(redo)
{
  for ( int inc=1; inc<=20; inc++){
  bananax=round(random(a)+1)*8;
  bananay=round(random(a)+1)*8;
  
  for ( int i=1; i<snakesize; i++)
  {
    if (headx[1]==bananax && heady[1]==bananay)
    {
      redo=true;
      i++;
      
    }
    else {
      redo=false;
    }
  }
}
}
}

```

//draw the new head of the snake…  
stroke(255);  
fill(0);  
rect(headx[1],heady[1],blocksx,blocksy);  
//…then erase the back end of the snake.  
fill(255);  
rect(headx[snakesize],heady[snakesize],blocksx,blocksy);  
}  
}

void keyPressed()  
{  
if (key == CODED)  
{

```
if (keyCode == UP && angle!=270 && (heady[1]-8)!=heady[2])
{
  angle=90;
}
if (keyCode == DOWN && angle!=90 && (heady[1]+8)!=heady[2])
{
  angle=270;
}if (keyCode == LEFT && angle!=0 && (headx[1]-8)!=headx[2])
{
  angle=180;
}if (keyCode == RIGHT && angle!=180 && (headx[1]+8)!=headx[2])
{
  angle=0;
}
if (keyCode == SHIFT)
{
  //restart the game by pressing shift
  restart();
}

```

}  
}

void travel()  
{  
if (pagesize==1) {  
for(int i=snakesize;i\>0;i–)  
{  
if (i!=1)  
{  
//shift all the coordinates back one array  
headx[i]=headx[i-1];  
heady[i]=heady[i-1];  
}  
else  
{  
//move the new spot for the head of the snake, which is  
//always at headx[1] and heady[1].  
switch(angle)  
{  
case 0:  
headx[1]+=blocksx;  
break;  
case 90:  
heady[1]-=blocksy;  
break;  
case 180:  
headx[1]-=blocksx;  
break;  
case 270:  
heady[1]+=blocksy;  
break;  
}  
}  
}  
}  
}

void restart()  
{  
if (pagesize==1){  
//by pressing shift, all of the main variables reset to their defaults.  
background(255);  
headx[1]=8;  
heady[1]=9;  
for(int i=2;i\<1000;i++)  
{  
headx[i]=0;  
heady[i]=0;  
}  
stopgame=false;  
applex=int(random(1,numbx))\*width/numbx;  
appley=int(random(1,numby))\*width/numby;

bananax=int(random(1,numbx))\*width/numbx;  
bananay=int(random(1,numby))\*width/numby;

for(int i=8; i\<width; i+=30)  
{  
cameleonx=int(random(i));  
}

for (int i=8; i\<height; i+=60)  
{  
cameleony=int(random(i));  
}

stroke(0);  
for ( int i=8; i\<=width; i+=30){  
line(i,0,i,height);  
}

for ( int i=8; i\<=width; i+=60){  
line(0,i,width,i);  
}

snakesize=5;  
time=0;  
angle=0;  
redo=true;  
score=0;

}  
}  
float sinecolor(float percent)  
{  
float slime=(sin(radians((((time +(255\*percent)) % 255)/255)\*360)))\*255;  
return slime;  
}

void score(){  
if (pagesize==1){

noStroke();  
fill(255);  
textSize(12);  
rect(width-80,height-20,70,12);  
fill(0);  
text("Score= " +score, width-40,height-10);  
}  
}

void checkdead()  
{  
if (pagesize==1){  
for(int i=2;i\<=snakesize;i++)  
{  
//is the head of the snake occupying the same spot as any of the snake chunks?  
if (headx[1]==headx[i] && heady[1]==heady[i])  
{  
fill(0);  
rect(80,125,300,120);  
fill(255);  
text(“GAME OVER”,230,150);  
text(“To restart, press Shift.”,230,200);  
stopgame=true;

```
  text("score=" +score, 230,225);
}
//is the head of the snake hitting the walls?
if (headx[1]>=(width-8) || heady[1]>=(height-8) || headx[1]<=0 || heady[1]<=8)
{
  fill(0);
  rect(80,125,300,120);
  fill(255);
  text("GAME OVER",230,150);
  text("To restart, press Shift.",230,200);
  
  stopgame=true;
  text("score=" +score, 230,225);
}

```

}  
}  
}

void Button1(){  
stroke(255);  
fill (buttonC);  
rect(xbuttons, buttonsY[0], buttonW, buttonH );  
fill(0);  
textSize(24);  
text(“Number of Players”, textLocX, textLocY[0] );

}

void Button2(){  
stroke(255);  
fill (buttonC);  
rect(xbuttons, buttonsY[1], buttonW, buttonH );  
fill(0);  
textSize(24);  
text("Number of Bananas 20 " , textLocX, textLocY[1] );  
}

void Button3(){  
stroke(255);  
fill (buttonC);  
rect(xbuttons, buttonsY[2], buttonW, buttonH );  
fill(0);  
text("Page Size ", textLocX, textLocY[2] );

}

void Button4(){  
fill (buttonC);  
rect(xbuttons, buttonsY[3], buttonW, buttonH );  
fill(0);  
textSize(24);  
text(“Start Game!” , textLocX+20, textLocY[3] );  
}

---

<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:** [October 28, 2020, 9:37pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/2 "2020-10-28T21:37:55Z")

</div>

Hey, and welcome to the forum!

Great to have you here!

Wow!  
I really had difficulties to find this one.

**Explanation**

Now, in these lines you draw the grid with a start of 8 and 8 (for your `i`'s) (should be 8 and 9 I guess)

```auto
      // GRID
      stroke(0);
      for ( int i=8; i<=width; i+=30) {
        line(i, 0, i, height);
      }

      for ( int i=8; i<=width; i+=60) {
        line(0, i, width, i);
      }

```

also you start the snake with 8 and 9 for x,y:

```
headx[1]=8;
heady[1]=9;

```

and add blocksx and blocksy to it (which are 30 and 60 like in the for-loops above).

**BUT you forgot to add this 8,9 to the apple etc.**

You can see what I mean:

- you have a grid, the snake is moving exactly (almost) moving in the grid. The apple etc. are all just a bit above and left from their cell (in the grid). This is caused by the missing 8 and 9 from the position x,y. Therefore the snake position never matches a apple (etc.) position.

(there are a lot of other errors, e.g. `restart()` doesn’t get called from `setup()` imho)

Please tell me if this helps.

I got a running version of your code here now. But maybe there were other issues I haven’t discussed here (and that I solved during searching the error). Then please do write.

Warmest regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://discourse.processing.org/u/umar)\
**Post date:** [October 29, 2020, 4:34pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/3 "2020-10-29T16:34:12Z")

</div>

> [@Chrisir](#):
>
> Please tell me if this helps.

Hi Chrisir,

Thank you for welcoming me and your time to help me. i appreciate it! Thank you so much for your explanation, you also mentioned “I got a running version of your code here now.” can you please send me the new complete code? or the parts you fixed? i would greatly appreaciate it! i really need to see the mistakes / issues that weren’t discussed.

---

<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:** [October 29, 2020, 4:35pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/4 "2020-10-29T16:35:45Z")

</div>

No, we are discouraged to send full codes.

My apologies.

But I can lead you through when you have questions regarding your code

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://discourse.processing.org/u/umar)\
**Post date:** [October 29, 2020, 4:46pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/5 "2020-10-29T16:46:02Z")

</div>

No worries I completely understand.

If you can help me with the code, then I have a question,  
I am having problem in assigning random values to apple,banana, & cameloen. like setting the limit (high,low).  
If you can help me with that, it would be highly appreciated.

Thank you.

---

<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:** [October 29, 2020, 4:52pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/6 "2020-10-29T16:52:55Z")

</div>

```auto

    applex=int(random(1, 14))*30+8;
    appley=int(random(1, 7))*60+9;

    bananax=int(random(1, numbx))*30+8;
    bananay=int(random(1, numby))*60+9;

```

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://discourse.processing.org/u/umar)\
**Post date:** [October 29, 2020, 5:08pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/7 "2020-10-29T17:08:32Z")

</div>

Thank you for your help. This correction had made my code work properly.

I have 2 more questions. If you can help me with them, I would really appreciate it…

First is I need the snake to move when I press the key, As I press the key, the snake should move 1 block.

second, the bananas should be spawned in 20 places all at once. as the snake eats them. it would add score and not spawned again on that block

I would really appreciate if you can help with that.

regards

Umar

---

<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:** [October 29, 2020, 5:18pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/8 "2020-10-29T17:18:21Z")

</div>

> [@umar](#):
>
> I need the snake to move when I press the key, As I press the key, the snake should move 1 block.

Do you mean, the snake stops when you don’t press the key anymore?

The moving of the snake is in travel().

You can for example say angle = -1; at the end of the function travel().

* * *

**Bananas**

> [@umar](#):
>
> the bananas should be spawned in 20 places all at once.

```auto

 for (int i=0; i<20; i+=!)
    {
      make a banana array (similar to your snake array) and fill it here
      bananaX[i]=...........................
      bananaY[i]=...........................
    }

```

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://discourse.processing.org/u/umar)\
**Post date:** [October 29, 2020, 10:11pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/9 "2020-10-29T22:11:39Z")

</div>

Hi, Thanky you for you help.

Now I have managed to get everything together, and now I have the correct code that works properly.

But there is only one problem, I have make this concept for avoiding a fruit to be spawned on the cameleon, but for some reason it’s not working for me.

void checkpos() {  
for ( int i=0; i\<index3; i++) {  
if(cameleonX[i]==bananaX[i] && cameleonY[i]==bananaY[i]) {  
cameleonX[i]=int(random(1, numbx))\*30+8;  
cameleonY[i]=int(random(1, numby))\*60+9;  
}  
for(int j=10; j\<20; j++)  
if(cameleonX[i]==bananaX[j] && cameleonY[i]==bananaY[j]) {  
cameleonX[i]=int(random(1, numbx))\*30+8;  
cameleonY[i]=int(random(1, numby))\*60+9;  
}  
}

}

Can you help?

Regards

Umar

---

<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:** [October 29, 2020, 11:44pm UTC](https://discourse.processing.org/t/i-need-help-with-my-game-snake-game/24984/10 "2020-10-29T23:44:32Z")

</div>

> [@umar](#):
>
> for avoiding a fruit to be spawned on the cameleon

you check the positions and when they are equal

you change cameleonX

you don’t change banana

I don’t know.

```auto

void checkpos() {
  for ( int i=0; i<index3; i++) {
    for (int j=10; j<20; j++) {
      if (cameleonX[i]==bananaX[j] && 
        cameleonY[i]==bananaY[j]) {
        cameleonX[i]=int(random(1, numbx))*30+8;
        cameleonY[i]=int(random(1, numby))*60+9;
      }
    }
  }
}

```
