Ball should change direction after touching edge of the frame

Hi! I am completely new in the world of programming! I have started my programming lessons recently and I am stuck with my homework. What I basically have to do is a moving ball. The ball should move with velocity 20 pixels/second and change the direction after touching one of the edges (right or left). What script/code should I use?
Here is what I already created:

int currTime, prevTime;  
float elapsedTime;       
float xspeed = 20;        
float x0, x1;
void setup(){
  size(600, 200);
  currTime = prevTime = millis();
void draw(){
  currTime = millis();
  elapsedTime = (currTime - prevTime) / 1000.0;
  prevTime = currTime;  
  background(150, 150, 150);
  x0 += xspeed * elapsedTime; 
  fill (250, 250, 0);
  ellipse(x0, 64, 20, 20);
 if (x0 >= 600 ) { 
    xspeed= -1;
1 Like

Additionally if(x0<10) xspeed=1;

Still not working! The ball disappeared behind the frame and started again slinding from right to left (like the begininng).

I think this is flawed.

Which value does prevTime get? Is it different from current time?

1 Like