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;
}
}