Need help with a restart button for my Snake Game please

I need help understanding how to make a restart button on my snake game. I want it to be the Shift key but it won't allow me to so I made the restart key to be 'r' and it still doesn't work.

//MY CODE

int WIDTH = 700;

int HEIGHT = 600;

int SQSIZE = 10;

int x = 350;

int y = 300;

int foodx = 500;

int foody = 80;

int xspeed, yspeed;

int speed = SQSIZE;

int [] bodyx = new int [80];

int [] bodyy = new int [80];

int score = 0;

int endgame;

int r;

boolean restart = false;

void setup()

{

smooth();

frameRate(14);

size (WIDTH, HEIGHT);

x = 350;

y = 300;

for (int i = 0; i < bodyx.length; i++)

{

bodyx [i] = x;

bodyy [i] = y;

}

}

void draw() { background(76, 142, 54, 105);

//lines

for (int i = 0; i < 600; i = i+10)

{

stroke(203, 240, 191, 105);

line(0, i, 700, i);

}

for (int i = 0; i < 700; i = i+10)

{

line(i, 0, i, 600);

}

//SNAKE

stroke(x, y, 0);

fill(x, y, 255);

rect(x, y, SQSIZE, SQSIZE);

//FOOD

fill(133, 33, 250, 267);

rect(foodx, foody, SQSIZE, SQSIZE);

//SCORE

textSize(30);

fill(255);

text("Score: "+ score, 500, 30);

//food

if ((x == foodx) && (y == foody))

{

foodx = int(random(2, 68))*SQSIZE;

foody = int(random(2, 58))*SQSIZE;

rect(foodx, foody, SQSIZE, SQSIZE);

score = score + 1;

}

bodyx[score+1] = x;

bodyy[score+1] = y;

//Body expanding every time food is eaten

for (int i = 0; i < score+1; i++)

{

bodyx[i] = bodyx[i+1];

bodyy[i] = bodyy[i+1];

stroke(x, y, 0);

fill (x, y, 255);

rect(bodyx[i], bodyy[i], SQSIZE, SQSIZE);


if ((x == bodyx[i]) && (y == bodyy[i]) && i != score)

{ 

  noLoop();

  speed = 0;


  fill(157, 167, 255, 255);

  rect(100, 175, 500, 200);


  textSize(50);

  fill(0);

  text("GAME OVER", 225, 300);


  textSize(30);

  fill(0);

  text("Press Shift to resart", 225, 325);

}

}

/////////////////////

x = x + xspeed;

y = y + yspeed;

//ENDGAME

if ((x > width-SQSIZE) || (x < +0))

{

xspeed = xspeed * 0;

speed = 0;

fill(157, 167, 255, 255);

rect(100, 175, 500, 200);

textSize(50);

fill(0);

text("GAME OVER", 225, 300);

textSize(30);

fill(0);

text("Press 'r' to resart", 225, 325);

}

if ((y > height-SQSIZE) || (y < +0))

{

yspeed = yspeed * 0;

speed = 0;

fill(157, 167, 255, 255);

rect(100, 175, 500, 200);

textSize(50);

fill(0);

text("GAME OVER", 225, 300);

textSize(30);

fill(0);

text("Press 'r' to resart", 225, 325);

}

}

void restart()

{

if (restart == false)

{

//SNAKE

stroke(x, y, 0);

fill(x, y, 255);

rect(x, y, SQSIZE, SQSIZE);

//FOOD

fill(133, 33, 250, 267);

rect(foodx, foody, SQSIZE, SQSIZE);

//SCORE

textSize(30);

fill(255);

text("Score: "+ score, 500, 30);

}

else

{

textSize(50);

fill(0);

text("GAME OVER", 225, 300);



textSize(30);

fill(0);

text("Press 'r' to resart", 225, 325);

}

}

void keyPressed()

{

if (keyCode == LEFT)

{

xspeed = -speed;

yspeed = 0;

}

if (keyCode == RIGHT)

{

xspeed = speed;

yspeed = 0;

}

if (keyCode == UP)

{

yspeed = -speed;

xspeed = 0;

}

if (keyCode == DOWN)

{

yspeed = speed;

xspeed = 0;

}

if(keyCode == 'r')

{

restart();

} }

