5.1.2 - And stay on

We managed to animate the rectangle. Now we are going to control its movement. If you haven't done it you can download the code from here and open it to Thonny.

Change lines 42 and 43 using different integer numbers (0, 1, 2,...) and execute the code after each change you make. Also try using negative numbers (ex -5 or -1).

What happens if speed_x or speed_y variables are 0? What happens if they are negative?

speed = 0 means that rectangle is not moving
speed > 0 means that rectangle moves to one direction (right or down)
speed < 0 means that rectangle moves to the other direction (left or up)

How can we change the direction of the rectangle? Try this in CLI :

>>> number = 5
>>> print (number)
>>> number *= -1
>>> print (number)

What do you think is the purpose of third command?

How do you think that we can change the direction of the cube when it reaches the edge of the screen?


We will add this lines of code to the loop inside the block of gaming logic after setting the speed of the cube.

if rect_x >= 650 or rect_x <= 0: speed_x *= -1 if rect_y >= 450 or rect_y <= 0: speed_y *= -1


If you haven't managed to complete the code or you have an error you can download it from here.

Try to figure out why te numbers that we use for right bounce is 650 and not 700. The same for bottom bounce. Why are we using 450 and not 500. Change the number 650 to 670 kai 450 to 470 and run the program again.