2019-03-08 10:05:39 +03:00
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
2019-10-30 19:34:44 +03:00
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
2019-03-08 10:05:39 +03:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/inancgumus/screen"
|
|
|
|
)
|
|
|
|
|
2019-03-22 13:57:20 +03:00
|
|
|
// ---------------------------------------------------------
|
|
|
|
// EXERCISE: Find the Bug
|
|
|
|
//
|
|
|
|
// As I've annotated in the lectures, there is a bug
|
|
|
|
// in this code. Please find the bug and fix it.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HINT #1
|
|
|
|
//
|
|
|
|
// 💀 Read this only if you get stuck.
|
|
|
|
//
|
|
|
|
// Print the width*height and the capacity of the drawing buffer
|
|
|
|
// after a single drawing loop ends. You might be surprised.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HINT #2
|
|
|
|
//
|
|
|
|
// 💀 Read this only if you get stuck.
|
|
|
|
//
|
|
|
|
// The bug is in the drawing buffer. It doesn't include the
|
|
|
|
// newline and space characters when creating the buffer. So
|
|
|
|
// the buffer is not large enough to hold all the characters.
|
|
|
|
// So new backing arrays are getting allocated.
|
|
|
|
//
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
2019-03-08 10:05:39 +03:00
|
|
|
func main() {
|
|
|
|
const (
|
2019-03-22 13:57:20 +03:00
|
|
|
width = 50
|
|
|
|
height = 10
|
|
|
|
|
2019-03-08 10:05:39 +03:00
|
|
|
cellEmpty = ' '
|
|
|
|
cellBall = '⚾'
|
|
|
|
|
|
|
|
maxFrames = 1200
|
|
|
|
speed = time.Second / 20
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-14 20:36:32 +03:00
|
|
|
px, py int // ball position
|
|
|
|
vx, vy = 1, 1 // velocities
|
2019-03-08 10:05:39 +03:00
|
|
|
|
|
|
|
cell rune // current cell (for caching)
|
|
|
|
)
|
|
|
|
|
|
|
|
// create the board
|
|
|
|
board := make([][]bool, width)
|
2019-03-21 23:48:48 +03:00
|
|
|
for column := range board {
|
|
|
|
board[column] = make([]bool, height)
|
2019-03-08 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a drawing buffer
|
|
|
|
buf := make([]rune, 0, width*height)
|
|
|
|
|
|
|
|
// clear the screen once
|
|
|
|
screen.Clear()
|
|
|
|
|
|
|
|
for i := 0; i < maxFrames; i++ {
|
|
|
|
// calculate the next ball position
|
|
|
|
px += vx
|
|
|
|
py += vy
|
|
|
|
|
|
|
|
// when the ball hits a border reverse its direction
|
|
|
|
if px <= 0 || px >= width-1 {
|
|
|
|
vx *= -1
|
|
|
|
}
|
|
|
|
if py <= 0 || py >= height-1 {
|
|
|
|
vy *= -1
|
|
|
|
}
|
|
|
|
|
2019-03-14 20:36:32 +03:00
|
|
|
// remove the previous ball
|
|
|
|
for y := range board[0] {
|
|
|
|
for x := range board {
|
|
|
|
board[x][y] = false
|
|
|
|
}
|
2019-03-08 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 20:36:32 +03:00
|
|
|
// put the new ball
|
|
|
|
board[px][py] = true
|
|
|
|
|
2019-03-08 10:05:39 +03:00
|
|
|
// rewind the buffer (allow appending from the beginning)
|
|
|
|
buf = buf[:0]
|
|
|
|
|
|
|
|
// draw the board into the buffer
|
|
|
|
for y := range board[0] {
|
|
|
|
for x := range board {
|
|
|
|
cell = cellEmpty
|
|
|
|
if board[x][y] {
|
|
|
|
cell = cellBall
|
|
|
|
}
|
|
|
|
buf = append(buf, cell, ' ')
|
|
|
|
}
|
|
|
|
buf = append(buf, '\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the buffer
|
|
|
|
screen.MoveTopLeft()
|
|
|
|
fmt.Print(string(buf))
|
|
|
|
|
|
|
|
// slow down the animation
|
|
|
|
time.Sleep(speed)
|
|
|
|
}
|
|
|
|
}
|