Modifying a game: score, random, display

I’m trying to use the concept you suggested, and I don’t think I’m implementing it correctly. Below is the “test” code I put together. Once I put my mouse on the ellipse, it turns gray, but when I remove my mouse, it is still gray. How can I correct it to where it only turns gray if the mouse is on top?
Also, when I try to add a virus2, neither one will popup anymore.

float x;
float y;
float r;
boolean virus1 = false;

void setup() {
  size (500, 500);
  x = 59;
  y = 59;
  r = 55;
}

void draw() {
  background(0);


  if (virus1 == true) {
    ellipse(x, y, r, r);
    if (dist(mouseX, mouseY, x, y) < r) {
      fill(150);
    }
  }
}

void keyPressed() {
  if (key == 'q') {
    virus1 = true;
  } else {
    virus1 = false;
  }
}


Thank you for confirming that all of the things I want to do are possible, and for trying to help me figure out how to make them possible. That’s the part I’m having trouble with :sweat_smile: implementing and adapting the information to what I want to do.

I will check out the mousePressed() function and try to use that instead of what I have. The score adds up way to fast that way! haha

As far as the array for the object. I thought about doing that initially, but I wasn’t sure how to implement it (while minimizing/simplifying the code) since I am using a certain key to activate each virus individually. (i.e. ‘w’ = virus1, ‘q’ = virus2). I tried googling it but I couldn’t find anything on this… that may be because I’m not searching properly, but I’m not sure how to search for that. Do you know of anything I can reference for further explanation?

Hello,

In your recent example…

That is exactly what you programmed; you set fill(150) and that will remain so until you change it. You have not set it to any other value in your code.

If you hit any key other than ‘q’ it will set virus1 = false as per your if else statement.

I suggest that you review your if else statements so you have a better understanding of how these work.

What condition do you want to set virus1 = false ?
When that condition is met then set virus1 = false.

You can use println() statements in your code to follow the state of variables to help with understanding flow and debugging.

The last two variables of ellipse are width and height and not radius.
Take a look at the references for this.

:)

3 Likes

I had actually started with your code and rewrote most of it for a personal challenge.

Here is an example of “time to live” for one button:

:)