Snake Game Code


#1

Snake Code assignment


#2

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?


#3

#4

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 - iSNAKE_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 + iSNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
else if(keyCode == DOWN)
{
ellipse(headX, headY - i
SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
}
}


#5

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?


#6

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


#7

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.


#8

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

#9

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.

#10

@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.


#11

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

#12

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


#13

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


#14

This is my check if snake hits the border function

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

#15

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


#16

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?


#17

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


#18

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.