Welcome!
In this section, I'm going to create a program that animates a ball on a 2D surface.
First of all, let me show you the final program. As you can see, the ball bounces inside an invisible board.
The invisible board is actually a two dimensional slice.
Your goal is creating this program.
Let me show you how it works in detail.
I'm using a multidimensional bool slice to keep track of the ball's position on the board.
You can use it to render a ball like so. There is no ball on the board yet because all the elements of
the inner slices are false now.
So when an element is false you only need to print an empty cell.
Let's bring the ball. To do that,
I'm going to set the first element of the first inner slice to true like so. When I do that, the program
prints the ball at the top left corner of the ball.
So when an element becomes true, you need to print the ball on that position.
By the way, the first index corresponds to the X position.
The second index corresponds to the Y position. After the board and ball is ready
the program starts animating the ball in a loop continuously like so. By the way,
whenever the program sets an element to true, it sets the other elements to false.
By doing so, it keeps rendering a single ball in a single cell.
Otherwise it would render multiple balls.
Finally the ball hits the bottom
edge of the board.
So now the program needs to reverse the vertical direction of the ball
like so. As you can see, now the program reverses the vertical direction of the ball.
That's why the Y position changes from 3 to 2.
The x position also keeps increasing because the ball didn't hit the horizontal edges yet.
Now the ball hits a horizontal edge, to the right border.
So this time it needs the bounce back horizontally. As you can see, now the program reverses the horizontal
direction of the ball.
That's why the X position changes from 5 to 4.
The Y position also keeps decreasing because the ball didn't hit the vertical edges yet. But now the ball
hits the top edge, so it needs to bounce back vertically, so the program reverses the vertical direction
of the ball.
Now the Y position starts to decrease and it goes like this.
So here is the overview of my algorithm. Your algorithm can be different.
Of course, you are free to create the same program using different logic.
OK first I create a two-dimensional bool slice for the board.
After that I clear the screen using the screen package.
If you don't know how to use it please watch the previous retro led clock project again.
After that I create a loop for the animation. In the loop,
first, I calculate the next position of the ball.
After that, I create a buffer, and I draw the characters that I'm going to print into that buffer.
It is because I cannot print a bool slice but I can print a rune slice by converting it to a string.
Now I need to print the board and the ball. To do that,
first, I move the cursor to the top left corner of the screen.
I do so by using the screen package again.
After that, I actually print the buffer by converting the rune buffer to a string value like so.
And lastly, I wait before drawing the next step of the animation.
This loop continues as long as I want.
By the way, can find more information about this challenge in the next lecture. By the way,
if you have a Twitter account, don't forget to tweet about your progress.
You can do so after you complete your solution or when you start working on it. Just send it to my Twitter
using my account name.
You can even upload it to your own github.
Okay.
See you in the next lecture.
Good luck.
Bye!