Hi @noel thanks for answering me! I have a problem. I don’t know what I did wrong but this is the exercise from the beginning. Now the number on the ball doesn’t appear anymore can you help me? I’m stuck
/ *
- Pass the exercise to logic of arrays and cycles for
- Each bounce will have a function that increases the value of the counter per ball.
- If the counter is greater than 10, stop drawing the ball
- When all the balls disappear print a message that says: “I love the code”
// initial variables for the ball
int bounces = 0;
float x = 0;
float y = 0;
float xspeed = 0;
float yspeed = 0;
boolean alive = true;
C color;
// variable for the background color
bgc color;
void setup ()
{
// we assign the dimensions of the canvas
size (700, 700);
// we assign that the ball will not have a stroke
noStroke ();
// we assign initial values to the variables of the ball
x = random (width);
y = random (height);
xspeed = random (-5, 5);
yspeed = random (-5, 5);
c = color (random (100, 255), random (100, 255), random (100, 255));
// we assign initial values for the background color
bgc = color (random (100, 255), random (100, 255), random (100, 255));
// we assign the font size of the letter
textSize (50);
// we justify the text vertically and horizontally centered
textAlign (CENTER, CENTER);
}
void draw ()
{
//black background
background (bgc);
// we increase the position of the object by its speed to achieve the illusion of movement
x + = xspeed;
y + = yspeed;
// logic for bouncing the ball
if (x <0 || x> width)
{
xspeed * = -1;
}
if (y <0 || y> height)
{
yspeed * = -1;
}
// we draw the ball
fill (255);
ellipse (x, y, 50, 50);
fill (0);
text (bounces, x, y);
}
//This is how I solved it:
/ *
- Pass the exercise to logic of arrays and cycles for
- Each bounce will have a function that increases the value of the counter per ball.
- If the counter is greater than 10, stop drawing the ball
- When all the balls disappear print a message that says: “I love the code”
// initial variables for the ball
int bounces = 10;
int ellipse = 5;
float [] x = new float [bounces];
float [] y = new float [bounces];
float [] xspeed = new float [bounces];
float [] yspeed = new float [bounces];
int [] types = new int [bounces];
boolean alive = true;
C color;
// variable for the background color
bgc color;
void setup ()
{
// we assign the dimensions of the canvas
size (700, 700);
// we assign that the ball will not have a stroke
noStroke ();
// we assign initial values to the variables of the ball
for (int i = 0; i <ellipse; ++ i)
{
x [i] = random (width);
y [i] = random (height);
xspeed [i] = random (-5, 5);
yspeed [i] = random (-5, 5);
c = color (random (100, 255), random (100, 255), random (100, 255));
// we assign initial values for the background color
bgc = color (random (100, 255), random (100, 255), random (100, 255));
// we assign the font size of the letter
textSize (25);
// we justify the text vertically and horizontally centered
textAlign (CENTER, CENTER);
}
}
void draw ()
{
//black background
background (bgc);
for (int i = 0; i <ellipse; ++ i)
{
moveEllipse (i);
bounceEllipse (i);
drawEllipse (i);
}
}
// we increase the position of the object by its speed to achieve the illusion of movement
void moveEllipse (int id)
{
x [id] + = xspeed [id];
y [id] + = yspeed [id];
}
// logic for bouncing the ball
void bounceEllipse (int id)
{
if (x [id] <0 || x [id]> width && bounces> 9)
{
xspeed [id] * = -1;
bounces = bounces +1;
}
if (y [id] <0 || y [id]> height && bounces> 9)
{
yspeed [id] * = -1;
bounces = bounces +1;
{
fill (0);
text (bounces, x [id], y [id]);
}
}
}
// we draw the ball
void drawEllipse (int id)
{
float xoff = (float) width / bounces;
fill (255);
ellipse (x [id], y [id], 50, 50);
}