I cannot for the life of me get this snake game to run right, can anyone help?

Problem solved, please delete thread.

Tagged:

Answers

  • Edit post, higlight code, ctrl + o

  • Sorry, new to this :|

  • Answer ✓

    Figured it all out, feel free to delete this thread :)

  • I cannot for the life of me get this snake game to run right, can anyone help? Solved I have two issues:

    1) I've tried to create a new array to increase the size of the snake by 1 whenever the snake collides with the image of fruit, but it only seems to be adding 1-2 pieces before it stops working.

    2) This seems a common problem for people making a snake game, where the snakes body doesn't follow the head. I thought with my most recent change I had fixed it, however it created a new problem where for some reason a second snake sometimes appears on the screen... I have no idea why.

    Can anyone help me? This is my code if that helps, but I'm new to processing so it's probably got more wrong with it than is apparent to me....

    int score;
    int dir;
    float [][] bodyParts = new float[2][4];
    float xLocation;
    float yLocation;
    float headX;
    float headY;
    float xVel;
    float yVel;
    float game;
    float fruitX;
    float fruitY;
    PImage fruit;
    boolean changing;
    
    void setup() {
      frameRate(10); //change the rate of how many frames are drawn per second
      size(1200, 600); //set size of screen
      background(0, 200, 0); //set background color to green
      fruit = loadImage("Strawberry.jpg"); //load image of fruit
      fruit.resize(10, 10); //make fruit same size as snake
      yVel = -10;
      xVel = 0;
      headX = width/2;
      headY = 3*(height/4);
      score = 0;
      game = 1;
      fruitX = (int) random(width-10);
      fruitY = (int) random(height-10);
    }
    
    void draw() {
      background(0, 200, 0); //clear the background;
      makeFruit(); //draws function that places fruit on screen
      makeSnake(); //draws function that creates the snake
      checkGame(); //checks if the game is over or not
      textSize(20);
      text("Score = " + score, 5, 20);
    }
    
    void makeFruit() {
      for (int i=0; i<bodyParts.length; i++) {
        if (dist(headX, headY, fruitX, fruitY) >= 10) { //if fruit already on screen, don't draw another
          image(fruit, fruitX, fruitY);
        } else if (dist(headX, headY, fruitX+2, fruitY+1) < 10) {
          score++;
          float [][] temp = new float[bodyParts.length+1][4];
          for (int j=0; j<temp.length-1; j++) {
            for (int k=0; k<temp[j].length; k++) {
              temp[j][k] = bodyParts[j][k];
            }
          }
          temp[temp.length-1][0] = bodyParts[bodyParts.length-1][0];
          temp[temp.length-1][1] = bodyParts[bodyParts.length-1][1];
          temp[temp.length-1][2] = bodyParts[bodyParts.length-1][2];
          bodyParts = temp;
    
          fruitX = (int) random(width-10);
          fruitY = (int) random(height-10);
        }
      }
    }
    void makeSnake() {
      fill(255);
      ellipse(headX, headY, 10, 10); //draw snake head
      headX = headX + xVel;
      headY = headY + yVel;
    
      for (int i=1; i<bodyParts.length; i++) { 
        bodyParts[1][0] = xLocation;
        bodyParts[i][1] = yLocation;
        bodyParts[i][2] = dir;
        bodyParts[i][3] = 0;
        if (bodyParts[i][2] == bodyParts[i-1][2]) {
          changing = false;
          ellipse(bodyParts[i][0], bodyParts[i][1], 10, 10); //draw the i'th body part
        } else {
          changing = true;
          followHead();
        }
        if (changing==true) {
          bodyParts[i][2] = bodyParts[i-1][2];
        }
        ellipse(headX-xVel, headY-yVel, 10, 10);
        xLocation = headX-xVel;
        yLocation = headY-yVel;
      }
    }
    
    void followHead() {
      for (int i=1; i<bodyParts.length; i++) { 
        if (dir==1||dir==2||dir==3||dir==4) {
          ellipse(bodyParts[i][0]-xVel, bodyParts[i][1]-yVel, 10, 10);
        }
      }
    }
    
    void checkGame() {
      if (game == 0 || headX <3 || headY <3 || headX > width - 3 || headY > height - 3) {
        game = 0; //game over
        xVel=0;
        yVel=0;
        textSize(30);
        text("Game Over", width/2 - 80, height/2);
      }
      for (int i=0; i<bodyParts.length; i++) {
        if (dist(headX, headY, bodyParts[i][0], bodyParts[i][1]) < 10) {
          game = 0;
        }
    
        // else game = 1; //allows game to continue if player doesn't meet conditions, only used for testing purposes, don't put in final product
      }
    }
    
    void keyPressed() {
      if (key==CODED) {
        if (keyCode == LEFT && xVel != 10) {
          dir=1;
          changing = true;
          xVel = -10;
          yVel = 0;
        }
        if (keyCode == RIGHT && xVel != -10) {
          dir=2;
          changing = true;
          xVel = 10;
          yVel = 0;
        }
        if (keyCode == UP && yVel != 10) {
          dir=3;
          changing = true;
          xVel = 0;
          yVel = -10;
        }
        if (keyCode == DOWN && yVel != -10) {
          dir=4;
          changing = true;
          xVel = 0;
          yVel = 10;
        }
      }
    }
    
This discussion has been closed.