Help with changing colours and setting boundaries-
in
Programming Questions
•
7 months ago
Hello! I'm really, really new to Processing and for my games programming class we've been enlisted to create a 'bouncing puck' type code that'll see the puck bounce off four walls and change colour when coming into contact with each one, then return to white when it's in the middle, change colour when it hits another and so on. So far I've got a moving puck and a layout- but I can't seem to get the colour to change no matter how much I fiddle with the code, and I've also ran into an issue regarding my boundaries- on two off the walls (the bottom and right wall) the puck will 'bounce off' rather then going into the walls like it does on the top one and the left. I was wondering if anyone knew how I could fix it so the puck would bounce off all the walls rather then two?
We were given some codes that were meant to help us, but no matter what I do it doesn't seem to want to work.
My code currently looks like this;
//ints!
int posX, posY, rad;
int lineX;
//colours!
color lightgrey = color(245, 245, 245);
color midgrey = color(175, 175, 175);
color white = color(255);
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
//floats!
float halfY = height/2;
float halfX = width/2;
float wholeX = width;
float wholeY = height;
float xspeed = 2;
float yspeed = 2;
float x = width*0.5;
float y = height/2.666;
float x1 = 1;
float y1 = 1;
float r = height*0.8;
float quartY = height/4;
float quartY2 = height/1.333;
float circx = width*0.1666;
float rect1 = width*0.9333;
float rect2 = height*0.05;
float rect3 = height*0.95;
void setup ()
{
println ("s2893849");
println ("Tori Maslen");
smooth();
size(300, 400);
//declaring
posX = 0;
posY = height/2;
rad = 10;
xspeed = 2.2;
yspeed = 1.5;
r = height*0.09;
wholeX = width;
wholeY = height;
halfY = height/2;
halfX = width/2;
x = width*0.5;
y = height/2.666;
x1 = 1;
y1 = 1;
quartY = height/4;
quartY2 = height/1.333;
circx = width*0.1666;
rect1 = width*0.9333;
rect2 = height*0.05;
rect3 = height*0.95;
}
void draw()
{
//background setup
background(white);
strokeWeight(4);
stroke(midgrey);
//find the middle.
//line (X, posY, wholeX, posY);
//netball court segments.
line (X, quartY, wholeX, quartY);
line (X, quartY2, wholeX, quartY2);
//println(quartY2);
fill(lightgrey);
//centre circle.
ellipse(halfX, halfY, circx, circx);
//shooters circles.
ellipse(halfX, Y, circx*2, circx*2);
ellipse(halfX, wholeY, circx*2, circx*2);
//borders.
strokeWeight(4);
fill(yellow);
rect(x1, y1, rect2, wholeY);
fill(pink);
rect(rect1, y1, rect2, wholeY);
fill(bblue);
rect(x1, rect3, wholeX, rect2);
fill(ggreen);
rect(x1, y1, wholeX, rect2);
x = x + xspeed;
y = y + yspeed;
if ((x > 260) || (x < 0))
{
xspeed = xspeed * -1;
}
if ((y > 360) || (y < 0))
{
yspeed = yspeed * -1;
}
//setting up the 'puck'.
stroke(midgrey);
strokeWeight(4);
fill(lightgrey);
if (y >= 100 || y1 < 400)
{
fill (45);
}
else if (posY<60)
{
fill(pink); //green
}
else if (posY > 70)
{
fill(0, 0, 255); //blue
}
else
{
fill (255); //white
}
ellipse(x, y, r, r);
posX++;;
if (posX > width)
{
posX = 0;
}
}
We were given some codes that were meant to help us, but no matter what I do it doesn't seem to want to work.
My code currently looks like this;
//ints!
int posX, posY, rad;
int lineX;
//colours!
color lightgrey = color(245, 245, 245);
color midgrey = color(175, 175, 175);
color white = color(255);
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
//floats!
float halfY = height/2;
float halfX = width/2;
float wholeX = width;
float wholeY = height;
float xspeed = 2;
float yspeed = 2;
float x = width*0.5;
float y = height/2.666;
float x1 = 1;
float y1 = 1;
float r = height*0.8;
float quartY = height/4;
float quartY2 = height/1.333;
float circx = width*0.1666;
float rect1 = width*0.9333;
float rect2 = height*0.05;
float rect3 = height*0.95;
void setup ()
{
println ("s2893849");
println ("Tori Maslen");
smooth();
size(300, 400);
//declaring
posX = 0;
posY = height/2;
rad = 10;
xspeed = 2.2;
yspeed = 1.5;
r = height*0.09;
wholeX = width;
wholeY = height;
halfY = height/2;
halfX = width/2;
x = width*0.5;
y = height/2.666;
x1 = 1;
y1 = 1;
quartY = height/4;
quartY2 = height/1.333;
circx = width*0.1666;
rect1 = width*0.9333;
rect2 = height*0.05;
rect3 = height*0.95;
}
void draw()
{
//background setup
background(white);
strokeWeight(4);
stroke(midgrey);
//find the middle.
//line (X, posY, wholeX, posY);
//netball court segments.
line (X, quartY, wholeX, quartY);
line (X, quartY2, wholeX, quartY2);
//println(quartY2);
fill(lightgrey);
//centre circle.
ellipse(halfX, halfY, circx, circx);
//shooters circles.
ellipse(halfX, Y, circx*2, circx*2);
ellipse(halfX, wholeY, circx*2, circx*2);
//borders.
strokeWeight(4);
fill(yellow);
rect(x1, y1, rect2, wholeY);
fill(pink);
rect(rect1, y1, rect2, wholeY);
fill(bblue);
rect(x1, rect3, wholeX, rect2);
fill(ggreen);
rect(x1, y1, wholeX, rect2);
x = x + xspeed;
y = y + yspeed;
if ((x > 260) || (x < 0))
{
xspeed = xspeed * -1;
}
if ((y > 360) || (y < 0))
{
yspeed = yspeed * -1;
}
//setting up the 'puck'.
stroke(midgrey);
strokeWeight(4);
fill(lightgrey);
if (y >= 100 || y1 < 400)
{
fill (45);
}
else if (posY<60)
{
fill(pink); //green
}
else if (posY > 70)
{
fill(0, 0, 255); //blue
}
else
{
fill (255); //white
}
ellipse(x, y, r, r);
posX++;;
if (posX > width)
{
posX = 0;
}
}
1