# Color not showing up

**URL:** <https://discourse.processing.org/t/color-not-showing-up/7289>\
**Category:** Coding Questions\
**Created:** [January 7, 2019, 9:06pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289 "2019-01-07T21:06:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jrekz](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Jrekz](https://discourse.processing.org/u/Jrekz)\
**Post date:** [January 7, 2019, 9:06pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/1 "2019-01-07T21:06:25Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 7, 2019, 9:22pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/2 "2019-01-07T21:22:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jrekz](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Jrekz](https://discourse.processing.org/u/Jrekz)\
**Post date:** [January 7, 2019, 9:26pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/3 "2019-01-07T21:26:04Z")

</div>

Forgot about that, should be good now

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 7, 2019, 9:30pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/4 "2019-01-07T21:30:02Z")

</div>

Havent solved the problem yet, but take a look to [rectMode()](https://processing.org/reference/rectMode_.html).

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 7, 2019, 9:45pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/5 "2019-01-07T21:45:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 7, 2019, 9:57pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/6 "2019-01-07T21:57:43Z")

</div>

> [@Jrekz](#):
>
> if (missed \>= 3) { noLoop(); textAlign(CENTER); textSize(30); fill(255); text(“GAME OVER”, width/2, height/2); } else { col=255; }

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

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [January 7, 2019, 9:59pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/7 "2019-01-07T21:59:19Z")

</div>

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:

```auto
  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:

```auto
posX = (int)random(width - size);

```

---

<div class="post-metadata">

**Author:** ![Jrekz](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Jrekz](https://discourse.processing.org/u/Jrekz)\
**Post date:** [January 7, 2019, 10:21pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/8 "2019-01-07T22:21:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 8, 2019, 1:04pm UTC](https://discourse.processing.org/t/color-not-showing-up/7289/9 "2019-01-08T13:04:38Z")

</div>

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