# Modifying a game: score, random, display

**URL:** https://discourse.processing.org/t/modifying-a-game-score-random-display/22785
**Category:** Coding Questions
**Tags:** homework
**Created:** [July 22, 2020, 8:41pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785 "2020-07-22T20:41:48Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 22, 2020, 8:41pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/1 "2020-07-22T20:41:48Z")

</div>

Hello,

I’m currently taking a ‘Learning Processing’ course and I need some help with a game I’m trying to create. It is a 2 person game (Player 1 controls the keyboard, Player 2 controls the mouse). It is a take on the arcade game whack-a-mole. I have a few different questions/areas I need help figuring out and debugging. (This is also the first time I post on a forum, so please let me know if I need to provide anything further).

1. For player 1 have set keys (Q, W, E, A, S, D, Z, X, C) as the controls for popping up a virus (my "mole). I used the keyCode == to set a specific key. Each key is currently working. However, it only displays one key at a time. If I press ‘Q’ and then press ‘X’, the virus on ‘Q’ disappears and it ‘X’ appears.

- Is it possible for the virus set to each key to appear at the same time as another? For example, if player 1 selects keys QSD can they all display at the same time?

- Also, to make the game fair, I would like for the virus (mole) to appear at random times. For example, when a key is pressed, the virus would pop up anywhere between -1 and 7 seconds? This way Player 1 doesn’t have full control of how long player 2 has to whack it.

- I have multiple classes built for this game. All code is set below.

1. To win the game, I’m keeping score. Currently, I only have player 2 keeping score.

- When the mouse is pressed on a virus, the score should increment. It is incrementing, by one, but if player 1 keeps the mouse pressed on the virus, the score continues to go up. How can I get it to only increment once?

- For player 1, I tried adding an else clause to count the number of times missed, but the score for player one continues to increment indefinitely. How can I set the proper parameters to ensure player 1’s score only incrememnts when player 2 didn’t whack-a-more (virus) on time?

```auto
Mallet m; //Mallet Class
Canvas c; //Canvas Class

//COVID-19 Class multiple times for each area #
Virus topR;
Virus topM;
Virus topL;

Virus midL;
Virus midM;
Virus midR;

Virus botR;
Virus botM;
Virus botL;

//Variables for scores
int player1Score;
int player2Score;

void setup() {
  size(700, 700);
  rectMode(CORNERS);

  //Initialize the scores for both players
  player1Score = 0;
  player2Score = 0;

  //Initialize objects
  c = new Canvas();
  m = new Mallet();
  
  topR = new Virus(200, 195, 65);
  topM = new Virus(350, 195, 65);
  topL = new Virus(500, 195, 65);

  midL = new Virus(200, 345, 65);
  midM = new Virus(350, 345, 65);
  midR = new Virus(500, 345, 65);

  botR = new Virus(200, 495, 65);
  botM = new Virus(350, 495, 65);
  botL = new Virus(500, 495, 65);
}

void draw() {
  background(33, 57, 65);
  c.display();

  //Text Area for players score
  textSize(14);
  fill(255);
  String player1 = "Player 1 Score: " + player1Score;
  text(player1, 40, 35, 120, 100);
  fill(150);
  String player2 = "Player 2 Score: " + player2Score;
  text(player2, 40, 350, 120, 100);

  //Virus & Player 1 Controlls
  if (keyCode == 'Q') { //top right
    topR.checkHit();
    topR.display();
  }
  if (keyCode == 'W') { //top middle
    topM.checkHit();
    topM.display();
  }
  if (keyCode == 'E') { //top left
    topL.checkHit();
    topL.display();
  }

  if (keyCode == 'A') { //middle left
    midL.checkHit();
    midL.display();
  }
  if (keyCode == 'S') { //center
    midM.checkHit();
    midM.display();
  }
  if (keyCode == 'D') { //middle right
    midR.checkHit();
    midR.display();
  }
  if (keyCode == 'Z') { //bottom right
    botR.checkHit();
    botR.display();
  }
  if (keyCode == 'X') { //bottom middle
    botM.checkHit();
    botM.display();
  }
  if (keyCode == 'C') { //bottom left
    botL.checkHit();
    botL.display();
  }

  //Mallet
  m.display();
} //end draw

class Virus {
  //variables
  float x;
  float y;
  float r;
  
  //Constructor
  Virus(float tempX, float tempY, float tempR) {
    x = tempX; //temporary x value
    y = tempY; //temporary y value
    r = tempR; //temporary width value
  }

  void display() { //what to display
    ellipseMode(CENTER);
    strokeWeight(1);
    ellipse(x, y, r, r);
  } //End Display

  void checkHit() { //check if the virus has been hit
    if (dist(mouseX, mouseY, x, y) < r){
      fill(0);
    }
    if ((dist(mouseX, mouseY, x, y) < r) && mousePressed && (player2Score < 100)) { //figure out how to make it oence clicked
      fill(50);
      player2Score++;
    } 
  } //End checkHit
} //End Class

class Canvas { //grid for play
  int edge = 125;

  //Constructor
  Canvas() {
  }

  void display() { //what to display
    rectMode(CORNERS);
    stroke(0);
    strokeWeight(4);
    noFill();
    rect(edge, edge, width-edge, height-edge);

    line(125, 275, 575, 275); //horizontal 1
    line(125, 425, 575, 425); //horizontal 2
    line(275, 125, 275, 575); //vertical 1
    line(425, 125, 425, 575); //vertical 2
    
    if (mousePressed) {
      mouseX = constrain(mouseX, edge + 30, 545); //constrain mouse X to canvas
      mouseY = constrain(mouseY, edge + 30, 545); //constrain mouse Y to canvas
    }
    
  } //end Display
} //end Class

class Mallet {

  //Constructor
  Mallet() {
  }

  void display() { //what to display

    rectMode(CENTER);
    if (!mousePressed) { 
      noStroke();
      fill(255);
      rect(mouseX, mouseY, 55, 55, 7);
    } else { 
      noStroke();
      fill(150);
      ellipse(mouseX, mouseY, 55, 55);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 22, 2020, 9:56pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/2 "2020-07-22T21:56:37Z")

</div>

Hello,

Welcome to the forum.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate, filter and use them.

This is a very short list:

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

Be mindful of our homework policy:  
[https://discourse.processing.org/faq#homework](https://discourse.processing.org/faq#homework)

> [@greenArtist7](#):
>
> I have a few different questions/areas I need help figuring out and debugging.

I find the the println() statement useful for debugging:

> **[println() / Reference](https://processing.org/reference/println_.html)**
>
> The println() function writes to the console area, the black rectangle at the bottom of the Processing environment. This function is often helpful for looking at the data a program is producing…

There are many keyboard related discussions in the forum.  
I encourage you to glean insight from these and try them on your own and decide what best fits with _your project_.

I suggest asking one question at a time with something you have researched and attempted; break it down and share _minimal code_ that you are asking about.

I generally do not assist with homework projects of this scope but will assist if I see _a question_ that I can offer help with.

`:)`

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 22, 2020, 10:00pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/3 "2020-07-22T22:00:15Z")

</div>

Thank you for those resources! I will check them out.

As far as my post, should I remove it, and repost as you suggested? With smaller amounts of code, and each question individually?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 22, 2020, 10:04pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/4 "2020-07-22T22:04:44Z")

</div>

Hello,

> [@greenArtist7](#):
>
> As far as my post, should I remove it, and repost as you suggested? With smaller amounts of code, and each question individually?

I will leave that with you.  
I am a member of the community just like you are.

Take a look at some of the other posts for examples.

My only suggestion is to break it down into parts, take it one step at a time, keep it simple and integrate it all in the end.

Lots of folks here to help guide you.

`:)`

---

<div class="post-metadata">

### Author: ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)
#### Post date: [July 23, 2020, 8:40am UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/5 "2020-07-23T08:40:52Z")

</div>

For the first problem, if I understood correctly, what I would do is assign a boolean for each virus and pressing the key toggles the boolean.  
Like this:  
Edit: I think now this is not exactly what you wanted but it may help

```auto
boolean virus1 = false;

