// For more tutorials: https://blog.learngoprogramming.com // // Copyright © 2018 Inanc Gumus // Learn Go Programming Course // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // package main import ( "fmt" "time" "github.com/inancgumus/screen" ) const ( width = 25 height = 8 maxFrames = 1200 ) func main() { // ------------------------------------------------- // CREATE THE BOARD // ------------------------------------------------- board := make([][]bool, width) for row := range board { board[row] = make([]bool, height) } var ( X, Y int // ball positions pX, pY int // previous ball positions (for clearing) xVel, yVel = 1, 1 // velocities buf [width * height]rune // drawing buffer ) screen.Clear() for i := 0; i < maxFrames; i++ { // ------------------------------------------------- // CALCULATE THE NEXT BALL POSITION // ------------------------------------------------- X += xVel Y += yVel // when the ball hits the borders change its direction // by changing its velocity if X <= 0 || X >= width-1 { xVel *= -1 } if Y <= 0 || Y >= height-1 { yVel *= -1 } // ------------------------------------------------- // PUT THE BALL AND CLEAR THE BOARD // ------------------------------------------------- board[X][Y] = true board[pX][pY] = false pX, pY = X, Y // ------------------------------------------------- // DRAW THE BOARD // ------------------------------------------------- var ball rune // rewind the buffer bufs := buf[:] for y := range board[0] { for x := range board { ball = ' ' if board[x][y] { ball = '🎾' } bufs = append(bufs, ball, ' ') } bufs = append(bufs, '\n') } // clear the screen and draw the board screen.MoveTopLeft() fmt.Print(string(bufs)) // draw after a while: slows down the animation time.Sleep(time.Second / 20) } }