I am working on a project for a beginner programming course. I am making a simple game where a vacuum character needs to collect small brown circles (rubbish) by moving over them and using a suction device, whilst avoiding moving spikey balls.
My character moves using the mouse and uses his suction device when a key is pressed. I have an array of rubbish set up but I have been struggling to figure out how to remove a specific one when a key is pressed and the mouse/character collides with it. I would also like this to count up a score displayed on screen but this is not a priority.
I have pasted my code below, sorry if it is really messy, I am not very good at this.
TLDR: I want to remove a specific object from an Array when the player has a key pressed and their mouse is colliding with it.
ArrayList<Rubbish> rubbishArray = new ArrayList<Rubbish>();
Spikeyball1 s1;
Spikeyball1 s2;
int x = 0;
int y = 0;
int p = 0;
int borderY = 300;
int gameScreen = 0;
void setup() {
size( 1600, 1000 );
s1 = new Spikeyball1(500, 300);
s2 = new Spikeyball1(1100, 1000);
for (int i = 0; i < 10; i++) {
rubbishArray.add(new Rubbish());
}
}
void draw() {
if (gameScreen == 0) {
initScreen();
} else if (gameScreen == 1) {
gameScreen();
} else if (gameScreen == 2) {
gameOverScreen();
}
}
void initScreen() {
background(0);
textAlign(CENTER);
textSize(50);
text("Click to Start", 800, 500);
}
void gameScreen() {
environment();
noCursor();
if (rubbishArray.size() < 10) {
rubbishArray.add(new Rubbish());
}
for (Rubbish r : rubbishArray) {
r.move();
r.display();
}
//top border
if (mouseY<borderY) mouseY = borderY;
//move vacuum
if (keyPressed) p = 20;
else p = 0;
//spikeyballs
s1.move();
s1.display();
s2.move();
s2.display();
s1.collision();
s2.collision();
//character
vacuum(mouseX, mouseY+p);
characterA(mouseX, mouseY);
}
void gameOverScreen() {
background(210);
textAlign(CENTER);
textSize(50);
text("Game Over!", 800, 400);
text("Click to Restart", 800, 600);
}
void startGame() {
gameScreen=1;
}
void mousePressed() {
if (gameScreen==0) {
startGame();
}
if (gameScreen==2) {
startGame();
}
}
void characterA(float x, float y) {
stroke( 0 );
strokeWeight( 2 );
fill(0, 0, 102);
ellipse(x-25, y+10, 15, 15);
ellipse(x-8, y+10, 15, 15);
ellipse(x+9, y+10, 15, 15);
ellipse(x+26, y+10, 15, 15);
fill(0, 76, 153);
quad(x+5, y-73, x+8, y-103, x+18, y-100, x+15, y-70);
quad(x-15, y-70, x-18, y-100, x-8, y-103, x-5, y-73);
fill(0, 76, 153);
rect(x-35, y-45, 70, 50);
fill(0, 100, 153);
arc(x, y-45, 70, 60, PI, TWO_PI);
fill(255);
ellipse(x-13, y-100, 15, 15);
ellipse(x+13, y-100, 15, 15);
fill(0);
ellipse(x-13, y-100, 5, 5);
ellipse(x+13, y-100, 5, 5);
noFill();
arc(x, y-65, 20, 20, 0, PI);
}
void vacuum(float x, float y) {
stroke( 0 );
strokeWeight( 2 );
fill(0, 100, 153);
quad(x+25, y-30, x+35, y-20, x+65, y-50, x+55, y-60);
fill(0, 76, 153);
quad(x+45, y-50, x+75, y-20, x+85, y-30, x+55, y-60);
fill(0, 100, 153);
triangle(x+56, y-13, x+96, y-13, x+76, y-43);
}
void environment() {
fill(150);
quad(-500, 300, -200, 300, 100, 1000, -200, 1000);
fill(125);
quad(-200, 300, 100, 1000, 400, 1000, 100, 300);
fill(150);
quad(400, 1000, 100, 300, 400, 300, 700, 1000);
fill(125);
quad(400, 300, 700, 1000, 1000, 1000, 700, 300);
fill(150);
quad(700, 300, 1000, 1000, 1300, 1000, 1000, 300);
fill(125);
quad(1000, 300, 1300, 1000, 1600, 1000, 1300, 300);
fill(150);
quad(1300, 300, 1600, 1000, 1900, 1000, 1600, 300);
fill(220);
rect(-10, 0, 1650, 100);
fill(200);
rect(-10, 100, 1650, 100);
fill(220);
rect(-10, 200, 1650, 100);
fill(10);
rect(1000, 100, 300, 100);
beginShape();
fill(240);
vertex(300, 300);
vertex(450, 300);
vertex(450, 0);
vertex(350, 0);
vertex(300, 100);
endShape(CLOSE);
beginShape();
vertex(600, 300);
vertex(450, 300);
vertex(450, 0);
vertex(550, 0);
vertex(600, 100);
endShape(CLOSE);
}
class Rubbish {
float x;
float y;
float speed = random(2, 5);
float r = random(80, 150);
float g = random(45, 55);
float b = 0;
Rubbish() {
x = 0;
y = random(400, 900);
}
void move() {
x+= speed;
if (x>1600) {
x=0;
}
}
void display() {
fill(r, g, b);
ellipse(x, y, 30, 30);
}
}
class Spikeyball1 {
float xpos;
float ypos;
float r;
boolean movingUp = true;
int m = 0;
boolean collision;
Spikeyball1(float tempXpos, float tempYpos) {
xpos = tempXpos;
ypos = tempYpos;
}
void move() {
if (movingUp) ypos += 3;
else ypos -= 3;
if (ypos<300||ypos>1000) movingUp = !movingUp;
}
void collision() {
if ( mouseX >= xpos-50 && mouseX <= xpos+50 && mouseY >= ypos-50 && mouseY <= ypos+100) {
gameScreen = 2;
};
}
void display() {
stroke( 0 );
strokeWeight( 2 );
fill(60, 1, 1);
beginShape();
vertex(xpos, ypos-50);
vertex(xpos+14, ypos-20);
vertex(xpos+47, ypos-15);
vertex(xpos+23, ypos+7);
vertex(xpos+29, ypos+40);
vertex(xpos, ypos+25);
vertex(xpos-29, ypos+40);
vertex(xpos-23, ypos+7);
vertex(xpos-47, ypos-15);
vertex(xpos-14, ypos-20);
endShape(CLOSE);
fill(200, 20, 10);
ellipse(xpos, ypos, 50, 50);
fill(255);
ellipse(xpos+8, ypos-5, 10, 10);
ellipse(xpos-8, ypos-5, 10, 10);
fill(0);
ellipse(xpos+8, ypos-5, 3, 3);
ellipse(xpos-8, ypos-5, 3, 3);
noFill();
arc(xpos, ypos+15, 25, 20, PI, TWO_PI);
}
}