if (virus1 == true) {
  topR.checkHit();
  topR.display();
}

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

void keyReleased() {
  if (key == 'q') virus1 = false;
}

```

> [@greenArtist7](#):
>
> Also, to make the game fair, I would like for the virus (mole) to appear at random times. For example, when a key is pressed, the virus would pop up anywhere between -1 and 7 seconds? This way Player 1 doesn’t have full control of how long player 2 has to whack it.

For this,:

```auto
delay(int(random(1000, 6000)));

```

delay() works pretty good.

Also always search on google first if you have questions using “processing ‘your question/error/problem’” format. It helps me a lot everyday.

Edit: actually dont use delay it doesnt work

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 23, 2020, 9:30am UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/6 "2020-07-23T09:30:12Z")

</div>

Actually `delay` should not be used as it would completely freeze the game.

In a general matter, it is often a bad idea to use the `delay` function.

I don’t have the time right now to go into more details but you want to use the `millis()` function to keep tack of the elapsed time. You can have a look on this forum, the question pops up often.

---

<div class="post-metadata">

### Author: ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)
#### Post date: [July 23, 2020, 9:31am UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/7 "2020-07-23T09:31:23Z")

</div>

Yes I know delay() sometimes causes problems (happened a lot lol) but I tried it in the code above and it worked fine.

Edit: I put it in the keyCode if statements.

```auto
if (keyCode == 'Q') { //top right
    delay(int(random(1000, 6000)));
    topR.checkHit();
    topR.display();
  }
  if (keyCode == 'W') { //top middle
    delay(int(random(1000, 6000)));
    topM.checkHit();
    topM.display();
  }

