Snake Game Code

Snake Code assignment

Sorry, but that’s not really how this works. We can’t just do you work for you.

However, if you break your problem down into smaller steps and take those steps on one at a time, we’ll be happy to help you- for free!

Which part of this are you stuck on?

So I’ve done so far which is like the first three steps
Can you please help from there onwards?

final int X_SPEED = 1;
final int Y_SPEED = 1;

int headX = 250;
int headY = 250;

final int SNAKE_SIZE=15;

int applex= (round(random(97))+10)+80;
int appley= (round(random(97))+10)+80;


void setup()
{
  size(500,500);
}

void draw()
{
  background(192);
  fill(255);
  moveSnake();
  
  if(headX >= 500 || headX <= 0)
  {
    background(0); 
  }
  
  if(headY >= 500 || headY <= 0)
  {
    background(0); 
  }
  
  fill(255,0,0);
   stroke(0);
   rect(applex,appley,8,8);
}

void moveSnake()
{
  if(keyCode == RIGHT)
  {
    drawSnakeX();
    headX += X_SPEED; 
  }
  else if(keyCode == LEFT)
  {
    drawSnakeX();
    headX -= X_SPEED;
  }
  else if(keyCode == UP)
  {
    drawSnakeY();
    headY -= Y_SPEED;
  }
  else if(keyCode == DOWN)
  {
    drawSnakeY();
    headY += Y_SPEED;
  }
}

void drawSnakeX()
{
  for(int i = 0; i < 3; i++)
  {
    if(keyCode == RIGHT)
    {
       ellipse(headX - i*SNAKE_SIZE, headY, SNAKE_SIZE, SNAKE_SIZE); 
    }
    else if(keyCode == LEFT)
    {
       ellipse(headX + i*SNAKE_SIZE, headY, SNAKE_SIZE, SNAKE_SIZE);
    }
  }
}

