Use the following tips only when you get stuck. This document isn't in a particular order, please do not follow it like so.
You can use velocity to change the ball's speed and position. In my example, the speed is constant, so I always use unit value: 1.
-
On each loop step: Add velocities to ball's position. This will make the ball move.
-
Velocity means: Speed and Direction
- X velocity = 1 -> ball moves right
- X velocity = -1 -> ball moves left
- Y velocity = 1 -> ball moves down
- Y velocity = -1 -> ball moves up
-
For more information on graphics and velocity:
I use [][]bool
for the board but you can use anything you like. For example, you can directly use [][]rune
or []rune
. Experiment with them and decide which one is the best for you.
-
Before the loop, clear the screen once by using my screen package, click on the link. You can find its documentation here.
-
After each loop step, move the cursor to the top-left position by using the screen package. So that you can draw the animation frame all over again in the same position.
-
You can find more information about the screen package and screen clearing in the Retro Clock project section lectures.
Instead of drawing the board and the ball to the screen everytime, you will fill a buffer, and when you complete, you can draw the board and the ball once by printing the buffer. I use a []rune
slice as a buffer because rune
can store an emoji character.
-
Make a large enough rune slice named
buf
using themake
function.-
HINT:
width * height
will give you a large enough buffer. -
TIP: You could also use
string
concatenation to draw into astring
buffer but it would be inefficient. -
You will find more information about bytes and runes in the strings section.
-
// TIP for converting the buffer
var buffer []rune
// For printing, you can convert a rune slice to a string like so:
str := string(buffer)
Call the time.Sleep
function to slow down the speed of the loop a little bit, so you can see the ball :)
time.Sleep(time.Second / 20)