```

like this

EDIT: @glv @jb4x yes delay actually doesnt work my bad sorry about that

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 23, 2020, 11:53am UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/8 "2020-07-23T11:53:14Z")

</div>

> [@jb4x](#):
>
> it is often a bad idea to use the `delay` function.

Agreed!

And also stated here along with when it should be used:

> **[delay() / Reference](https://processing.org/reference/delay_.html)**
>
> The delay() function causes the program to halt for a specified time. Delay times are specified in thousandths of a second. For example, running delay(3000) will stop the program for thr…

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 23, 2020, 12:07pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/9 "2020-07-23T12:07:15Z")

</div>

> [@greenArtist7](#):
>
> For example, when a key is pressed, the virus would pop up anywhere between -1 and 7 seconds?

A virus should not show up in the past (-1 sec) to impact the future.

I shared is a snippet of code (recycled yours) using frameCount to randomly display a virus on the grid every second.

I often use frameCount for timing and millis(); it very much depends on my application.

I used println() to display outputs to aid in understanding this.

```auto
  int test = 0; //This is a global variable and should be outside of setup() and draw()

 //println(frameCount); //prints frameCount

  if (frameCount%60 == 0)
    {
    float rand = random(0, 3);
    test = int(rand);  
    println(frameCount, frameCount%60, frameCount/60, rand, test);
    }

  //Virus & Player 1 Controls
  if (test == 0) { //top right
    topR.checkHit();
    topR.display();
  }
  if (test == 1) { //top middle
    topM.checkHit();
    topM.display();
  }
  if (test == 2) { //top left
    topL.checkHit();
    topL.display();
  }