Answers

  • int x;
    int y; 
    
    void setup(){
      size(220,220);
      reset();
    }
    
    void draw(){
      background(0);
      x++;
      rect(x,x,y,y);
    }
    
    void keyPressed(){
      if(key=='r') reset();
    }
    
    void reset(){
      x = 5;
      y = int(random(5,20));
    }
    

    Your sketch's state is entirely defined by the variables that store the information that your sketch needs to remember. To reset the state to how it was at the start, simply change the value stored in all your variables back to what they were when you sketch started.

  • edited February 2015

    So I did everything and I went to play the processing and it says 'Duplicate field SNAKE_GAME.WIDTH and it's highlighting the 'int WIDTH = 700;' What does that mean?

    @TfGuy44

  • See also How to format code and text...

    About WIDTH: no idea why you have this error, but Processing practice is to define numbers values in size(), and to use built-in width and height variables...

  • Answer ✓

    you probably have this line twice:

    'int WIDTH = 700;'

    Anyway, show your new entire code please

  • int WIDTH = 700;

    int HEIGHT = 600;

    int SQSIZE = 10;

    int x = 350;

    int y = 300;

    int foodx = 500;

    int foody = 80;

    int xspeed, yspeed;

    int speed = SQSIZE;

    int [] bodyx = new int [80];

    int [] bodyy = new int [80];

    int score = 0;

    int endgame;

    int r;

    boolean restart = false;

    void setup()

    {

    smooth();

    reset();

    frameRate(14);

    size (WIDTH, HEIGHT);

    x = int(random(2, 68))*SQSIZE;

    y = int(random(2, 68))*SQSIZE;

    for (int i = 0; i < bodyx.length; i++)

    {

    bodyx [i] = x;
    
    bodyy [i] = y;
    

    }

    }

    void draw()

    {

    background(76, 142, 54, 105);

    //lines

    for (int i = 0; i < 600; i = i+10)

    {

    stroke(203, 240, 191, 105);
    
    line(0, i, 700, i);
    

    }

    for (int i = 0; i < 700; i = i+10)

    {

    line(i, 0, i, 600);
    

    }

    //SNAKE

    stroke(x, y, 0);

    fill(x, y, 255);

    rect(x, y, SQSIZE, SQSIZE);

    //FOOD

    fill(133, 33, 250, 267);

    rect(foodx, foody, SQSIZE, SQSIZE);

    //SCORE

    textSize(30);

    fill(255);

    text("Score: "+ score, 500, 30);

    //food

    if ((x == foodx) && (y == foody))

    {

    foodx = int(random(2, 68))*SQSIZE;
    
    foody = int(random(2, 58))*SQSIZE;
    
    rect(foodx, foody, SQSIZE, SQSIZE);
    
    score = score + 1;
    

    }

    bodyx[score+1] = x;

    bodyy[score+1] = y;

    //Body expanding every time food is eaten

    for (int i = 0; i < score+1; i++)

    {

    bodyx[i] = bodyx[i+1];
    
    bodyy[i] = bodyy[i+1];
    
    stroke(x, y, 0);
    
    fill (x, y, 255);
    
    rect(bodyx[i], bodyy[i], SQSIZE, SQSIZE);
    
    
    if ((x == bodyx[i]) && (y == bodyy[i]) && i != score)
    
    { 
    
      noLoop();
    
      speed = 0;
    
    
      fill(157, 167, 255, 255);
    
      rect(100, 175, 500, 200);
    
    
      textSize(50);
    
      fill(0);
    
      text("GAME OVER", 225, 300);
    
    
      textSize(30);
    
      fill(0);
    
      text("Press Shift to resart", 225, 325);
    
    }
    

    }

    /////////////////////

    x = x + xspeed;

    y = y + yspeed;

    //ENDGAME

    if ((x > width-SQSIZE) || (x < +0))

    {

    xspeed = xspeed * 0;
    
    speed = 0;
    
    
    
    fill(157, 167, 255, 255);
    
    rect(100, 175, 500, 200);
    
    
    textSize(50);
    
    fill(0);
    
    text("GAME OVER", 225, 300);
    
    
    textSize(30);
    
    fill(0);
    
    text("Press 'r' to resart", 225, 325);
    

    }

    if ((y > height-SQSIZE) || (y < +0))

    {

    yspeed = yspeed * 0;
    
    speed = 0;
    
    
    fill(157, 167, 255, 255);
    
    rect(100, 175, 500, 200);
    
    
    textSize(50);
    
    fill(0);
    
    text("GAME OVER", 225, 300);
    
    
    textSize(30);
    
    fill(0);
    
    text("Press 'r' to resart", 225, 325);
    

    }

    }

    void keyPressed()

    {

    if (keyCode == LEFT)

    {

    xspeed = -speed;
    
    yspeed = 0;
    

    }

    if (keyCode == RIGHT)

    {

    xspeed = speed;
    
    yspeed = 0;
    

    }

    if (keyCode == UP)

    {

    yspeed = -speed;
    
    xspeed = 0;
    

    }

    if (keyCode == DOWN)

    {

    yspeed = speed;
    
    xspeed = 0;
    

    }

    if (keyCode == 'r')

    {

    reset();
    

    }

    }

    void reset()

    {

    WIDTH = 700;

    HEIGHT = 600;

    x = int(random(2, 70))*SQSIZE;

    y = int(random(2, 70))*SQSIZE;

    foodx = int(random(2, 68))*SQSIZE;

    foody = int(random(2, 58))*SQSIZE;

    score = 0;

    }

  • I got it working...

    you must check r with this line

         if (key == 'r')
    

    (use key please, not keyCode)

    in reset() you also must say speed = SQSIZE;

  • int WIDTH = 700;
    
    int HEIGHT = 600;
    
    int SQSIZE = 10;
    
    int x = 350;
    
    int y = 300;
    
    int foodx = 500;
    
    int foody = 80;
    
    int xspeed, yspeed;
    
    int speed = SQSIZE;
    
    int [] bodyx = new int [80];
    
    int [] bodyy = new int [80];
    
    int score = 0;
    
    int endgame;
    
    int r;
    
    // boolean restart = false;
    
    
    void setup()
    
    {
      size (WIDTH, HEIGHT);
    
      smooth();
    
      reset();
    
      frameRate(14);
    
    
      x = int(random(2, 68))*SQSIZE;
    
      y = int(random(2, 68))*SQSIZE;
    
      for (int i = 0; i < bodyx.length; i++)
    
      {
    
        bodyx [i] = x;
    
        bodyy [i] = y;
      }
    }
    
    void draw()
    
    {
    
      background(76, 142, 54, 105);
    
      //lines
    
      for (int i = 0; i < 600; i = i+10)
    
      {
    
    
        stroke(203, 240, 191, 105);
    
        line(0, i, 700, i);
      }
    
      for (int i = 0; i < 700; i = i+10)
    
      {
    
        line(i, 0, i, 600);
      }
    
      //SNAKE
    
      stroke(x, y, 0);
    
      fill(x, y, 255);
    
      rect(x, y, SQSIZE, SQSIZE);
    
      //FOOD
    
      fill(133, 33, 250, 267);
    
      rect(foodx, foody, SQSIZE, SQSIZE);
    
      //SCORE
    
        textSize(30);
    
      fill(255);
    
      text("Score: "+ score, 500, 30);
    
      //food
    
      if ((x == foodx) && (y == foody))
    
      {
    
        foodx = int(random(2, 68))*SQSIZE;
    
        foody = int(random(2, 58))*SQSIZE;
    
        rect(foodx, foody, SQSIZE, SQSIZE);
    
        score = score + 1;
      }
    
      bodyx[score+1] = x;
    
      bodyy[score+1] = y;
    
      //Body expanding every time food is eaten
    
      for (int i = 0; i < score+1; i++)
    
      {
    
        bodyx[i] = bodyx[i+1];
    
        bodyy[i] = bodyy[i+1];
    
        stroke(x, y, 0);
    
        fill (x, y, 255);
    
        rect(bodyx[i], bodyy[i], SQSIZE, SQSIZE);
    
    
        if ((x == bodyx[i]) && (y == bodyy[i]) && i != score)
    
        {
    
          // noLoop();
    
          speed = 0;
    
    
          fill(157, 167, 255, 255);
    
          rect(100, 175, 500, 200);
    
    
          textSize(50);
    
          fill(0);
    
          text("GAME OVER", 225, 300);
    
    
          textSize(30);
    
          fill(0);
    
          text("Press Shift to restart", 225, 325);
        }
      }
    
      /////////////////////
    
      x = x + xspeed;
    
      y = y + yspeed;
    
      //ENDGAME
    
      if ((x > width-SQSIZE) || (x < +0))
    
      {
    
    
        xspeed = xspeed * 0;
    
        speed = 0;
    
    
    
        fill(157, 167, 255, 255);
    
        rect(100, 175, 500, 200);
    
    
        textSize(50);
    
        fill(0);
    
        text("GAME OVER", 225, 300);
    
    
        textSize(30);
    
        fill(0);
    
        text("Press 'r' to restart", 225, 325);
      }
    
      if ((y > height-SQSIZE) || (y < +0))
    
      {
    
    
        yspeed = yspeed * 0;
    
        speed = 0;
    
    
        fill(157, 167, 255, 255);
    
        rect(100, 175, 500, 200);
    
    
        textSize(50);
    
        fill(0);
    
        text("GAME OVER", 225, 300);
    
    
        textSize(30);
    
        fill(0);
    
        text("Press 'r' to restart", 225, 325);
      }
    }
    
    void keyPressed()
    
    {
    
      if (keyCode == LEFT)
    
      {
    
    
        xspeed = -speed;
    
        yspeed = 0;
      }
    
      if (keyCode == RIGHT)
    
      {
    
    
        xspeed = speed;
    
        yspeed = 0;
      }
    
      if (keyCode == UP)
    
      {
    
        yspeed = -speed;
    
        xspeed = 0;
      }
    
      if (keyCode == DOWN)
    
      {
    
        yspeed = speed;
    
        xspeed = 0;
      }
    
      if (key == 'r')
    
      {
    
        println("HERE");
        reset();
      }
    }
    
    void reset()
    
    {
    
      WIDTH = 700;
    
      HEIGHT = 600;
    
      x = int(random(2, 70))*SQSIZE;
    
      y = int(random(2, 70))*SQSIZE;
    
      speed = SQSIZE;
    
      foodx = int(random(2, 68))*SQSIZE;
    
      foody = int(random(2, 58))*SQSIZE;
    
      score = 0;
    }
    
  • edited March 2015

    Please, read the page at the link I posted... :-(

Sign In or Register to comment.