Rainbow ball : success but no real random traject

Hello (I’m back with level 3 again, I’m really, really sorry). I’ve read the other discussions on this topic, but I haven’t found a way to change the perception of my problem.

The exercise appears to be completed, but I’m not satisfied because the ball doesn’t bounce randomly in all directions… It maintains a sort of similar trajectory.

I’m not really having any difficulty changing the color. However, I don’t really understand how to use the new “random” function.

Below is my code. I think I need to add to my if statements that, as soon as the ball touches an edge, it’s the X AND Y positions that change, and not one or the other. But I can’t figure out how to code this to achieve this result. If I widen the “min” and “max” (which I tried), the ball appears and disappears in various places but doesn’t really bounce…

Could you please point me in the right direction?

// TODO: Create your variables
set x_and_y_position to 5 //starting value's position
set x to 3 //before increasement
set y to 4 //before increasement
set radius to 10
set hue to 99 //before increasement because starting value is 100
set saturation to 80
set luminosity to 50

//animation
set speed_x to 2 //the one that will change again and again
set speed_y to 1//the one that will change again and again

set speed_right to 2 //the variable to change the move_x
set speed_down to 1 //the variable to change the move_y
set speed_left to -2 //the variable to change the move_x
set speed_up to -1 //the variable to change the move_y

set hue_increasement to 1


//bouncing
set min_x_left to -10
set max_x_left to 10

set min_x_right to 110
set max_x_right to 90

set min_y_up to -10
set max_y_up to 10

set min_y_down to 110
set max_y_down to 90


  // TODO: Update variables
repeat 900 times do

  
//changing hue
  change hue to hue + hue_increasement
     
    if hue == 355 do
    change hue_increasement to -1
      
    else if hue == 100 do
    change hue_increasement to 1

    end

// changing X position

  change x to x + speed_x

    if x >= 100 do
    change speed_x to speed_left
    change x to random_number(min_x_right, max_x_right)

    else if x <= 0 do
    change speed_x to speed_right
    change x to random_number(min_x_left, max_x_left)
    end

  // changing y position

  change y to y + speed_y

    if y >= 100 do
    change speed_y to speed_up
    change y to random_number(min_y_down, max_y_down)
    else if y <= 0 do
    change speed_y to speed_down
    change y to random_number(min_y_up, max_y_up)
    end
  
  // TODO: Update variables

  // Draw the circle
  fill_color_hsl(hue, saturation, luminosity)
  circle(x, y, radius)
end

Can you explain, in English, what it would look like for the trajectory to change (beyond just bouncing)? What about the ball would change? Please do so without referencing any variables.

The ball must go back in the opposite direction from which it came (“backward” or “forward”) and go to the right or left randomly.

What do you mean “go to the right or left randomly”? Like what your code does?

Actually it seems to be more difficult than I thouht to explain this clearly (in my mind, I knows what it have to looks but I don’t really succeed to put some words on it, maybe that’s why I’m struggling with coding correctly :smiling_face_with_tear:)

The ball have to touch a “wall” (right, left, down or up) and go back (in the opposite direction) and touch an other, and again and again…
In coding terms, this means that the speed must change from positive to negative.
Inf we want to put a word on this, it"s the “speed”.

But what seems most complicated to me is how to broaden the scope of the direction to take, without causing the ball to appear in a different location (without losing its bounce effect).

The ball has both a current position and a velocity. The velocity has both a directly (left vs right) and a speed.

So you don’t want to randomly change the balls location. What else can you change?

I want to change its direction.

But I don’t understand what’s the difference between the directly and the speed in this change… And how could it be different in terms of coding, because we only have three functions :thinking:

Direction is right or left. Speed is how fast I’m that question. I can walk towards a door slow or fast. Speed is how far I travel every second.

Yes I understand that but how it’s represented with Jiki Script with just having X position / X position or radius for drawing the ball, that’s the reflexion point I don’t sucess to go through…
Because if we want the ball go randomly, we need to change X AND Y position, insn’t it ?

Changing all three factors (position, direction, speed) after the ball’s position over time.

Oh I feel so stupid… .The point is that I focused on the sentence : * To start with, in each iteration you should move it 2 to the right and 1 down."
So I was thinking that this (I considered this as the speed) couldn’t change :sob:

And so I didn’t see how to direct the ball any other way…

1 Like