Whack-A-Mole Game

Hello there!

I am a somewhat beginner in Processing. So far, all I’ve learned is for and while loops, simple animation, if and if else statements, drawing primitives, and some other related things along with relative basics.

I want to code a “whack-a-mole” game, with six holes where moles can appear. I created the visual aspect of it, but I don’t know how I can randomize moles appearing in the specific locations I want them to appear in (the six holes) and how I can keep them there for a specified period of time. I know how I can count and record how many times the player has hit a mole, but I don’t know how to make actual moles appear and disappear in specific locations and stay there for a few seconds!

This is probably a very dumb question with a simple answer, but any help would be appreciated. Thank you!

1 Like

There is a method called random.

float random_number=random(lowest_value,highest_value);

The number will always been in between them. So if you add a cast to int it will never have the highest value.

2 Likes

That’s great! I was thinking of using random, though I don’t want my “moles” to appear in random locations on the canvas, I want them to randomly appear in one of 6 preset locations. What should I do if I wanted to do that?

1 Like

Use something like

if(random_number==0){x=…;y=…;}
if(random_number==1){x=…;y=…;}
.
.
.

or if you are familiar you could use switch-case expressions.
If you aren’t it looks like this:

switch(random_number){
case 1:
//do stuff
break;
case 2:
//do stuff
break;
.
.
.
default:
//do anything that  doesn't meet any of this conditions.
break;
}
2 Likes

Thank you! That’s very helpful, I implemented it into my code and it works.
Although, now I just need to figure out how to make the “moles” disappear after a certain period of time and to make sure each one appearing happens right after the last one disappears!

Would anyone be able to help?

1 Like

Here is a simple concept, I tried to explain it best as I could:

//this is the timer variable
int t = 0;
//this is our mole's x loaction
int x = 110;
//this is our random number which will help us determine the mole's location
int r = int(random(1,4));


void setup() {
  size(640, 640);
  frameRate(60);
}

void draw() {
  background(255);
  //t++ means t is going to go up by 1 every frame 
  //(60 times a second cause the frameRate() is set to 60)
  //like this: 1,2,3,4,5,6,7,8,9...
  t++;
  //so what we are doing here in the next line is we say:
  //if t is bigger than 60 (which happens every second)
  //we reset t and make r a new random number between 1 and 3
  if (t > 60) {
    t = 0;
    r = int(random(1,4));
  }
  
  //here we work with our random r number
  //if r is 1, then the mole is on the first circle
  //if r is 2, he is on the second
  //if r is 3, he is on the third
  //this is a simple concept you can greatly improve upon this
  if (r == 1) x = 110;
  else if (r == 2) x = 220;
  else if (r == 3) x = 330;
  
  //draw our circles
  fill(0);
  ellipse(110,320,100,100);
  ellipse(220,320,100,100);
  ellipse(330,320,100,100);
  
  //draw our mole
  fill(150);
  ellipse(x,320,70,70);
}

References:
https://processing.org/reference/random_.html
https://processing.org/reference/intconvert_.html
https://processing.org/reference/frameRate_.html

Now you need to make it so when the player clicks on the mole, it goes to another circle. And you should also make more circles. I’ll leave you to those :smiley:

3 Likes

Hey there !

The solutions here are awesome, but I decided to give a take on a certain solution myself!

The point of my solution, as you said you are relatively new is to showcase you the power of what you can do with a programming language if you really dig into a programming mindset.

The solution with utilizing a switch statement or a if else if, it great ! But you we can take it a step further.

I don’t know if you have came across classes but we can make something like this.

class Mole {

  PVector pos;
  int size;
  Mole(PVector pos, int size) {
    this.pos = pos;
    this.size = size;
  }


  public void show() {
    fill(255, 0, 0);
    rect(pos.x, pos.y, size, size);
  }
}

So now what we can do is produce a mole, epic games!
Now with classes you can produce objects which are instances of that class (it like little aliens which have the genetic code of that class).

To make stuff easier we can store our objects within a data structure called array with this they can all be kept in one place and we don’t have to write them all out one by one if we want to use many!

So let’s make an array of moles!

Mole moles[];

Awesome, awesome! Now this is cool and all but currently this array doesn’t contain any moles, so we gotta fill it up using a for loop.

moles = new Mole[5];
for (int i = 0; i < moles.length; i++) {
    moles[i] = new Mole(new PVector( (i * 100) + 100, height / 2), 30);
  }

Okay nice! So now we have said, that there’s 5 slots for moles within our array and now we are gonna use the for loop to look at each slot and put in a mole in there.

Now this gives us a lot of power and we can do different kind of stuff. But for our case what we are interested in is looking at some random mole in point in time. Since we have the show() method and all our moles are within our array. What we can simply do is just pick a random number and just look up a random mole within the array and he will just light up (show up)!


r =(int) random(0, moles.length);

if (frameCount % 60 == 0) {
    r = (int) random(0, moles.length);
}

moles[r].show();

Oh this is real, real cool stuff! Our code is capable of handling any amount of moles and any amount of looking up! Very very cool.

Here the frameCount % 60 is utilized to pick a number after processing is able to count that 60 frames were drawn on screen and then our program is able to pick a new mole. If we just picked a new mole every time within draw() (each frame) it be so so quick be barely able to see it (feel free to increase/decrease this number I recommend sticking to pairs of 60’s so like, 120, 180…)

Well that is my explanation and go at your epic awesome mole game! Here’s the full code! Happy coding!

Mole moles[];
int r;

void setup() {
  size(800, 600);
  moles = new Mole[5];

  for (int i = 0; i < moles.length; i++) {
    moles[i] = new Mole(new PVector( (i * 100) + 100, height / 2), 30);
  }

  r =(int) random(0, moles.length);
  
}

void draw() {
  background(0);
  if (frameCount % 60 == 0) {
    r = (int) random(0, moles.length);
  }

  moles[r].show();
}


class Mole {

  PVector pos;
  int size;
  Mole(PVector pos, int size) {
    this.pos = pos;
    this.size = size;
  }
  

  public void show() {
    fill(255, 0, 0);
    rect(pos.x, pos.y, size, size);
  }
}

Extra resources

Classes
PVector
frameCount

6 Likes

Hello,

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.

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:

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

:)

4 Likes

You could add something like a ‘state’ (hiding, rising, waiting,whacked,falling…) and some time reference to your mole class, do you can put all the behavioir of your critters into their clsss.

1 Like

Thank you, everyone! I was able to figure it out with your help! I was even able to code up a nice arcade 8-bit design for it :slight_smile:

Everyone was really helpful, thanks to all of you again!

3 Likes