```

The above was integrated with your code _only as an example_ of timing with frameCount.

References:  
See my previous link.  
You can also “right click” on the keywords in your code and display references.

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 23, 2020, 2:14pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/10 "2020-07-23T14:14:39Z")

</div>

> [@qewer3322](#):
>
> @glv @jb4x yes delay actually doesnt work my bad sorry about that

No worries! It happens… to me as well. Even when I test my code first!

`:)`

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 23, 2020, 4:45pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/11 "2020-07-23T16:45:55Z")

</div>

Thank you for your advise… I tried it out on my code, and it’s not doing what I need it to do. When I press ‘Q’ nothing happens now.

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 23, 2020, 4:55pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/12 "2020-07-23T16:55:50Z")

</div>

This is kind of what I need. If I implement what jb4x mentioned about the millis() function to keep track of time, then something like this could work, right?

Would I add the millis() function to the main program?  
Maybe declare

```auto
float rand = random(0, 3);

```

at the top and under each if keyCode statement add

```auto
rand;

```

would something like that work? or is my theory flawed somewhere?

---

<div class="post-metadata">

### Author: ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)
#### Post date: [July 23, 2020, 5:00pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/13 "2020-07-23T17:00:40Z")

</div>

it works on me with his code:

```auto
Mallet m; //Mallet Class
Canvas c; //Canvas Class

//COVID-19 Class multiple times for each area #
Virus topR;
Virus topM;
Virus topL;

Virus midL;
Virus midM;
Virus midR;

Virus botR;
Virus botM;
Virus botL;

//Variables for scores
int player1Score;
int player2Score;
boolean virus1 = false;

void setup() {
  size(700, 700);
  rectMode(CORNERS);

  //Initialize the scores for both players
  player1Score = 0;
  player2Score = 0;

  //Initialize objects
  c = new Canvas();
  m = new Mallet();
  
  topR = new Virus(200, 195, 65);
  topM = new Virus(350, 195, 65);
  topL = new Virus(500, 195, 65);

  midL = new Virus(200, 345, 65);
  midM = new Virus(350, 345, 65);
  midR = new Virus(500, 345, 65);

  botR = new Virus(200, 495, 65);
  botM = new Virus(350, 495, 65);
  botL = new Virus(500, 495, 65);
}

void draw() {
  background(33, 57, 65);
  c.display();

  //Text Area for players score
  textSize(14);
  fill(255);
  String player1 = "Player 1 Score: " + player1Score;
  text(player1, 40, 35, 120, 100);
  fill(150);
  String player2 = "Player 2 Score: " + player2Score;
  text(player2, 40, 350, 120, 100);
  
  if (virus1 == true) {
  topR.checkHit();
  topR.display();
}

  if (keyCode == 'W') { //top middle
    topM.checkHit();
    topM.display();
  }
  if (keyCode == 'E') { //top left
    topL.checkHit();
    topL.display();
  }

  if (keyCode == 'A') { //middle left
    midL.checkHit();
    midL.display();
  }
  if (keyCode == 'S') { //center
    midM.checkHit();
    midM.display();
  }
  if (keyCode == 'D') { //middle right
    midR.checkHit();
    midR.display();
  }
  if (keyCode == 'Z') { //bottom right
    botR.checkHit();
    botR.display();
  }
  if (keyCode == 'X') { //bottom middle
    botM.checkHit();
    botM.display();
  }
  if (keyCode == 'C') { //bottom left
    botL.checkHit();
    botL.display();
  }

  //Mallet
  m.display();
} //end draw

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

void keyReleased() {
  if (key == 'q') virus1 = false;
}

class Virus {
  //variables
  float x;
  float y;
  float r;
  
  //Constructor
  Virus(float tempX, float tempY, float tempR) {
    x = tempX; //temporary x value
    y = tempY; //temporary y value
    r = tempR; //temporary width value
  }

  void display() { //what to display
    ellipseMode(CENTER);
    strokeWeight(1);
    ellipse(x, y, r, r);
  } //End Display

  void checkHit() { //check if the virus has been hit
    if (dist(mouseX, mouseY, x, y) < r){
      fill(0);
    }
    if ((dist(mouseX, mouseY, x, y) < r) && mousePressed && (player2Score < 100)) { //figure out how to make it oence clicked
      fill(50);
      player2Score++;
    } 
  } //End checkHit
} //End Class

