Happiness Meter

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.


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

Look at :

And create a Boolean counting_down and a int hapiness_level variables.

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

Like that :

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

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:

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.

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

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.

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.

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

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)

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

Found it !

This is because you don’t call checkRock() in the draw() function :smirk:

:hushed: 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.

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

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?

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

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‘.

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.

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.