How can I simplify my code and how can i get the balls to spawn in different locations

Joined
Dec 9, 2019
Messages
6
Reaction score
0
The balls currently follow each other while slowly distancing away. Is there a way I can get them to spawn in random locations.

How can I also simplify my code?



My code:
float x=600;
float y=300;
float x1=600;
float y1=300;


// controls size
float size= 50;

//controls speed
float xSpeed =10;
float ySpeed= 10;
float x1Speed =10;
float y1Speed= 10;

//line variables
float l= 10;
float lSpeed=5;

void setup () {
size(1600, 900);
}


void draw () {

// repeatedly clears background
background(0);

//changes colour of the ball
fill(random(255),random(255),random(255));

// draws circle
ellipse(x, y, size , size);
ellipse(x1, y1, size , size);


//makes ball 1 bounce
x+= xSpeed;
if(x> width|| x< 0) {
xSpeed= xSpeed *-1;
}
y+= ySpeed;
if(y> height|| y< 0) {
ySpeed= ySpeed *-1;
}

//makes ball 2 bounce
x1+= xSpeed;
if(x1> width|| x1< 0) {
x1Speed= x1Speed *-1;
}
y1+= ySpeed;
if(y1> height|| y1< 0) {
y1Speed= y1Speed *-1;
}






//moving line

fill(random(0,255),random(0,255),random(0,255));
rect(0, l, 1600, 3);
l=l+ lSpeed;
if(l> height) {
l=0;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top