class Canvas { //grid for play
  int edge = 125;

  //Constructor
  Canvas() {
  }

  void display() { //what to display
    rectMode(CORNERS);
    stroke(0);
    strokeWeight(4);
    noFill();
    rect(edge, edge, width-edge, height-edge);

    line(125, 275, 575, 275); //horizontal 1
    line(125, 425, 575, 425); //horizontal 2
    line(275, 125, 275, 575); //vertical 1
    line(425, 125, 425, 575); //vertical 2
    
    if (mousePressed) {
      mouseX = constrain(mouseX, edge + 30, 545); //constrain mouse X to canvas
      mouseY = constrain(mouseY, edge + 30, 545); //constrain mouse Y to canvas
    }
    
  } //end Display
} //end Class

class Mallet {

  //Constructor
  Mallet() {
  }

  void display() { //what to display

    rectMode(CENTER);
    if (!mousePressed) { 
      noStroke();
      fill(255);
      rect(mouseX, mouseY, 55, 55, 7);
    } else { 
      noStroke();
      fill(150);
      ellipse(mouseX, mouseY, 55, 55);
    }
  }
}

```

But what this code does is, when you press q virus appears and when you release it, it dissapears. I think that is not exactly you want but you can delete the keyReleased part and make the virus’ dissapear after some time.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 23, 2020, 5:17pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/14 "2020-07-23T17:17:12Z")

</div>

Hello,

Stop asking questions (I counted 4) and start writing code. \<\< That is encouragement and the same advise I give everyone. `:)`

Every time I have an idea or given a suggestion… I research it, explore it, start writing a simple example to understand it and then may integrate it into my code.

There are numerous posts on timers.  
Look at them, try them out and then write them out on your own; the last step will help you understand them and a good exercise.

A few of mine:

> [@Need assistance with timing! \[millis()\]](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/4):
>
> I provided a simple example: int whattimeisitnow; int whattimeisitatstartofstopwatch; int timepassedtoresetstopwatch = 1000; //setup runs once void setup() { whattimeisitatstartofstopwatch = millis(); } //draw loops 60 frames per sec void draw() { whattimeisitnow = millis(); if (whattimeisitnow \> whattimeisitatstartofstopwatch + timepassedtoresetstopwatch) { println("1000 ms have passed!"); whattimeisitatstartofstopwatch = millis(); //reset st…

> [@Some questions about millis()](https://discourse.processing.org/t/some-questions-about-millis/19541/3):
>
> Hello, You can’t reset millis(). You can start a time elapsed counter that counts from the time started. A simple example: int timeStart; int timeElapsed; void setup() { timeStart = millis(); } void draw() { timeElapsed = millis() - timeStart; println(timeElapsed); } void keyPressed() { timeStart = millis(); //Resets time elapsed counter }slight_smile Other millis() examples here:

`:)`

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 23, 2020, 5:19pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/15 "2020-07-23T17:19:51Z")

</div>

Thank you for that. I put the boolean in the wrong place which made it not work. It’s working now, but I don’t see how that is much different than what I currently have set up. Would doing it this way have some kind of advantage to my current code?

What I’m having trouble figuring out is how to make each virus appear for a random amount of time.

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 23, 2020, 5:22pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/16 "2020-07-23T17:22:44Z")

</div>

I’m just trying to make sure that what I want to try is worth spending time on. I’m not unwilling to try or research things, I just don’t want to waist my time trying to implement something that isn’t possible. I’m still learning how this all works, which is why I’m asking questions.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 23, 2020, 5:48pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/17 "2020-07-23T17:48:54Z")

</div>

Hello,

You can use frameCount or millis() for a timer.

Look up references for these and run the examples.

I like to use the modulo % operator:  
[https://processing.org/reference/modulo.html](https://processing.org/reference/modulo.html)

In my example I am using frameCount%60 which is a one second timer  
draw() has a frameRate of 60 frames per second so every 60 frames is one second.

frameCount%2 is 2/60 of a second and so on…

You could make the 60 (in %60) a variable and work with that.

You could also go the millis() way for timing.

I work with both so not going to recommend one or the other.

The code I shared was only a simple example of a timer; you had some if statements there and it was easy to integrate rather than writing a separate example. The focus was on timers and not your overall project.

`:)`

---

<div class="post-metadata">

### Author: ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)
#### Post date: [July 23, 2020, 5:55pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/18 "2020-07-23T17:55:35Z")

</div>

The only advantage of my code is that there can be two viruses at the same time. What I suggest doing there is remove the mouseReleased void and make it so the boolean becomes false when you click on the virus.

---

<div class="post-metadata">

### Author: ![greenArtist7](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@greenArtist7](https://discourse.processing.org/u/greenArtist7)
#### Post date: [July 23, 2020, 7:31pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/19 "2020-07-23T19:31:25Z")

</div>

Oh, I see now! That makes sense! Thank you! Hopefully, with the timer, I can implement a random feature, and make it disappear without the keyReleased function and implement your suggestion about the disappearing when it becomes false. Thank you 🙂

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 24, 2020, 12:46pm UTC](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785/20 "2020-07-24T12:46:34Z")

</div>

> [@greenArtist7](#):
>
> I just don’t want to waist my time trying to implement something that isn’t possible.

Everything on that list is possible.

Considerations:

- There is a difference between:  
[mousePressed() / Reference / Processing.org](https://processing.org/reference/mousePressed_.html)  
[mousePressed / Reference / Processing.org](https://processing.org/reference/mousePressed.html) This was giving some high scores!

- You are working with 9 objects and these can be easily managed with an array of objects:  
[https://youtu.be/-sSRHRfK2EU](https://youtu.be/-sSRHRfK2EU)  
[array objects | Learning Processing 2nd Edition](http://learningprocessing.com/examples/chp09/example-09-09-array-objects)  
[Array Objects / Examples / Processing.org](https://processing.org/examples/arrayobjects.html)  
Imagine a 4x4 or even larger grid!

- The object data can be anything. You already have position; you can also code (keep track) of anything your creative mind can think of… within the limits of what Processing\Java allows.

I worked through your code and it is all achievable except for time warp (the -1 sec.).

In the end I ended up using arrays of objects as this was much easier and then it was simple to assign color, times, state, boolean, etc. to each object and work with that.

> **Simple Example of Object Array \< Click here!**
>
> ```auto
> Virus [] v = new Virus[3];
> 
> void setup()
> {
> size(700, 400);
>   
> //Use this:     
> //for(int i = 0; i< v.length; i++)
> // {
> // color c1 = color(random(0, 255), random(0, 255), random(0, 255));  
> // v[i] = new Virus(random(0+100, width-100), random(0+100, height-100), random(10, 100), c1);
> // }
>   
> // Or use below:
> color c1 = color(random(0, 255), random(0, 255), random(0, 255)); //EAch has the same color
>   
> v[0] = new Virus(200, 195, 65, c1);
> v[1] = new Virus(350, 195, 65, c1);
> v[2] = new Virus(500, 195, 65, c1);
> }
> 
> void draw()
> {
> background(200);
>   
> for(int i = 0; i< v.length; i++)
> {
> fill(v[i].c);
> circle(v[i].x, v[i].y, v[i].r);
> }
> }    
>  
> // ***************************************************************************************************
> class Virus 
> {
> float x;
> float y;
> float r;
> color c;
> // What else can I add here?
>   
> //Constructor
> Virus(float tempX, float tempY, float tempR, color tempC) 
> {
> x = tempX;
> y = tempY; 
> r = tempR; 
> c = tempC;
> }
> }
> 
> ```

`:)`

[Next page](https://discourse.processing.org/t/modifying-a-game-score-random-display/22785.md?page=2)
