Color not showing up

After I click inside the moving cube, the hit count rises but the color doesn’t change. However, when I click outside it the color changes to red and the miss count increases (it should be like that). What could cause that?

Also, how do I constrain cube with random coordinates to appear only inside the window?

boolean start, hit, miss;
int pauseCounter, rectCol, hits, missed;
int xPosR, yPosR, widthR, heightR;
int posX, posY, size=20, col;

void setup() {
  size(900, 300); 

  xPosR=width/2;
  yPosR=height/2;
  widthR=width/4;
  heightR=height/10;

  posX=width/2;
  posY=height/2;
}
void draw() {
  background(0);

  if (start) {
    background(0);

    fill(255);
    text("Hits:"+hits, 350, 20); 
    text("Missed:"+missed, 450, 20);

    fill(col);
    rect(posX, posY, size, size);
  } else {
    drawStartButton(xPosR, yPosR);
  }

  if (pauseCounter == 120) {
    posX = (int)random(width);
    posY = (int)random(height);
    pauseCounter=0;
  }
  pauseCounter++;

  if (hit) {
    col=color(0, 255, 0);
    hits+=1;
    hit=false;
  } else {
    col=255;
  }

  if (miss && start == true) {
    col=color(255, 0, 0);
    missed+=1;
    miss=false;
  }
  if (missed >= 3) {
    noLoop();
    textAlign(CENTER);
    textSize(30);
    fill(255);
    text("GAME OVER", width/2, height/2);
  } else {
    col=255;
  }
}

void drawStartButton(int x, int y) {
  //rectangle text box
  if (mouseX > x-widthR/2 && mouseX < x+widthR/2 &&
    mouseY > y-heightR/2 && mouseY < y+heightR/2) {
    rectCol=200;
  } else {
    rectCol=100;
  }

  fill(rectCol);
  rect(x-widthR/2, y-heightR/2, widthR, heightR);

  //text
  textAlign(CENTER);
  fill(255);
  textSize(15);
  text("Click to Start", xPosR, yPosR+heightR/5);
}

void mousePressed() {
  if (mouseX > xPosR-xPosR/4 && mouseX < xPosR+widthR/2 &&
    mouseY > yPosR-yPosR/10 && mouseY < yPosR+heightR/2) {
    start=true;
  }

  if (mouseX > posX && mouseX < posX+size &&
    mouseY > posY && mouseY < posY + size) {
    hit=true;
  } else {
    miss=true;
  }
}

void keyPressed() {
  if (key == 'r' || key == 'R') {
    loop();
    start=false;
    hits=0;
    missed=0;
    posX=width/2;
    posY=height/2;
  }
}

Why nobody does the tutorial? :sob:
Please, format your code with </>
I will take a look to your code

Forgot about that, should be good now

Havent solved the problem yet, but take a look to rectMode().

I can t find whats wrong,(I m a beginner too but I like at least try to help ppl) but
to constrain the cube, you can do a random 0,widht - 'cube' width and rand 0 height - cube height . For the cube I suggested you to look at rectMode() because maybe you fogot that the cube is created from the left top corner.
I keep looking to your code because I have nothing else to do ;).
Forgot to say that my cube doesnt even turn red

This is your problem, the Else. Another mistake you made is when i pres start i stars with 1 missed

1 Like

You’re setting the color to white in the else statement for both hit and miss. I suggest doing an else if to solve this problem. Like:

  if (hit) {
    // hit stuff
  } else if (missed >= 3) {
    // miss stuff
  } else {
    col=255;
  }

As for your other question try subtracting the size from your random pos. Because the rect is drawn starting in the top left corner using the default rectMode. Like:

posX = (int)random(width - size);
1 Like

Color and boundaries are now fixed, thanks. Any suggestions on how to make ‘missed’ count 0 instead of 1 at the start of the sketch?

initialize to -1 or make a function for the menu and one for the game