Snake game questions

What to do next?

int snakeX=370;
int snakeY=370;
int foodX=int(random(1,780));
int foodY=int(random(1,780));
int score=1;
boolean up=false;
boolean down=false;
boolean right=false;
boolean left=false;
void setup(){
  size(800,800);
}
void draw(){
  fill(0);
  stroke(255);
  rect(-1,-1,801,801);
  rect(snakeX,snakeY,29,29);
  fill(255);
  stroke(255);
  rect(foodX,foodY,19,19);
  fill(0);
  if (up==true && down==false && right==false && left==false){
    snakeY-=1;
  }
  if (down==true && up==false && right==false && left==false){
    snakeY+=1;
  }
  if (right==true && up==false && down==false && left==false){
    snakeX+=1;
  }
  if (left==true && up==false && down==false && right==false){
    snakeX-=1;
  }
  if (up==true){
    if (snakeX<foodX+20 && snakeX+30>foodX+20 && snakeY==foodY+20 || snakeX<foodX && snakeX+30>foodX && snakeY==foodY+20){
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
    }
    if (down==true){
    if (snakeX<foodX+20 && snakeX+30>foodX+20 && snakeY+30==foodY || snakeX<foodX && snakeX+30>foodX && snakeY+30==foodY){
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
    }
  }
  if(up==true){
  rect(snakeX,snakeY,30,score*30);
  }
  if(down==true){
  rect(score*snakeX,snakeY,30,30);
  }
  }
}
void keyPressed()
{
  if  (key == CODED)
  {
    if (keyCode == UP)
    {
      if(down==false){
      left=false;
      right=false;
      up=true;
      }
    }
    if (keyCode == DOWN)
    {
      if(up==false){
      up=false;
      left=false;
      right=false;
      down=true;
      }
    }
        if (keyCode == RIGHT)
    {
      if(left==false){
      up=false;
      down=false;
      left=false;
      right=true;
      }
    }
    if (keyCode == LEFT)
    {
      if(right==false){
      up=false;
      down=false;
      right=false;
      left=true;
      }
    }
  }
}

Ok, sorry, just realized it didn't paste the whole code, here it is.

Tagged:

