I need help with my game (snake game)

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] );
}

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)

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

1 Like

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.

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

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.




    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;

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

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


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

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

you check the positions and when they are equal

you change cameleonX

you don’t change banana

I don’t know.


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