Game wont resume

So I have a somewhat simple snake game code here:

ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();
int w=30, h=30, blocks=20, direction = 2, foodx =15, foody = 15, fcl1=255, fcl2=255, fcl3=255, speed = 10;
int[]x_direction={0, 0, 1, -1}, y_direction={1, -1, 0, 0};
boolean gameover=false;
boolean pause=false;
PFont zigBlack;
PImage img;
int foods;
int randomnumber = (int)random(0);
int newdir;
String hud = "E";
String hud2 = "1";


void setup() {
  size(600, 600);
  img = loadImage("Food" + randomnumber + ".png"); //didn't work bruh
  x.add(0); 
  y.add(15);
  //font
  zigBlack = createFont("Minecraftia 2.0", 32);
  textFont(zigBlack);
  fill(0);
 
}

void draw() {
  background(159, 222, 0);
  fill(217, 125, 4);
  for (int i = 0; i< x.size(); i++) rect(x.get(i)*blocks, y.get(i)*blocks, blocks, blocks);
  if (!gameover) {
    fill(fcl1, fcl2, fcl3); //food color
    //rect(foodx*blocks, foody*blocks, blocks-5, blocks-5); //food (blocks)
    image(img,foodx*blocks, foody*blocks, blocks, blocks-5); //food (hamburger) 
    if (pause==true){
     fill(0, 140);
     rect(0,410,600,170);
     fill(255);
     textSize(35);
     text("     Game",10,500);
     text("  Paused",10,550);
     frameRate(0);
    }
    //speed hud 
    fill(0, 140);
    rect(463,40,130,24);
    fill(255, 200);
    textSize(18);
    text("Speed: " + hud2,467,72);
    if (speed==9) hud2 = "2";
    if (speed==8) hud2 = "3";
    if (speed==7) hud2 = "4";
    if (speed==6) hud2 = "5";
    if (speed==5) hud2 = "6";
    if (speed==4) hud2 = "7";
    if (speed==3) hud2 = "8";
    if (speed==2) hud2 = "MAX";
    //direction hud 
    fill(0, 140);
    rect(463,10,130,24);
    fill(255, 200);
    textSize(18);
    text("Direction: " + hud,467,42);
    if (newdir == 1) hud = "N";
    if (newdir == 0) hud = "S";
    if (newdir == 2) hud = "E";
    if (newdir == 3) hud = "W";
    //score back rectangle (extends if score is 2 digits) 
    if (x.size() < 10){ 
      fill(0, 140);
      rect(5,10,100,24);
    }
    if (x.size() > 9){
      fill(0, 140); 
      rect(5,10,115,24);
    }
    textAlign(LEFT); //score
    textSize(18);
    fill(255);
    text("Score: " + x.size(), 10, 10, width - 20, 50);
    if (frameCount%speed==0) {
      x.add(0, x.get(0) + x_direction[direction]);  
      y.add(0, y.get(0) + y_direction[direction]);
      if (x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >=h) gameover=true;
      for (int i = 1; i<x.size(); i++)
      if (x.get(0)==x.get(i)&&y.get(0)==y.get(i)) gameover=true;
      if (x.get(0)==foodx && y.get(0)== foody) {
        if (x.size()%5==0 && speed>=2) speed-=1; //speed increase
        //don't need these but they are still here anyways
        foodx = (int)random(0, w);
        foody = (int)random(0, h);
        fcl1 = (int)random(255);
        fcl2 = (int)random(255);
        fcl3 = (int)random(255);
      } else {
        x.remove(x.size()-1);
        y.remove(y.size()-1);
      }
    }
  } else{
    //gameover screen
    fill(0, 140);
    rect(0,135,600,180);
    fill(255);
    textSize(27);
    textAlign(CENTER);
    text("Game Over \n Your Score: " + x.size() +"\n Press Enter To Start Again", width/2, height/3);
    if (keyCode == ENTER){
      x.clear();
      y.clear();
      x.add(0);
      y.add(15);
      direction = 2;
      speed = 10;
      hud2 = "1";
      hud = "E";
      gameover = false;
    }
  
}}

void keyPressed() {
  newdir=keyCode == DOWN? 0:(keyCode== UP? 1:(keyCode== RIGHT? 2:(keyCode== LEFT? 3:-1)));
  if (newdir != -1) direction = newdir;
  if(key == TAB)
    pause = !pause;
  if(newdir == 1) //here also didn't work for some reason
    newdir=keyCode == DOWN? 1:(keyCode== UP? 1:(keyCode== RIGHT? 2:(keyCode== LEFT? 3:-1)));
    
}


You need this image and the Minecraftia 2.0 font (https://www.dafont.com/minecraftia.font) to run it.

What I want is to pause the game when I press tab.

  if (pause==true){
     fill(0, 140);
     rect(0,410,600,170);
     fill(255);
     textSize(35);
     text("     Game",10,500);
     text("  Paused",10,550);
     frameRate(0);

this is the part of the code that needs to pause the game. It pauses it but I can’t resume it.

void keyPressed() {
  if(key == TAB)
    pause = !pause;

this is the part of the code which toggles the pause boolean.
Is using frameRate(0); a bad idea?
Is there a way to make this work or do I need to try something else?

1 Like

Replace it by return;

Or add

if(pause == false)
frameRate(100);

I already tried

if(pause == false)
frameRate(100);

multiple times but it doesn’t work. Can you explain what you mean by replace it by return?

And then insert return;

It ends the draw function and restarts it

2 Likes

It works beatifully now. Thanks!

1 Like

Delete this…

Chrisir

yep, replaced it with return;

1 Like