Expecting EOF, found else

please format code with </> button * homework policy * asking questions

boolean running = false;
PImage imgbk;
PImage imgm;
PImage albert;
PImage imgnote;
String[] imageNames = { 
  "albert0.jpg", "albert1.jpg"
};
PImage[] images = new PImage[imageNames.length];
int numPeople = 2;
int dx, dy;
int score;
int startTime;
int albertX, albertY, albertW, albertH;


void setup() 
{
  size(850, 700);

  imgbk = loadImage("b.jpg");
  imgm = loadImage("m.jpg");
  imgnote = loadImage("n.png");
  for (int i = 0; i < imageNames.length; i=i++)
  { 
    String imageName = imageNames[i];
    images[i] = loadImage(imageName);
  }

  albertW = 150;
  albertH = 150;
  albertX = int(random(0, width-albertW));
  albertY = int(random(0, height/2-albertH));


  dx=5;
  dy=5;

  startTime = millis();
  score = 0;
}


void draw() {
  {
    fill(0);
    if ( score == -10) {
      textSize(50);
      fill(255, 0, 0);
      text("You Lose!", 150, 270);
    }
   else  if (running) {
        image(imgm, 0, 0, width, height);
        image(imgnote, mouseX, mouseY, 100, 130);
        float r = random(0, 2); 
        int ir = int(r);
        PImage img = images[ir]; 
        image(img, 0, 0);

        albertX= albertX + dx;
        albertY= albertY + dy;
        textSize(48);
        fill(255, 255, 255);
        text("Time: "+ (millis() - startTime)/1000 + "s", 25, 50);
        text("Score: "+ score, 25, 100);

        if (albertX<0 || albertX>width-albertW)
        { 
          dx=-dx;
        }
        if (albertY<0 || albertY>height-albertH)
        { 
          dy=-dy;
        }

        if (albertX+albertW >= mouseX && albertX <= mouseX + 100 && albertY+albertH >= mouseY && albertY <= mouseY + 130)
        {
          score = score - 1;
          albertX = int(random(0, width-albertW));
          albertY = int(random(0, height/2-albertH));
        }
      }
  }
}
 else {
  image(imgbk, 0, 0, width, height);
  PFont myFont;
  myFont = loadFont("Rockwell-ExtraBold-65.vlw");
  textFont(myFont);
  fill(255);
  text("Start", 80, 400);
}
}
}

void mousePressed() {
  running = !running;
}

i want to have a welcome screen at the beginning and the user need to press to start the game. Getting a -10 will lead to a game lose. Thank you so much!

Hi,

Nice tip : you can press Ctrl+T in the Processing editor to auto fomat your code ! :wink:

After this operation, you can see that line 91, the bracket is underlined with a red colored line :

Capture d’écran de 2020-11-27 20-56-52

In Java and most of the other languages like C or JavaScript when you open a block (with barckets) you need to close it.

And also you added too much brackets in the draw() function, an empty block, I just removed it and added some blank lines to make the code more clear :

boolean running = false;
PImage imgbk;
PImage imgm;
PImage albert;
PImage imgnote;
String[] imageNames = { 
  "albert0.jpg", "albert1.jpg"
};
PImage[] images = new PImage[imageNames.length];
int numPeople = 2;
int dx, dy;
int score;
int startTime;
int albertX, albertY, albertW, albertH;


void setup() {
  size(850, 700);

  imgbk = loadImage("b.jpg");
  imgm = loadImage("m.jpg");
  imgnote = loadImage("n.png");

  for (int i = 0; i < imageNames.length; i=i++) { 
    String imageName = imageNames[i];
    images[i] = loadImage(imageName);
  }

  albertW = 150;
  albertH = 150;
  albertX = int(random(0, width-albertW));
  albertY = int(random(0, height/2-albertH));

  dx=5;
  dy=5;

  startTime = millis();
  score = 0;
}


void draw() {
  fill(0);

  if (score == -10) {
    textSize(50);
    fill(255, 0, 0);
    text("You Lose!", 150, 270);
  } else if (running) {
    image(imgm, 0, 0, width, height);
    image(imgnote, mouseX, mouseY, 100, 130);
    float r = random(0, 2); 
    int ir = int(r);
    PImage img = images[ir]; 
    image(img, 0, 0);

    albertX= albertX + dx;
    albertY= albertY + dy;
    textSize(48);
    fill(255, 255, 255);
    text("Time: "+ (millis() - startTime)/1000 + "s", 25, 50);
    text("Score: "+ score, 25, 100);

    if (albertX<0 || albertX>width-albertW) { 
      dx=-dx;
    }

    if (albertY<0 || albertY>height-albertH) { 
      dy=-dy;
    }

    if (albertX+albertW >= mouseX && albertX <= mouseX + 100 && albertY+albertH >= mouseY && albertY <= mouseY + 130) {
      score = score - 1;
      albertX = int(random(0, width-albertW));
      albertY = int(random(0, height/2-albertH));
    }
  } else {
    image(imgbk, 0, 0, width, height);
    PFont myFont;
    myFont = loadFont("Rockwell-ExtraBold-65.vlw");
    textFont(myFont);
    fill(255);
    text("Start", 80, 400);
  }
}

void mousePressed() {
  running = !running;

PS : EOF means end of file, and it’s because with an additional bracket, the parser thought it was the end of the file but there is an else!

2 Likes