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