# Whack-A-Mole Game

**URL:** <https://discourse.processing.org/t/whack-a-mole-game/23113>\
**Category:** Coding Questions\
**Created:** [August 7, 2020, 6:00pm UTC](https://discourse.processing.org/t/whack-a-mole-game/23113 "2020-08-07T18:00:50Z")\
**Posts on this page:** 1\
**Showing post:** 8

<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:** [August 8, 2020, 10:45pm UTC](https://discourse.processing.org/t/whack-a-mole-game/23113/8 "2020-08-08T22:45:00Z")

</div>

Hello,

> [@chemicalite](#):
>
> make actual moles appear and disappear in specific locations and stay there for a few seconds!

I wrote an example that used an array of objects (balls) filling the canvas with different colors, radii, random locations and random _time to live_ in a class to create these objects. I added the _time to live_ last once everything else was working. There is a learning curve to doing this but worth it and can easily be updated.

This is a _very minimal example_ implementing “time to live” for one ball without a class.

You can try to integrate it (or something similar) into an array of objects (balls or buttons) in a class or with your example.

```auto
boolean b = false;

void setup()
  {
  size(700, 700);
  x = width/2;
  y = height/2;
  ttl = 2;
  noStroke();
  }

void draw()
  {
  background(255);

  if (frameCount%(3*60) == 0) //Every 3s
    {
    ttl = frameCount + int(random(0.5*60, 2*60)); //random ttl .5 to 2 s
    b = true;
    } 

  if (b == true)
    {  
    fill(100);
    circle(x, y, 200);
    }
  
  if (frameCount > ttl) 
    {
    b = false;
    }
  }

```

References:

> **[Array Objects / Examples](https://processing.org/examples/arrayobjects.html)**
>
> Demonstrates the syntax for creating an array of custom objects.

I do not support or endorse the whacking of moles. `:)`

`:)`

---

_[View the full topic](https://discourse.processing.org/t/whack-a-mole-game/23113)._