void drawSnakeY()
{
  for(int i = 0; i < 3; i++)
  {
    if(keyCode == UP)
    {
       ellipse(headX, headY + i*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
    }
    else if(keyCode == DOWN)
    {
       ellipse(headX, headY - i*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
    }
  }
}

When posting code, please format it using the </> code button.

Looks like a good start. What does this code do? What’s the next thing you’re trying to add? Where are you stuck?

Its drawing the food object in a random location and the snake moves perfectly as well
Next I want the snake to hit the food object and as it does so it should increase the snake length by 1
Also when the snake eats the food object the object has to be drawn to a new random location

1 Like

Okay. So you need to determine if the snakes head has reached the target. You know where the snake’s head is. You know how big it is. You know where the target is. You know how big it is. What can you say about the distance between the head and the target’s positions in relationship to their sizes when they two things are touching?

When that condition is true, you can increase the snake’s length, and give the target a new random position.

Try just writing the code that does this on it’s own, and post it here so we can work with you on it.

So I did this I dont really understand how I am to approach this

void display() //Displaying the snake and the apple
{
  if (headX==applex && headY==appley)
  {
    snakeSize=round(random(3)+1);
    redo=true;
    while(redo)
    {
      applex=(round(random(47))+1)+8;
      appley=(round(random(47))+1)+8;
      for(int i=1; i<snakeSize; i++)
      {
        if (applex==headX*i && appley== headY*i)
        {
          redo=true;
        }
        else
        {
          redo=false;
          i=1000;
        }
      }
    }
  }
  stroke(255,255,255);
  fill(0);
  ellipse(headX,headY,8,8);
  fill(255);
  ellipse(headX*snakeSize,headY*snakeSize,8,8);
}


void checkdead()  //Ending the game
{
  for(int i=2;i<snakeSize;i++)
  {
    if (headX==headX*i && headY==headY*i)
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("EXIT",200,150);
      text("RESTART",200,175);
      text("PRESS SHIFT TO RESTART",200,200);
      stopgame=true;
    }
  }
}

You have a lot of code and it’s not clear what a lot of it is doing. I would suggest that you put in some comments explaining each and every line.

This is, essentailly, rubber duck coding. Explain your code to yourself (and us!) via comments, and in doing so, you will see what is wrong with it.

Here are some comments to start you off. These are not in order! You will have to put them in the right places in your code…

// Determine if the snake's position the same as the target's position.
// Give the target a new X position.
// Give the target a new Y position.
// Increase the snake's length.
// Draw the snake, each time the loop runs one shape is drawn.
// Draw one shape.
// Draw the target.
// The setup function, which runs once when the sketch starts.
// A reset function, which gives all game variables their initial values.
// The snake starts in the middle of the screen.
// The target has a random position.
// This function determines which way the snake needs to move.
// This moves the snake.

@nofil7, These posts can potentially help you get an idea on how to solve your problem. But, its important to work through the problem solving process.

This is my code so far that draws the snake,moves it and also draws the target.Need help with step 4 and step 5 now as I mentioned in my first post

final int X_SPEED = 1;
final int Y_SPEED = 1;

int headX = 250;
int headY = 250;

int snakeSize=15;

int applex= (round(random(97))+10)+80;
int appley= (round(random(97))+10)+80;

boolean redo=true;
boolean stopgame=false;


void setup()
{
  size(500,500);
}

void draw()
{
  background(192);
  fill(255);
  moveSnake();
  
  if(headX >= 500 || headX <= 0)
  {
    background(0); 
  }
  
  if(headY >= 500 || headY <= 0)
  {
    background(0); 
  }
  
  fill(255,0,0);
   stroke(0);
   rect(applex,appley,8,8);
}

void moveSnake() //For moving the snake
{
  if(keyCode == RIGHT)
  {
    drawSnakeX();
    headX += X_SPEED; 
  }
  else if(keyCode == LEFT)
  {
    drawSnakeX();
    headX -= X_SPEED;
  }
  else if(keyCode == UP)
  {
    drawSnakeY();
    headY -= Y_SPEED;
  }
  else if(keyCode == DOWN)
  {
    drawSnakeY();
    headY += Y_SPEED;
  }
}

void drawSnakeX() //Drawing the snake X-Axis
{
  for(int i = 0 ; i < 3; i++)
  {
    if(keyCode == RIGHT)
    {
       ellipse(headX - i*snakeSize, headY, snakeSize, snakeSize); 
    }
    else if(keyCode == LEFT)
    {
       ellipse(headX + i*snakeSize, headY, snakeSize, snakeSize);
    }
  }
}

void drawSnakeY() //Drawing the snake Y-Axis
{
  for(int i = 0; i < 3; i++)
  {
    if(keyCode == UP)
    {
       ellipse(headX, headY + i*snakeSize, snakeSize, snakeSize);
    }
    else if(keyCode == DOWN)
    {
       ellipse(headX, headY - i*snakeSize, snakeSize, snakeSize);
    }
  }
}

void display() //Displaying the snake and the apple
{
  if (headX==applex && headY==appley)
  {
    snakeSize=round(random(30)+1);
    redo=true;
    while(redo)
    {
      applex=(round(random(97))+10)+80;
      appley=(round(random(497))+10)+80;
      for(int i=0; i<snakeSize; snakeSize++)
      {
        if (applex==headX*i && appley== headY*i)
        {
          redo=true;
        }
        else
        {
          redo=false;
        }
      }
    }
  }
  stroke(255,255,255);
  fill(0);
  ellipse(headX,headY,8,8);
  fill(255);
  ellipse(headX*snakeSize,headY*snakeSize,8,8);
}


void checkdead()  //Ending the game
{
  for(int i=2;i<snakeSize;i++)
  {
    if (headX==headX*i && headY==headY*i)
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("EXIT",200,150);
      text("RESTART",200,175);
      text("PRESS SHIFT TO RESTART",200,200);
      stopgame=true;
    }
  }
}

You have to attempt to do it yourself. Where is your attempt at a checkIfTargetHit() function?

Do the snake head and the target collide only when their X and Y positions are exactly the same?

Consider:

DoesCollide

I have been attempting to do it by myself that is why I asked for your help in the first place.I wrote the code and it doesn’t works

This is my check if snake hits the border function

if(headX >= 500 || headX <= 0)
  {
    background(0); 
  }
  
  if(headY >= 500 || headY <= 0)
  {
    background(0); 
  }

