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).
- 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.
- 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?
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);
}
}
}