# Happiness Meter

**URL:** <https://discourse.processing.org/t/happiness-meter/5326>\
**Category:** Coding Questions\
**Created:** [November 7, 2018, 5:12pm UTC](https://discourse.processing.org/t/happiness-meter/5326 "2018-11-07T17:12:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 5:12pm UTC](https://discourse.processing.org/t/happiness-meter/5326/1 "2018-11-07T17:12:37Z")

</div>

I am having trouble making a happiness meter. So I want to be able to have a timer aka my meter start counting down when I press the s key. Then if the happiness meter gets to low the rock image will change and if it gets super low it will change again. Then I want to be able to press the p key so the happiness of the rock goes up and the image changes to a happy rock. But if you go to high the rock will change images again.

```auto

PImage rock;
PImage neutralrock;
PImage likes;
PImage dislikes;
PImage happyrock;
PImage madrock;
PImage exploderock;
PImage deadrock;
int happiness;
int beforeTickle = 0;
int beforeTickleFrames = 5;
int tickleEnd;
boolean gameOver = false;

void setup()
{
  
  frameRate(1);
  size(800,600);
  dislikes = loadImage("dislikes.png");
  likes = loadImage("likes.png");
  exploderock = loadImage("exploderock.png");
  happyrock = loadImage("happyrock.png");
  rock = loadImage("rock.png");
  deadrock = loadImage("deadrock.png");
  madrock = loadImage("madrock.png");
  neutralrock = rock;
  happiness = 100;
  tickleEnd = millis(); //done for any early pet doAction
  noLoop(); //reset by start button doAction
}

void draw()
{
  //sun
  fill(253,184,19);
  noStroke();
  ellipse(690,100,175,175);
  
  //grass
  fill(136, 204, 51);
  rect(0,450,800,200);
  fill(196, 136,71);
  
  //tree
  rect(64,239,60,250);
  fill(210,105,30);
  ellipse(100,217,220,220);
  ellipse(20,287,180,180);
  ellipse(160,287,180,180);
  
  
  image(neutralrock,400,400,90,90);
   if (gameOver) 
   {
     noLoop();
   }
   println("Happiness level of Rock is ",happiness);
   textSize(26);
   text(happiness,20,30);
}

 

void checkRock()
{
  if (happiness<60) {
    neutralrock = madrock;
    gameOver = true;
  }
  else if (happiness<80) {
    neutralrock = deadrock;
  }
  else if (happiness < 120) {
    neutralrock = rock;
  }
  else if (happiness <150) {
    neutralrock = happyrock;
  }
  else {
    neutralrock = exploderock;
    gameOver = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 7, 2018, 5:31pm UTC](https://discourse.processing.org/t/happiness-meter/5326/2 "2018-11-07T17:31:18Z")

</div>

Look at :

- [https://processing.org/reference/key.html](https://processing.org/reference/key.html)
- [https://processing.org/reference/keyPressed\_.html](https://processing.org/reference/keyPressed_.html)

And create a `Boolean counting_down` and a `int hapiness_level` variables.

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 5:36pm UTC](https://discourse.processing.org/t/happiness-meter/5326/3 "2018-11-07T17:36:15Z")

</div>

I know how to do the keypress part, what confusing me is how to make a timer that will work with what I want.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 7, 2018, 5:40pm UTC](https://discourse.processing.org/t/happiness-meter/5326/4 "2018-11-07T17:40:57Z")

</div>

Like that :

```auto
Boolean counting_down =false;
int hapiness_meter = 255;

void setup(){
  size(400,400);
  
}

void draw(){
  background(hapiness_meter,0,0);
  text(hapiness_meter,50,50);
  
  if (counting_down && frameCount % 10 == 0) hapiness_meter --;
}

void keyPressed(){
  if (key == 's'){
    counting_down = true;
  }
  if (key == 'p'){
    hapiness_meter += 10;
  }
}

```

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 6:03pm UTC](https://discourse.processing.org/t/happiness-meter/5326/5 "2018-11-07T18:03:55Z")

</div>

Thank you for that. Now I’m trying to make the images change once it goes above or below a certain time. That’s what this code is for:

```auto
void checkRock()
{
  if (happiness<60) {
    neutralrock = madrock;
    counting_down = true;
  }
  else if (happiness<80) {
    neutralrock = deadrock;
  }
  else if (happiness < 120) {
    neutralrock = rock;
  }
  else if (happiness <150) {
    neutralrock = happyrock;
  }
  else {
    neutralrock = exploderock;
    counting_down = true;
  }
}

```

I set hapiness=to happiness\_meter and used the variable happiness in checkRock. But nothing is happening.

```auto
Boolean counting_down =false;
int hapiness_meter = 100;
PImage rock;
PImage neutralrock;
PImage likes;
PImage dislikes;
PImage happyrock;
PImage madrock;
PImage exploderock;
PImage deadrock;
int happiness;
int beforeTickle = 0;
int beforeTickleFrames = 5;
int tickleEnd;

void setup(){
  size(800,600);
  dislikes = loadImage("dislikes.png");
  likes = loadImage("likes.png");
  exploderock = loadImage("exploderock.png");
  happyrock = loadImage("happyrock.png");
  rock = loadImage("rock.png");
  deadrock = loadImage("deadrock.png");
  madrock = loadImage("madrock.png");
  neutralrock = rock;
  happiness = hapiness_meter;
  
}

void draw(){
  background(135,206,250);
  textSize(26);
  text(hapiness_meter,50,50);
  
  if (counting_down && frameCount % 10 == 0) hapiness_meter --;
  
  //sun
  fill(253,184,19);
  noStroke();
  ellipse(690,100,175,175);
  
  //grass
  fill(136, 204, 51);
  rect(0,450,800,200);
  fill(196, 136,71);
  
  //tree
  rect(64,239,60,250);
  fill(210,105,30);
  ellipse(100,217,220,220);
  ellipse(20,287,180,180);
  ellipse(160,287,180,180);
  image(neutralrock,400,400,90,90);
   
}

void keyPressed(){
  if (key == 's'){
    counting_down = true;
  }
  if (key == 'p'){
    hapiness_meter += 10;
  }
}

void checkRock()
{
  if (happiness<60) {
    neutralrock = madrock;
    counting_down = true;
  }
  else if (happiness<80) {
    neutralrock = deadrock;
  }
  else if (happiness < 120) {
    neutralrock = rock;
  }
  else if (happiness <150) {
    neutralrock = happyrock;
  }
  else {
    neutralrock = exploderock;
    counting_down = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 7, 2018, 6:49pm UTC](https://discourse.processing.org/t/happiness-meter/5326/6 "2018-11-07T18:49:12Z")

</div>

> [@ant](#):
>
> But nothing is happening.

This is because you are affecting `hapiness` to `hapiness_meter` in the `setup()` function so `hapiness = 100` just at the end of the setup and then nothing (remember that setup is running just once and draw, every frame).  
You don’t need 2 variables, just use hapiness as the value that is decrementing and incrementing when you press a key.

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 7:10pm UTC](https://discourse.processing.org/t/happiness-meter/5326/7 "2018-11-07T19:10:36Z")

</div>

Alright, I got rid of the other variable and changed the variables in the Rock Check function to hapiness\_meter and I’m still getting nothing.

```auto
Boolean counting_down =false;
int hapiness_meter = 100;
PImage rock;
PImage neutralrock;
PImage likes;
PImage dislikes;
PImage happyrock;
PImage madrock;
PImage exploderock;
PImage deadrock;

int beforeTickle = 0;
int beforeTickleFrames = 5;
int tickleEnd;

void setup(){
  size(800,600);
  dislikes = loadImage("dislikes.png");
  likes = loadImage("likes.png");
  exploderock = loadImage("exploderock.png");
  happyrock = loadImage("happyrock.png");
  rock = loadImage("rock.png");
  deadrock = loadImage("deadrock.png");
  madrock = loadImage("madrock.png");
  neutralrock = rock;
  
  
}

void draw(){
  background(135,206,250);
  textSize(26);
  text(hapiness_meter,50,50);
  
  if (counting_down && frameCount % 10 == 0) hapiness_meter --;
  
  //sun
  fill(253,184,19);
  noStroke();
  ellipse(690,100,175,175);
  
  //grass
  fill(136, 204, 51);
  rect(0,450,800,200);
  fill(196, 136,71);
  
  //tree
  rect(64,239,60,250);
  fill(210,105,30);
  ellipse(100,217,220,220);
  ellipse(20,287,180,180);
  ellipse(160,287,180,180);
  image(neutralrock,400,400,90,90);
   
}

void keyPressed(){
  if (key == 's'){
    counting_down = true;
  }
  if (key == 'p'){
    hapiness_meter += 10;
  }
}

void checkRock()
{
  if (hapiness_meter<60) {
    neutralrock = madrock;
    counting_down = true;
  }
  else if (hapiness_meter<80) {
    neutralrock = deadrock;
  }
  else if (hapiness_meter < 120) {
    neutralrock = rock;
  }
  else if (hapiness_meter <150) {
    neutralrock = happyrock;
  }
  else {
    neutralrock = exploderock;
    counting_down = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 7, 2018, 8:46pm UTC](https://discourse.processing.org/t/happiness-meter/5326/8 "2018-11-07T20:46:20Z")

</div>

Do you press the key `'s'` ?  
Why do you use `couting_down = true;` three thimes in your code? (just put this line when you press s)

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 8:51pm UTC](https://discourse.processing.org/t/happiness-meter/5326/9 "2018-11-07T20:51:25Z")

</div>

Yeah, I pressed ‘s’ that starts the countdown and when the number hits a certain instance(number) im trying to get it to change pictures. I’ve tried several things I’m unsure what I’m missing. I also got rid of `counting_down=true;`.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 7, 2018, 9:35pm UTC](https://discourse.processing.org/t/happiness-meter/5326/10 "2018-11-07T21:35:50Z")

</div>

Found it !

This is because you don’t call `checkRock()` in the `draw()` function 😏

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 7, 2018, 10:06pm UTC](https://discourse.processing.org/t/happiness-meter/5326/11 "2018-11-07T22:06:22Z")

</div>

😯 Thanks!! Anyways now that I have that part done I want to implement one more user action. If the rock is pressed with the mouse I want it to randomly change to either the (likes) image or (dislikes) image. Problem is it only changes for a split second.

```auto
Boolean counting_down =false;
int hapiness_meter = 100;
PImage rock;
PImage neutralrock;
PImage likes;
PImage dislikes;
PImage happyrock;
PImage madrock;
PImage exploderock;
PImage deadrock;

void setup(){
  size(800,600);
  dislikes = loadImage("dislikes.png");
  likes = loadImage("likes.png");
  exploderock = loadImage("exploderock.png");
  happyrock = loadImage("happyrock.png");
  rock = loadImage("rock.png");
  deadrock = loadImage("deadrock.png");
  madrock = loadImage("madrock.png");
  neutralrock = rock;
  
  
}

void draw(){
  background(135,206,250);
  textSize(26);
  text(hapiness_meter,50,50);
  
  if (counting_down && frameCount % 10 == 0) hapiness_meter --;
  
  //sun
  fill(253,184,19);
  noStroke();
  ellipse(690,100,175,175);
  
  //grass
  fill(136, 204, 51);
  rect(0,450,800,200);
  fill(196, 136,71);
  
  //tree
  rect(64,239,60,250);
  fill(210,105,30);
  ellipse(100,217,220,220);
  ellipse(20,287,180,180);
  ellipse(160,287,180,180);
  image(neutralrock,400,400,90,90);
  
  checkRock();
   
}

void keyPressed(){
  if (key == 's'){
    counting_down = true;
  }
  if (key == 'p'){
    hapiness_meter += 10;
  }
}

void checkRock()
{
  if (hapiness_meter<60) {
    neutralrock = deadrock;
    counting_down = false;
    
  }
  else if (hapiness_meter<80) {
    neutralrock = madrock;
  }
  else if (hapiness_meter < 120) {
    neutralrock = rock;
  }
  else if (hapiness_meter <150) {
    neutralrock = happyrock;
  }
  else {
    neutralrock = exploderock;
    counting_down = false;
  }
}

void mouseReleased() {
  if (.5<random(1)){
        neutralrock=likes;
      }
   else{
        neutralrock=dislikes;
      }
}
    

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 8, 2018, 11:05pm UTC](https://discourse.processing.org/t/happiness-meter/5326/12 "2018-11-08T23:05:32Z")

</div>

It depends, do you want the rock to change once for all to likes or dislikes image or do you want it to stay like this for a second for example?

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 9, 2018, 1:41pm UTC](https://discourse.processing.org/t/happiness-meter/5326/13 "2018-11-09T13:41:49Z")

</div>

I want it to stay as like or dislike for a few seconds then change back to the neutral rock.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 9, 2018, 4:16pm UTC](https://discourse.processing.org/t/happiness-meter/5326/14 "2018-11-09T16:16:01Z")

</div>

Because once you Release the mouse it Sets to what you want, But checkRock is still activated in the Next Frame. So it is changed again to the value of happiness you had. You should set the value to that of either happy or sad in the mouseReleased() function. And to change it back to neutral, just add a global int and set it to millis() inside the mouseReleased() function and in draw check if millis() - the new int is bigger than 5000 and if so, then set the happiness Meter to the neutral value. That adds a 5 seconds ‚delay‘.

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [November 9, 2018, 6:36pm UTC](https://discourse.processing.org/t/happiness-meter/5326/15 "2018-11-09T18:36:16Z")

</div>

> [@ant](#):
>
> void mouseReleased() { if (.5\<random(1)){ neutralrock=likes; } else{ neutralrock=dislikes; } }

I’m sorry not exactly understanding what you are saying. I now see what you mean by in the next frame checkRock is still active, but I’m not exactly sure what you are saying about setting the value. Also I’m trying to make it random whether it changes or not.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 9, 2018, 8:06pm UTC](https://discourse.processing.org/t/happiness-meter/5326/16 "2018-11-09T20:06:07Z")

</div>

Well, since checkRock is still in use, your program now sets (in one Frame) if happiness = 0, so rock = deadrock, then from the mouseClick Rock = happyrock (50%chance, so just for example), so happy Rock is set. But the Next Frame happiness is still 0, so deadrock is set again. One Frame is 1/60 of a Second, so the change is pretty fast.

So instead of using Rock = happyrock in mouseReleased, use happiness = 150 to make it become happy When checkRock is called.
