We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I wanted to change the aspects of a previous game I posted on here before but this time I built it again from the ground up (code below) and managed to get classes working this time.
I wanted to have it so that when a ball connects with a colored box, the ball changes to the color of the box (e.g. ball touches blue box, ball becomes blue, then if it touches green box, blue ball becomes green, etc.)
This is the code I have:
Ball b1 = new Ball();
Ball b2 = new Ball();
Ball b3 = new Ball();
Ball b4 = new Ball();
Chamber c1 = new Chamber();
Chamber c2 = new Chamber();
Chamber c3 = new Chamber();
Chamber c4 = new Chamber();
Chamber c5 = new Chamber();
Chamber c6 = new Chamber();
void setup() {
size(640, 440);
smooth();
b2.ballY = 80;
b2.ballXV = 3;
b2.ballYV = 5;
b3.ballX = 100;
b3.ballY = 150;
b3.ballYV = 5;
b4.ballX = 300;
b4.ballY = 300;
b4.ballXV = 6;
//red
c1.chamberX = 0;
c1.chamberY = 0;
//blue
c2.chamberX = 600;
c2.chamberY = 0;
//green
c3.chamberX = 0;
c3.chamberY = 400;
//purple
c4.chamberX = 600;
c4.chamberY = 400;
//yellow
c5.chamberX = 300;
c5.chamberY = 0;
//cyan
c6.chamberX = 300;
c6.chamberY = 400;
}
void draw() {
background(240);
b1.drawAndMoveBall();
b2.drawAndMoveBall();
b3.drawAndMoveBall();
b4.drawAndMoveBall();
//red
c1.drawChamber1();
//blue
c2.drawChamber2();
//green
c3.drawChamber3();
//purple
c4.drawChamber4();
//yellow
c5.drawChamber5();
//cyan
c6.drawChamber6();
}
class Ball {
int ballX = 50;
int ballY = 50;
int ballXV = 4;
int ballYV = 2;
int ballDimension = 25;
int ballRadius = ballDimension/2;
color c;
void drawAndMoveBall() {
ballX += ballXV;
ballY += ballYV;
//checks to see if ball touches edges and bounces back
if (ballX + ballRadius > width) {
ballXV = -ballXV;
}
if (ballX - ballRadius < 0) {
ballXV = -ballXV;
}
if (ballY + ballRadius > height) {
ballYV = -ballYV;
}
if (ballY - ballRadius < 0) {
ballYV = -ballYV;
}
c = color(255);
strokeWeight(2);
fill(c);
ellipse(ballX, ballY, ballDimension, ballDimension);
}
}
class Chamber {
int chamberX = 0;
int chamberY = 0;
void drawChamber1() {
fill(255, 0, 0);
rect(chamberX, chamberY, 40, 40);
}
void drawChamber2() {
fill(0, 255, 0);
rect(chamberX, chamberY, 40, 40);
}
void drawChamber3() {
fill(0, 0, 255);
rect(chamberX, chamberY, 40, 40);
}
void drawChamber4() {
fill(255, 0, 255);
rect(chamberX, chamberY, 40, 40);
}
void drawChamber5() {
fill(255, 255, 0);
rect(chamberX, chamberY, 40, 40);
}
void drawChamber6() {
fill(0, 255, 255);
rect(chamberX, chamberY, 40, 40);
}
}
Answers
A minor first step. but an important one.
Thanks @TfGuy44, made those changes.
Now I need the color advice, hopefully by tonight since this project is due tomorrow :D
Before advancing any further, you should seriously consider learning about arrays:
That is, rather than having a series of b1, b2, b3, b4 labels, we'd have everything under 1 array label accessible w/ index:
Doing so allows us to traverse the whole thing using some loop:
This simplifies the code a lot, believe me! :-\"
Also, you should add some constructor to your classes:
This way you can pass its initial values at the same time of using
new
:Noticed also that you have 6 drawChamber() methods for class Chamber.
But the only diff. among them is its fill()'s value! :|
Instead you shoulda declared a field to store it and pass it along its constructor:
Well, here's the whole tweaks I've made.
I hope it's gonna be easier to add some collision method later on: ;)
Boop.
Wow thanks a bunch, @GoToLoop!
So if I wanted to work on the coloring the balls how would I go about it? I'm assuming I would need to make an "if" statement on draw?
Dang I have much slow. 8-|
for ()
loop! :(|)boolean colliding(Chamber c) { // ... }
How exactly do I check them as circular shapes? I haven't had any luck thus far getting this collision to work. :-SS
Can you show us your attempt at least? ~O)
Only did it for the first ball so far but here:
And yes I added two more chambers :D
if ()
blocks! @-)sq(c.x - b.x) + sq(c.y - b.y) <= sq(c.RAD + b.RAD);
The extra Chamber.RAD in
c.x + Chamber.RAD
&c.y + Chamber.RAD
is to offset Chamber's rect() top-left based CORNER coordinates into CENTER coordinates just like that of Ball's ellipse().And I've also turned
for (Ball b : balls) b.script();
into a double loop:So each Ball is checked against each Chamber.
If method colliding() returns
true
, it assigns that specific Chamber's c variable to current Ball's also c variable.Then we finally can call Ball's script() method, which ends up display() it w/ the updated
color
. :-bdHere's the whole working sketch. You can watch it online too here: :bz
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o