Need Help with Popping Balloons (Arrays)
in
Programming Questions
•
1 year ago
Hey all. I'm fairly new to processing and I need some help. I've been working on a mini game for my programming class where you "pop" balloons. It involves arrays and I've gotten everything to show up just fine. My problem is getting them to "pop" when I hover the cursor over them. I'm drawing a blank and need some help figuring out what to put under "void pop()" I'm pretty sure the solution is simple. Here's a link to the assignment directions:
http://evc-cit.info/cit020/arrays.html
Here is the code that I have (main tab):
Balloon[] balloon = new Balloon[30];
void setup()
{
size(400, 400);
smooth();
frameRate(20);
for (int i = 0; i < balloon.length; i++)
{
float w = random(20, 46);
balloon[i] = new Balloon(random(50, 350), random(50, 350),
w, w + random(5, 11), makeRandomColor());
}
}
void draw()
{
background(255);
for (int i = 0; i < balloon.length; i++)
{
balloon[i].display();
balloon[i].move();
balloon[i].bounce();
// balloon[i].hit(); <-- This would go here for the balloon pop
}
}
color makeRandomColor()
{
color result = color(
int(random(128, 255)),
int(random(128, 255)),
int(random(128, 255)));
return result;
}
______________________________________________________________________________________
Here the code for the class:
class Balloon
{
float w;
float h;
float x;
float y;
float r;
float g;
float b;
color bColor;
float HSpeed;
float VSpeed;
Balloon(float x, float y, float w, float h, color bColor)
{
this.w = w;
this.h = h;
this.x = x;
this.y = y;
this.bColor = bColor;
this.HSpeed = int(random(3, 8));
this.VSpeed = int(random(3, 7));
}
void display()
{
fill(bColor);
ellipse(x, y, w, h);
}
void move()
{
x = x + HSpeed;
y = y + VSpeed;
}
void bounce()
{
if (x < 0 + w / 2.0 || x > width - w / 2.0)
{
HSpeed = -HSpeed;
}
if (y < 0 + h / 2.0 || y > height - h / 2.0)
{
VSpeed = -VSpeed;
}
}
boolean hit(int pX, int pY)
{
float a = w / 2.0;
float b = h / 2.0;
float ex = pX - x; // calculate relative to center of ellipse
float ey = pY - y;
float r = (ex * ex) / (a * a) + (ey * ey) / (b * b);
return (r < 1);
}
void pop()
{
}
}
I think I should put an "if" statement to set the x and y coordinates of each balloon to -1000 when the cursor touches them but I'm not sure how to go about doing that. Help would be appreciated.
Here is the code that I have (main tab):
Balloon[] balloon = new Balloon[30];
void setup()
{
size(400, 400);
smooth();
frameRate(20);
for (int i = 0; i < balloon.length; i++)
{
float w = random(20, 46);
balloon[i] = new Balloon(random(50, 350), random(50, 350),
w, w + random(5, 11), makeRandomColor());
}
}
void draw()
{
background(255);
for (int i = 0; i < balloon.length; i++)
{
balloon[i].display();
balloon[i].move();
balloon[i].bounce();
// balloon[i].hit(); <-- This would go here for the balloon pop
}
}
color makeRandomColor()
{
color result = color(
int(random(128, 255)),
int(random(128, 255)),
int(random(128, 255)));
return result;
}
______________________________________________________________________________________
Here the code for the class:
class Balloon
{
float w;
float h;
float x;
float y;
float r;
float g;
float b;
color bColor;
float HSpeed;
float VSpeed;
Balloon(float x, float y, float w, float h, color bColor)
{
this.w = w;
this.h = h;
this.x = x;
this.y = y;
this.bColor = bColor;
this.HSpeed = int(random(3, 8));
this.VSpeed = int(random(3, 7));
}
void display()
{
fill(bColor);
ellipse(x, y, w, h);
}
void move()
{
x = x + HSpeed;
y = y + VSpeed;
}
void bounce()
{
if (x < 0 + w / 2.0 || x > width - w / 2.0)
{
HSpeed = -HSpeed;
}
if (y < 0 + h / 2.0 || y > height - h / 2.0)
{
VSpeed = -VSpeed;
}
}
boolean hit(int pX, int pY)
{
float a = w / 2.0;
float b = h / 2.0;
float ex = pX - x; // calculate relative to center of ellipse
float ey = pY - y;
float r = (ex * ex) / (a * a) + (ey * ey) / (b * b);
return (r < 1);
}
void pop()
{
}
}
I think I should put an "if" statement to set the x and y coordinates of each balloon to -1000 when the cursor touches them but I'm not sure how to go about doing that. Help would be appreciated.

1