Answers

  • look, don't get frustrated

    I think you need keyPressed to get key input cursor up down etc.

    Look at reference for keyPressed and keyCode

    then set your up and down etc. accordingly

    now I see you are checking the snake head aginst the food. Good. But do it always not only when up is true. You could also use dist instead, See Reference.

    Now when you ate, give foodX and y a new random pos. foodX=random(0,width); And say score++; declare score at the top

    And you need to shorten your sknake, right? Not sure.... you could have all Segments in a array

  • Maybe use background(0); at start of draw()

  • So I used dist and put background at the beginning of draw(), here it is:

    int snakeX=370;
    int snakeY=370;
    int foodX=int(random(1,780));
    int foodY=int(random(1,780));
    int score=1;
    boolean up=false;
    boolean down=false;
    boolean right=false;
    boolean left=false;
    void setup(){
      size(800,800);
    }
    void draw(){
      background(0);
      fill(0);
      stroke(255);
      rect(-1,-1,801,801);
      rect(snakeX,snakeY,29,29);
      fill(255);
      stroke(255);
      rect(foodX,foodY,19,19);
      fill(0);
      if (up==true && down==false && right==false && left==false){
        snakeY-=1;
      }
      if (down==true && up==false && right==false && left==false){
        snakeY+=1;
      }
      if (right==true && up==false && down==false && left==false){
        snakeX+=1;
      }
      if (left==true && up==false && down==false && right==false){
        snakeX-=1;
      }
      if(dist(snakeX+15,snakeY+30,foodX+10,foodY)==0 || dist(snakeX+15,snakeY,foodX+10,foodY+20)==0 || dist(snakeX,snakeY+15,foodX+20,foodY+10)==0 || dist(snakeX+30,snakeY+15,foodX,foodY+10)==0){
      rect(snakeX,snakeY,30,score*30);
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
      }
    }
    void keyPressed()
    {
      if  (key == CODED)
      {
        if (keyCode == UP)
        {
          if(down==false){
          left=false;
          right=false;
          up=true;
          }
        }
        if (keyCode == DOWN)
        {
          if(up==false){
          up=false;
          left=false;
          right=false;
          down=true;
          }
        }
            if (keyCode == RIGHT)
        {
          if(left==false){
          up=false;
          down=false;
          left=false;
          right=true;
          }
        }
        if (keyCode == LEFT)
        {
          if(right==false){
          up=false;
          down=false;
          right=false;
          left=true;
          }
        }
      }
    }
    
  • well done!

    now in line 35 you can insert a line break after each || sign

    line 15 to 17 not needed

    does the snake grow? you need this

    make sure it dies when hits wall or itself

  • The snake growth is the hardest part, do you have any tips? You've been a real help.

  • Google snake in the forum

    in google Enter:

    snake site:forum.processing.org

    Or so

    I think I'd do it with an array but not sure

  • Use text(score, 19,19); to display the score (fill() for its color)

    you could also say lives and say lives--; when you hit a wall or itself

  • You should use empty lines between the functions

    And in other places

    Ctrl-t makes auto-indents - good!

    Also you can make functions and put lines from draw() into them

    Eg. manageSnake() could be one function for the lines 23 to 34

  • Ok this is still screwed up, for some reason it's not registering an "eat". Here's the newest code

    int snakeX=370;
    int snakeY=370;
    int foodX=int(random(1,780));
    int foodY=int(random(1,780));
    int score=1;
    boolean up=false;
    boolean down=false;
    boolean right=false;
    boolean left=false;
    void setup(){
      size(800,800);
    }
    void draw(){
      background(0);
      rect(snakeX,snakeY,29,29);
      fill(255);
      stroke(255);
      rect(foodX,foodY,19,19);
      fill(0);
      if (up==true && down==false && right==false && left==false){
        snakeY-=1;
      }
      if (down==true && up==false && right==false && left==false){
        snakeY+=1;
      }
      if (right==true && up==false && down==false && left==false){
        snakeX+=1;
      }
      if (left==true && up==false && down==false && right==false){
        snakeX-=1;
      }
      if(dist(snakeX+15,snakeY+30,foodX+10,foodY)==0 ||
      dist(snakeX+15,snakeY,foodX+10,foodY+20)==0 ||
      dist(snakeX,snakeY+15,foodX+20,foodY+10)==0 ||
      dist(snakeX+30,snakeY+15,foodX,foodY+10)==0){
      rect(snakeX,snakeY,30,score*30);
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
      }
    }
    void keyPressed()
    {
      if  (key == CODED)
      {
        if (keyCode == UP)
        {
          if(down==false){
          left=false;
          right=false;
          up=true;
          }
        }
        if (keyCode == DOWN)
        {
          if(up==false){
          up=false;
          left=false;
          right=false;
          down=true;
          }
        }
            if (keyCode == RIGHT)
        {
          if(left==false){
          up=false;
          down=false;
          left=false;
          right=true;
          }
        }
        if (keyCode == LEFT)
        {
          if(right==false){
          up=false;
          down=false;
          right=false;
          left=true;
          }
        }
      }
    }
    
  • use dist...... with < 20

  • I looked up how to do that the snake gets longer

    you can use 2 arrays (x and y position for each segment) which holds x and y position and you append one new segment (in x and y) and you copy / shift all elements one step forward

    before setup()

    int []snakesX;
    int []snakesY;
    int snakeSize = 1;
    

    in setup()

      snakesX = new int[100];
      snakesY = new int[100];
    

    this is somewhere down but must be called from draw()

    void drawSnake() {
      fill(col);
    
      for (int i = 0; i < snakeSize; i++) {
        int X = snakesX[i];
        int Y = snakesY[i];
        rect(X-5, Y-5, 10, 10);
      }
      if (!paused) { 
        for (int i = snakeSize; i > 0; i--) {
          snakesX[i] = snakesX[i-1];
          snakesY[i] = snakesY[i-1];
        }
      }
    }
    
    void snakeMove() {
      snakeX += moveX;
      snakeY += moveY;
      if (snakeX > windowSize-5 || snakeX < 5||
        snakeY > windowSize-5||snakeY < 5) {
        gameOver = true;
      }
      snakesX[0] = snakeX;
      snakesY[0] = snakeY;
    }
    
  • edited May 2016

    Did you figure it out? Do you need more help?

    I think that if you focus on one thing at a time you'll be able to make sure one part of the game works before you add another. So make sure your core function of the game works (moving around the snake).

    The way you are moving your snake around now is by changing the coordinates of the snake. But if you do it like that it will look a bit weird when you make a turn. So what you need is for the snake to a chain. And each part of the chain is headed towards the part in front of it. And when we control the snake, we just control the head.

    So let's make snakeX and snakeY into two arrays. Before setup() we say: float snakeX[] = new snakeX[4]; and float snakeY[] = new snakeY[4]; Now we have a snake with four parts, but we need to set a start-location. We do that in setup() like this:

    snakeX[0] = 370;
    snakeY[0] = 370;
    for(int i = 1; i<snakeX.length;i++){ //snakeX and snakeY are the same length.
      snakeX[i] = snakeX[0];  //Because each part is following the next, the snake will
      snakeY[i] = snakeY[0]; //unfold as soon as it starts moving.
    }
    

    Now we'll start drawing the snake, but in order to keep things nice and ordered I think it's best to move the snake-drawing out of draw and in to a new function that we can call.. snake()? In snake() we will draw a rect at each coordinate and we'll make the snake move. The first thing we'll move is the head of the snake (snakeX[0] and snakeY[0]) towards a new point. And we never want the snake to reach that point. We'll move it around forever. or until the snake dies.

    So snakeX[0] = 370. The new point we'll call dirX and we'll set it to 375. The distance between snakeX[0] and dirX can be calculated like this: float distX = snakeX[0] - dirX; and if we then set snakeX[0]'s new location like this: snakeX[0] = snakeX[0] - distX;, we'll appear at the new point. But we don't want to appear at the new point, we want to move towards it. So we'll say it like this:

    float distX = snakeX[0] - dirX;
    snakeX[0] = snakeX[0] - distX/5;
    

    Now that we are moving toward the new point we need to make sure the rest of the body is following. We can use a for-loop and do it like this:

    for (int i=0; i<bodyX.length; i++) {
        float distX, distY;
        if (i==0) {
          distX = snakeX[0] - dirX; 
          distY = snakeY[0] - dirY;
        } else {
          distX = snakeX[i] - snakeX[i-1]; 
          distY = snakeY[i] - snakeY[i-1];
        }
        snakeX[i] = snakeX[i] - distX/5;
        snakeY[i] = snakeY[i] - distY/5;
        ellipse(bodyX[i], bodyY[i], 10, 10);
      }
    

    Now I would make a new variable called addX and addY and then add them to the dirX and dirY in snake(). If you want to change the direction of the snake you simply change addX and addY.

    Here is what I have done for you so far:

    float [] snakeX = new float [10];
    float [] snakeY = new float [10];
    float dirX = 370, dirY = 370, addX = 1, addY =0;
    boolean dirX = true, dirY = false;
    void setup() {
      size(800, 600);
      snakeX[0] = dirX; 
      snakeY[0] = dirY;
      for (int i = 1; i<snakeX.length; i++) { //snakeX and snakeY are the same length.
        snakeX[i] = snakeX[0];  //Because each part is following the next, the snake will
        snakeY[i] = snakeY[0]; //unfold as soon as it starts moving.
      }
    }
    
    void draw() {
      background(100);
      snake();
    }
    
    void snake() {
      fill(0);
      for (int i=0; i<snakeX.length; i++) {
        ellipse(snakeX[i], snakeY[i], 10, 10);
        float distX, distY;
        if (i==0) {
          distX = snakeX[0] - dirX; 
          distY = snakeY[0] - dirY;
        } else {
          distX = snakeX[i] - snakeX[i-1]; 
          distY = snakeY[i] - snakeY[i-1];
        }
        snakeX[i] = snakeX[i] - distX/5;
        snakeY[i] = snakeY[i] - distY/5;
        //println(snakeX[i]);
      }
      dirX += addX;
      dirY += addY;
    }
    

    I know these are pretty radical changes, so I understand if you don't want to use them, but hopefully they have given you some new ideas or you've learned something new.

Sign In or Register to comment.