WEBVTT 0 00:01.630 --> 00:02.330 Welcome! 1 00:02.560 --> 00:07.960 In this section, I'm going to create a program that animates a ball on a 2D surface. 2 00:08.020 --> 00:15.680 First of all, let me show you the final program. As you can see, the ball bounces inside an invisible board. 3 00:16.520 --> 00:20.880 The invisible board is actually a two dimensional slice. 4 00:20.990 --> 00:23.650 Your goal is creating this program. 5 00:23.660 --> 00:26.690 Let me show you how it works in detail. 6 00:26.690 --> 00:32.840 I'm using a multidimensional bool slice to keep track of the ball's position on the board. 7 00:32.870 --> 00:39.980 You can use it to render a ball like so. There is no ball on the board yet because all the elements of 8 00:39.980 --> 00:42.450 the inner slices are false now. 9 00:42.560 --> 00:47.310 So when an element is false you only need to print an empty cell. 10 00:47.450 --> 00:49.440 Let's bring the ball. To do that, 11 00:49.460 --> 00:56.330 I'm going to set the first element of the first inner slice to true like so. When I do that, the program 12 00:56.600 --> 00:59.420 prints the ball at the top left corner of the ball. 13 01:00.320 --> 01:05.870 So when an element becomes true, you need to print the ball on that position. 14 01:05.990 --> 01:10.260 By the way, the first index corresponds to the X position. 15 01:10.460 --> 01:16.690 The second index corresponds to the Y position. After the board and ball is ready 16 01:16.730 --> 01:23.950 the program starts animating the ball in a loop continuously like so. By the way, 17 01:23.980 --> 01:29.370 whenever the program sets an element to true, it sets the other elements to false. 18 01:29.400 --> 01:33.390 By doing so, it keeps rendering a single ball in a single cell. 19 01:34.020 --> 01:37.390 Otherwise it would render multiple balls. 20 01:37.410 --> 01:39.770 Finally the ball hits the bottom 21 01:39.810 --> 01:40.890 edge of the board. 22 01:40.890 --> 01:45.440 So now the program needs to reverse the vertical direction of the ball 23 01:45.510 --> 01:50.880 like so. As you can see, now the program reverses the vertical direction of the ball. 24 01:51.120 --> 01:54.890 That's why the Y position changes from 3 to 2. 25 01:54.960 --> 02:00.440 The x position also keeps increasing because the ball didn't hit the horizontal edges yet. 26 02:00.450 --> 02:05.900 Now the ball hits a horizontal edge, to the right border. 27 02:05.910 --> 02:14.300 So this time it needs the bounce back horizontally. As you can see, now the program reverses the horizontal 28 02:14.300 --> 02:15.800 direction of the ball. 29 02:15.800 --> 02:19.340 That's why the X position changes from 5 to 4. 30 02:19.370 --> 02:26.970 The Y position also keeps decreasing because the ball didn't hit the vertical edges yet. But now the ball 31 02:26.970 --> 02:33.150 hits the top edge, so it needs to bounce back vertically, so the program reverses the vertical direction 32 02:33.150 --> 02:33.740 of the ball. 33 02:33.750 --> 02:37.640 Now the Y position starts to decrease and it goes like this. 34 02:37.650 --> 02:43.050 So here is the overview of my algorithm. Your algorithm can be different. 35 02:43.050 --> 02:47.120 Of course, you are free to create the same program using different logic. 36 02:48.010 --> 02:53.320 OK first I create a two-dimensional bool slice for the board. 37 02:53.370 --> 02:56.940 After that I clear the screen using the screen package. 38 02:56.940 --> 03:02.410 If you don't know how to use it please watch the previous retro led clock project again. 39 03:02.460 --> 03:06.090 After that I create a loop for the animation. In the loop, 40 03:06.120 --> 03:09.110 first, I calculate the next position of the ball. 41 03:09.120 --> 03:14.940 After that, I create a buffer, and I draw the characters that I'm going to print into that buffer. 42 03:14.940 --> 03:21.690 It is because I cannot print a bool slice but I can print a rune slice by converting it to a string. 43 03:21.690 --> 03:25.030 Now I need to print the board and the ball. To do that, 44 03:25.110 --> 03:29.000 first, I move the cursor to the top left corner of the screen. 45 03:29.040 --> 03:32.250 I do so by using the screen package again. 46 03:32.250 --> 03:38.970 After that, I actually print the buffer by converting the rune buffer to a string value like so. 47 03:39.000 --> 03:43.670 And lastly, I wait before drawing the next step of the animation. 48 03:43.680 --> 03:46.920 This loop continues as long as I want. 49 03:47.040 --> 03:52.370 By the way, can find more information about this challenge in the next lecture. By the way, 50 03:52.380 --> 03:56.890 if you have a Twitter account, don't forget to tweet about your progress. 51 03:56.970 --> 04:02.760 You can do so after you complete your solution or when you start working on it. Just send it to my Twitter 52 04:02.790 --> 04:04.700 using my account name. 53 04:04.740 --> 04:06.770 You can even upload it to your own github. 54 04:07.110 --> 04:07.570 Okay. 55 04:07.590 --> 04:08.910 See you in the next lecture. 56 04:08.910 --> 04:09.460 Good luck. 57 04:09.510 --> 04:09.720 Bye!