Question regarding ball trail.
in
Programming Questions
•
6 months ago
Hello!
I've been working on a code for my programming class (I'm currently studying to be a concept designer, but unfortunately this is mandatory!) and I've ran into a bit of an issue-
in the final product, we're supposed to have the puck bounce around and change colours on impact to each coloured wall (there's four walls acting as boundaries which are all different colours), but have a trail as well.
In previous versions we just had to get the puck to change colour and return to the original colour, which I was able to do with help from you lovely people. However, I'm puzzled as to why after putting in the code for the trail, the puck will no longer change colours?
Also, I was curious if anyone could help me with the hit count- i can't seem to get it to count actual impacts. :(
(I apologize for the messy code- I'm not very good at this!)
The current code I'm working on is:
// Variables
float halfX, halfY, vx, vy, startX, startY, speedx, speedy, wholeX, wholeY, d=18, r=9, quartX, quartY, halfX2, halfY2; // Puck's position, velocity, and radius
color c; // Puck's color
color p; //pink
color b; //blue
color g; //green
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
color midgrey = color(175);
color white = color(255);
color lightgrey = color(245);
color ballcolour = color(245, 245, 245);
float x, y, mx, my;
int num = 15;
float[] posx = new float[num];
float[] posy = new float[num];
int bar=16; // Thickness of side bars
int hit= 0; //hit count
void setup ()
{
size(400, 300);
mx = 10;
my = 5;
// Initial position, velocity, and color of the puck
//had to bring in halfX2 and halfY2 because everytime I used halfY and halfX, the circles for the background decided to move.
halfY2 = height/2;
halfX2 = width/2;
x = width*0.5;
y = height/2.666;
wholeX = width;
wholeY = height;
halfX = width/2;
halfY = height/2;
speedx = 2.2;
speedy = 1.5;
c = color(245);
startX = 0;
startY = 0;
quartX = width/4;
quartY = height/4;
// Static drawing settings
smooth();
strokeWeight(4);
stroke(midgrey);
}
void draw()
{
background(white);
// Lines
line (quartX, startY, quartX, wholeY);
line (3*quartX, startY, 3*quartX, wholeY);
fill(lightgrey);
// Centre circle
ellipse(halfX2, halfY2, wholeX/6, wholeX/6);
// Shooters circles
ellipse(0, halfY2, wholeX/3, wholeX/3);
ellipse(wholeX, halfY2, wholeX/3, wholeX/3);
// Borders
fill(yellow); // Yellow - left
rect(startX, startY, bar, wholeY);
fill(pink); // Pink - right
rect(wholeX-bar, startY, bar, wholeY);
fill(bblue); // Blue - bottom
rect(startX, wholeY-bar, wholeX, bar);
fill(ggreen); // Green - top
rect(startX, startY, wholeX, bar);
//PADDLES
if (halfY >wholeY-bar*2-r*4|| halfY<bar+r) {
speedy= speedy * -1;
}
halfX+=speedx;
halfY+=speedy;
rect(quartX-bar*3, halfY, quartX/3, wholeY/6);
rect(quartX*3+bar, halfY, quartX/3, wholeY/6);
//BALL & FADE
for (int i = num - 1; i > 0; i --) {
posx[i] = posx[i-1];
posy[i] = posy[i-1];
}
//1st element.
posx[0] = x;
posy[0] = y;
// ball position.
x += mx;
y += my;
//boundries
if (x < 0 + bar + d|| x > width-bar-d) {
mx = -mx;
}
if (y < 0 + bar+d|| y > height-bar-d) {
my = -my;
}
// and draw the trail
for (int i = 0; i < num; i++) {
fill(245, 245 - (245/num)*i);
// Draw puck
ellipse(posx[i], posy[i], d, d);
//draw paddles
// Determine puck color
//right wall.
if (posx[i]>wholeX-bar-d) {
fill(pink);
println("turn pink!");
}
//left wall.
if (posx[i]<bar+d) {
fill(yellow);
println("turn yellow!");
}
//bottom wall.
if (posy[i]>wholeY-bar-d) {
fill(bblue);
println("turn blue!");
}
//top wall.
if (posy[i]<bar+d) {
fill(ggreen);
println("turn green!");
}
//hit count
//can't seem to get it to work in manner of increasing rather then
//just showing the hit.
int hitsx = 0;
//startX, startY, bar, wholeY
if(posx[i] > 0 + bar + d) {
hitsx += 1;
}
println("hitsx: " + hitsx);
int hitsy = 0;
if(posy[i] > 0 + bar+d) {
hitsy += 1;
}
println("hitsy: " + hitsy);
//colour change back.
//colour stops changing eventually.
// if 200(ball) is more then 100 and 200 is less then 300 = grey.
// else if (halfX>quartX&&halfX<3*quartX) {
// c=color(lightgrey);
}
// if 150(ball) is more then 75 and 150 is less then 225 = grey.
// else if (halfY>quartY&&halfY<3*quartY) {
// c=color(lightgrey);
}
Previous code:
// Variables
float halfX, halfY, vx, vy, startX, startY, speedx, speedy, wholeX, wholeY, r=18, x, y, quartX, quartY, halfX2, halfY2; // Puck's position, velocity, and radius
color c; // Puck's color
color p; //pink
color b; //blue
color g; //green
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
color midgrey = color(175);
color white = color(255);
color lightgrey = color(245);
color ballcolour = color(245, 245, 245);
int bar=16; // Thickness of side bars
void setup ()
{
size(400, 300);
// Initial position, velocity, and color of the puck
//had to bring in halfX2 and halfY2 because everytime I used halfY and halfX, the circles for the background decided to move.
halfY2 = height/2;
halfX2 = width/2;
x = width*0.5;
y = height/2.666;
wholeX = width;
wholeY = height;
halfX = width/2;
halfY = height/2;
speedx = 2.2;
speedy = 1.5;
c = color(245);
startX = 0;
startY = 0;
quartX = width/4;
quartY = height/4;
// Static drawing settings
smooth();
strokeWeight(4);
stroke(midgrey);
}
void draw()
{
background(white);
// Lines
line (quartX, startY, quartX, wholeY);
line (3*quartX, startY, 3*quartX, wholeY);
fill(lightgrey);
// Centre circle
ellipse(halfX2, halfY2, wholeX/6, wholeX/6);
// Shooters circles
ellipse(0, halfY2, wholeX/3, wholeX/3);
ellipse(wholeX, halfY2, wholeX/3, wholeX/3);
// Borders
fill(yellow); // Yellow - left
rect(startX, startY, bar, wholeY);
fill(pink); // Pink - right
rect(wholeX-bar, startY, bar, wholeY);
fill(bblue); // Blue - bottom
rect(startX, wholeY-bar, wholeX, bar);
fill(ggreen); // Green - top
rect(startX, startY, wholeX, bar);
// Draw puck
fill(c);
ellipse(halfX, halfY, 2*r, 2*r);
//keeps ball inside wall boundries.
if ( halfX >wholeX-bar-r || halfX<bar+r) {
speedx = speedx * -1;
}
if (halfY >wholeY-bar-r|| halfY<bar+r) {
speedy= speedy * -1;
}
halfX+=speedx;
halfY+=speedy;
// Determine puck color
//colour sops changing eventually.
//right wall.
// if halfX(ball)[200] is more then 400 minus the bar width (16) minus the ball size (18) ball colour = pink.
if (halfX>wholeX-bar-r) {
c= color(pink);
}
//left wall.
// if halfX(ball)[200] is less then the bar width (16) plus the ball size (18) ball colour = yellow.
if (halfX<bar+r) {
c= color(yellow);
}
//bottom wall.
// if halfY (ball[150]) is more then 300 minus the bar width, minus the ball size ball colour= blue.
if (halfY>wholeY-bar-r) {
c= color(bblue);
}
//top wall.
//if halfY (ball [150]) is less then the bar width (16)plus the ball size (18) ball colour = green.
if (halfY<bar+r) {
c= color(ggreen);
}
//colour change back.
// if 200(ball) is more then 100 and 200 is less then 300 = grey.
else if (halfX>quartX&&halfX<3*quartX) {
c=color(lightgrey);
}
// if 150(ball) is more then 75 and 150 is less then 225 = grey.
else if (halfY>quartY&&halfY<3*quartY) {
c=color(lightgrey);
}
}
//This puck wouldn't stay within the four walls, would only bounce back off two and go into the coloured zones on the top and left walls.
// // Draw puck
// fill(c);
// ellipse(x, y, 2*r, 2*r);
any help would be greatly appreciated!
I've been working on a code for my programming class (I'm currently studying to be a concept designer, but unfortunately this is mandatory!) and I've ran into a bit of an issue-
in the final product, we're supposed to have the puck bounce around and change colours on impact to each coloured wall (there's four walls acting as boundaries which are all different colours), but have a trail as well.
In previous versions we just had to get the puck to change colour and return to the original colour, which I was able to do with help from you lovely people. However, I'm puzzled as to why after putting in the code for the trail, the puck will no longer change colours?
Also, I was curious if anyone could help me with the hit count- i can't seem to get it to count actual impacts. :(
(I apologize for the messy code- I'm not very good at this!)
The current code I'm working on is:
// Variables
float halfX, halfY, vx, vy, startX, startY, speedx, speedy, wholeX, wholeY, d=18, r=9, quartX, quartY, halfX2, halfY2; // Puck's position, velocity, and radius
color c; // Puck's color
color p; //pink
color b; //blue
color g; //green
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
color midgrey = color(175);
color white = color(255);
color lightgrey = color(245);
color ballcolour = color(245, 245, 245);
float x, y, mx, my;
int num = 15;
float[] posx = new float[num];
float[] posy = new float[num];
int bar=16; // Thickness of side bars
int hit= 0; //hit count
void setup ()
{
size(400, 300);
mx = 10;
my = 5;
// Initial position, velocity, and color of the puck
//had to bring in halfX2 and halfY2 because everytime I used halfY and halfX, the circles for the background decided to move.
halfY2 = height/2;
halfX2 = width/2;
x = width*0.5;
y = height/2.666;
wholeX = width;
wholeY = height;
halfX = width/2;
halfY = height/2;
speedx = 2.2;
speedy = 1.5;
c = color(245);
startX = 0;
startY = 0;
quartX = width/4;
quartY = height/4;
// Static drawing settings
smooth();
strokeWeight(4);
stroke(midgrey);
}
void draw()
{
background(white);
// Lines
line (quartX, startY, quartX, wholeY);
line (3*quartX, startY, 3*quartX, wholeY);
fill(lightgrey);
// Centre circle
ellipse(halfX2, halfY2, wholeX/6, wholeX/6);
// Shooters circles
ellipse(0, halfY2, wholeX/3, wholeX/3);
ellipse(wholeX, halfY2, wholeX/3, wholeX/3);
// Borders
fill(yellow); // Yellow - left
rect(startX, startY, bar, wholeY);
fill(pink); // Pink - right
rect(wholeX-bar, startY, bar, wholeY);
fill(bblue); // Blue - bottom
rect(startX, wholeY-bar, wholeX, bar);
fill(ggreen); // Green - top
rect(startX, startY, wholeX, bar);
//PADDLES
if (halfY >wholeY-bar*2-r*4|| halfY<bar+r) {
speedy= speedy * -1;
}
halfX+=speedx;
halfY+=speedy;
rect(quartX-bar*3, halfY, quartX/3, wholeY/6);
rect(quartX*3+bar, halfY, quartX/3, wholeY/6);
//BALL & FADE
for (int i = num - 1; i > 0; i --) {
posx[i] = posx[i-1];
posy[i] = posy[i-1];
}
//1st element.
posx[0] = x;
posy[0] = y;
// ball position.
x += mx;
y += my;
//boundries
if (x < 0 + bar + d|| x > width-bar-d) {
mx = -mx;
}
if (y < 0 + bar+d|| y > height-bar-d) {
my = -my;
}
// and draw the trail
for (int i = 0; i < num; i++) {
fill(245, 245 - (245/num)*i);
// Draw puck
ellipse(posx[i], posy[i], d, d);
//draw paddles
// Determine puck color
//right wall.
if (posx[i]>wholeX-bar-d) {
fill(pink);
println("turn pink!");
}
//left wall.
if (posx[i]<bar+d) {
fill(yellow);
println("turn yellow!");
}
//bottom wall.
if (posy[i]>wholeY-bar-d) {
fill(bblue);
println("turn blue!");
}
//top wall.
if (posy[i]<bar+d) {
fill(ggreen);
println("turn green!");
}
//hit count
//can't seem to get it to work in manner of increasing rather then
//just showing the hit.
int hitsx = 0;
//startX, startY, bar, wholeY
if(posx[i] > 0 + bar + d) {
hitsx += 1;
}
println("hitsx: " + hitsx);
int hitsy = 0;
if(posy[i] > 0 + bar+d) {
hitsy += 1;
}
println("hitsy: " + hitsy);
//colour change back.
//colour stops changing eventually.
// if 200(ball) is more then 100 and 200 is less then 300 = grey.
// else if (halfX>quartX&&halfX<3*quartX) {
// c=color(lightgrey);
}
// if 150(ball) is more then 75 and 150 is less then 225 = grey.
// else if (halfY>quartY&&halfY<3*quartY) {
// c=color(lightgrey);
}
Previous code:
// Variables
float halfX, halfY, vx, vy, startX, startY, speedx, speedy, wholeX, wholeY, r=18, x, y, quartX, quartY, halfX2, halfY2; // Puck's position, velocity, and radius
color c; // Puck's color
color p; //pink
color b; //blue
color g; //green
color yellow = color(252, 255, 183);
color pink = color(255, 193, 193);
color bblue = color(222, 241, 255);
color ggreen = color(222, 255, 227);
color midgrey = color(175);
color white = color(255);
color lightgrey = color(245);
color ballcolour = color(245, 245, 245);
int bar=16; // Thickness of side bars
void setup ()
{
size(400, 300);
// Initial position, velocity, and color of the puck
//had to bring in halfX2 and halfY2 because everytime I used halfY and halfX, the circles for the background decided to move.
halfY2 = height/2;
halfX2 = width/2;
x = width*0.5;
y = height/2.666;
wholeX = width;
wholeY = height;
halfX = width/2;
halfY = height/2;
speedx = 2.2;
speedy = 1.5;
c = color(245);
startX = 0;
startY = 0;
quartX = width/4;
quartY = height/4;
// Static drawing settings
smooth();
strokeWeight(4);
stroke(midgrey);
}
void draw()
{
background(white);
// Lines
line (quartX, startY, quartX, wholeY);
line (3*quartX, startY, 3*quartX, wholeY);
fill(lightgrey);
// Centre circle
ellipse(halfX2, halfY2, wholeX/6, wholeX/6);
// Shooters circles
ellipse(0, halfY2, wholeX/3, wholeX/3);
ellipse(wholeX, halfY2, wholeX/3, wholeX/3);
// Borders
fill(yellow); // Yellow - left
rect(startX, startY, bar, wholeY);
fill(pink); // Pink - right
rect(wholeX-bar, startY, bar, wholeY);
fill(bblue); // Blue - bottom
rect(startX, wholeY-bar, wholeX, bar);
fill(ggreen); // Green - top
rect(startX, startY, wholeX, bar);
// Draw puck
fill(c);
ellipse(halfX, halfY, 2*r, 2*r);
//keeps ball inside wall boundries.
if ( halfX >wholeX-bar-r || halfX<bar+r) {
speedx = speedx * -1;
}
if (halfY >wholeY-bar-r|| halfY<bar+r) {
speedy= speedy * -1;
}
halfX+=speedx;
halfY+=speedy;
// Determine puck color
//colour sops changing eventually.
//right wall.
// if halfX(ball)[200] is more then 400 minus the bar width (16) minus the ball size (18) ball colour = pink.
if (halfX>wholeX-bar-r) {
c= color(pink);
}
//left wall.
// if halfX(ball)[200] is less then the bar width (16) plus the ball size (18) ball colour = yellow.
if (halfX<bar+r) {
c= color(yellow);
}
//bottom wall.
// if halfY (ball[150]) is more then 300 minus the bar width, minus the ball size ball colour= blue.
if (halfY>wholeY-bar-r) {
c= color(bblue);
}
//top wall.
//if halfY (ball [150]) is less then the bar width (16)plus the ball size (18) ball colour = green.
if (halfY<bar+r) {
c= color(ggreen);
}
//colour change back.
// if 200(ball) is more then 100 and 200 is less then 300 = grey.
else if (halfX>quartX&&halfX<3*quartX) {
c=color(lightgrey);
}
// if 150(ball) is more then 75 and 150 is less then 225 = grey.
else if (halfY>quartY&&halfY<3*quartY) {
c=color(lightgrey);
}
}
//This puck wouldn't stay within the four walls, would only bounce back off two and go into the coloured zones on the top and left walls.
// // Draw puck
// fill(c);
// ellipse(x, y, 2*r, 2*r);
any help would be greatly appreciated!
1