Seriously guys I attempted everything before coming to this forum.If you guys are just asking me to attempt something I already attempted and failed then how is it helping my cause

Please post your entire current code that shows your attempt at everything. So far you have asked us to do your work for you, and claimed you have tried to do it yourself. You need to show us you have attempted it.

Also include a description of exactly what is not working. Which lines of code do you think are the problem? What is your sketch doing (or not doing) that it shouldn’t (or should) be?

I just posted my whole code saying it is not working as far as hitting the target is concerned

I REPEAT AGAIN

The snake moves,target appears,hits the borders and restarts

I want the snake to hit the target and grow

Step 4 includes a direction to write a checkIfTargetHit() function. This function will determine if the snake has hit the target.

None of the code you have posted so far includes this function.

PLEASE ATTEMPT TO WRITE THIS FUNCTION. PLEASE POST YOUR ATTEMPT AT WRITING THIS FUNCTION. WITHOUT SEEING THE CODE OF YOUR ATTEMPT, WE ARE AT A LOSS AS TO HOW TO HELP YOU MAKE IT WORK.

… And this is coming from the forum regular who is the most likely to just write it for you. You’ve got to put some effort in.

Guys, is this too much code for a simple snake game? :

ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();
int speedinv = 4;
int scl = 10;
int hx=1,hy = 1,apx = int(int(random(scl,width-scl))/scl), apy = int(int(random(scl,height-scl))/scl), dir = 1,start = 0;
int[] dx = {0,1,0,-1}; 
int[] dy = {-1,0,1,0};
void setup() {
  size(400,400);
  background(250);
  stroke(250);
  strokeWeight(3);
  for(int i = 0 ; i < width ; i+=scl) {line(0+i,0,0+i,height);};
  for(int i = 0 ; i < height ; i+=scl) {line(0,0+i,width,0+i);};
  x.add(1);
  y.add(1);
}

void draw() {
  background(250);
  stroke(250);
  for(int i = 0 ; i < width ; i+=scl) {line(0+i,0,0+i,height);};
  for(int i = 0 ; i < height ; i+=scl) {line(0,0+i,width,0+i);};
  stroke(0);
  strokeWeight(14);
  line(0,0,width,0);
  line(width,0,width,height);
  line(width,height,0,height);
  line(0,height,0,0);
  strokeWeight(3);
  for(int i = 0 ; i < x.size() ; i++) {
    fill(0,255,0);
    rect(x.get(i)*scl, y.get(i)*scl, scl,scl);
    fill(255,0,0);
    rect(apx*scl,apy*scl,scl,scl);
  }
  
  if (frameCount%speedinv==0) {
  if ((dir == 0) && (hy > 0)) {hy -= 1;};
  if ((dir == 1) && (hx<width/scl-1)) {hx += 1;};
  if ((dir == 2) && (hy<height/scl-1)) {hy += 1;};
  if ((dir == 3) && (hx > 0)) {hx -= 1;};
  x.add(0,x.get(0)+dx[dir]);
  y.add(0,y.get(0)+dy[dir]);
  for(int i=1;i<x.size();i++) {if(x.get(0)==x.get(i)&&y.get(0)==y.get(i)) {exit();}};
  
  
  
  if (hx == apx && hy == apy) {
  apx = int(int(random(scl,width-scl))/scl);
  apy = int(int(random(scl,height-scl))/scl);
  } else {
    x.remove(x.size()-1);
    y.remove(y.size()-1);
  }
  
 } if(hx - 1 == -1) {exit();};if(hy - 1 == -1) {exit();};if(hx - (width/scl-2) > 0) {exit();};if(hy - (height/scl-2) > 0) {exit();};
} void keyPressed() {
  if ((key == 'w') && (dir != 2) && (hy > 0)) {dir = 0;};if ((key == 'd') && (dir != 3) && (hx<width/scl-1)) {dir = 1;};if ((key == 's') && (dir != 0) && (hy<height/scl-1)) {dir = 2;};if ((key == 'a') && (dir != 1) && (hx > 0)) {dir = 3;};
}

Is This The Final Completed Product