rename: bouncing ball

This commit is contained in:
Inanc Gumus
2019-03-18 16:22:06 +03:00
parent 11f71ff7b3
commit 4b8ffe7f63
11 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,176 @@
// 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/
//
// ---------------------------------------------------------
// EXERCISE: Adjust the width and height automatically
//
// In this exercise, your goal is simple:
//
// Instead of setting the width and height manually,
// you need to get the width and height of the terminal
// screen from your operating system.
//
// Don't worry, it is easier than it sounds. You just
// need to read a few documentation and install a
// Go package.
//
// 1. Go here: https://godoc.org/golang.org/x/crypto/ssh/terminal
//
// Download the package:
// go get -u golang.org/x/crypto/ssh/terminal
//
// 2. Find the function that gives you the width and height
// of the terminal.
//
// 3. Call that function from your program and get the
// width and height.
//
// 4. When an error occurs while retrieving the width
// and height, report it.
//
// 5. Set the width and height of the board.
//
// 6. After solving the above steps, update your program
// to use my screen package instead. It offers an
// easier way to get the width and height of a
// terminal.
//
// go get -u https://github.com/inancgumus/screen
//
//
// BONUS
//
// 1. When you set the width, you may see that the ball
// goes beyond the left and right borders. This happens
// because the ball emoji spans to multiple console
// columns (or cells). Ordinary characters have a
// single column.
//
// 1. Get the width of the ball emoji using a function
// from the following package:
//
// go get -u github.com/mattn/go-runewidth
//
// 2. Divide the width using the rune width of the
// ball emoji.
//
// 2. Your terminal may have borders, so reduce the
// height by taking into account the height of
// your terminal borders.
//
//
// EXPECTED OUTPUT
//
// When you run the program, the ball should start
// animating across the total width and height of your
// terminal screen dynamically.
//
// Currently you set width and height manually, so it
// wasn't matter whether your terminal was bigger or
// smaller, but now it will be!
//
//
// HINT
//
// Please take a look at this if you get stuck.
//
// You need to pass the Standard Out file handler
// to the function that returns you the dimensions.
//
// Check out my screen package to find out how I'm
// passing the Standard Out file handler.
//
// https://github.com/inancgumus/screen/blob/master/dimensions.go
//
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
const (
width = 50
height = 10
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
vx, vy = 1, 1 // velocities
cell rune // current cell (for caching)
)
// create the board
board := make([][]bool, width)
for row := range board {
board[row] = make([]bool, height)
}
// 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
}
// remove the previous ball
for y := range board[0] {
for x := range board {
board[x][y] = false
}
}
// put the new ball
board[px][py] = true
// 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)
}
}

View File

@ -0,0 +1,111 @@
// 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"
"os"
"time"
"github.com/mattn/go-runewidth"
"github.com/inancgumus/screen"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
const (
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
vx, vy = 1, 1 // velocities
cell rune // current cell (for caching)
)
// get the width and height
width, height, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
fmt.Println(err)
return
}
// you can get the width and height using the screen package easily:
// width, height := screen.Size()
// get the rune width of the ball emoji
ballWidth := runewidth.RuneWidth(cellBall)
// adjust the width and height
width /= ballWidth
height-- // there is a 1 pixel border in my terminal
// create the board
board := make([][]bool, width)
for row := range board {
board[row] = make([]bool, height)
}
// 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
}
// remove the previous ball
for y := range board[0] {
for x := range board {
board[x][y] = false
}
}
// put the new ball
board[px][py] = true
// 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)
}
}

View File

@ -0,0 +1,126 @@
// 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"
"github.com/mattn/go-runewidth"
)
// ---------------------------------------------------------
// EXERCISE: Previous positions
//
// Let's optimize the program once more. This time you're
// going to optimize the clearing off the previous positions.
//
// 1. Find the code below marked as "remove the previous ball"
//
// 2. Instead of clearing every position on the board to false,
// only set the previous position to false. So, don't use
// a loop, remove it.
//
// 3. Change the velocity of the ball like so:
//
// vx, vy = 5, 2
//
// 4. Run the program and solve the problem
//
//
// HINT
//
// Don't forget saving the previous position.
//
// ---------------------------------------------------------
func main() {
const (
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
vx, vy = 1, 1 // velocities
cell rune // current cell (for caching)
)
// you can get the width and height using the screen package easily:
width, height := screen.Size()
// get the rune width of the ball emoji
ballWidth := runewidth.RuneWidth(cellBall)
// adjust the width and height
width /= ballWidth
height-- // there is a 1 pixel border in my terminal
// create the board
board := make([][]bool, width)
for row := range board {
board[row] = make([]bool, height)
}
// 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
}
// remove the previous ball
for y := range board[0] {
for x := range board {
board[x][y] = false
}
}
// put the new ball
board[px][py] = true
// 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)
}
}

View File

@ -0,0 +1,101 @@
// 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"
"github.com/mattn/go-runewidth"
)
func main() {
const (
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
ppx, ppy int // previous ball position
vx, vy = 5, 2 // velocities
cell rune // current cell (for caching)
)
// you can get the width and height using the screen package easily:
width, height := screen.Size()
// get the rune width of the ball emoji
ballWidth := runewidth.RuneWidth(cellBall)
// adjust the width and height
width /= ballWidth
height-- // there is a 1 pixel border in my terminal
// create the board
board := make([][]bool, width)
for row := range board {
board[row] = make([]bool, height)
}
// 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
}
// check whether the ball goes beyond the borders
if px < width && py < height {
// remove the previous ball and put the new ball
board[px][py], board[ppx][ppy] = true, false
// save the previous positions
ppx, ppy = px, py
}
// 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)
}
}

View File

@ -0,0 +1,121 @@
// 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"
"github.com/mattn/go-runewidth"
)
// ---------------------------------------------------------
// EXERCISE: Single Dimensional
//
// In this exercise you will understand why I use
// a multi-dimensional board slice instead of a
// single-dimensional one.
//
// 1. Remove this:
// board := make([][]bool, width)
//
// 2. Use this:
// board := make([]bool, width*height)
//
// 3. Adjust the rest of the operations in the code to work
// with this single-dimensional slice.
//
// You'll see how hard it becomes to work with it.
//
// ---------------------------------------------------------
func main() {
const (
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
ppx, ppy int // previous ball position
vx, vy = 5, 2 // velocities
cell rune // current cell (for caching)
)
// you can get the width and height using the screen package easily:
width, height := screen.Size()
// get the rune width of the ball emoji
ballWidth := runewidth.RuneWidth(cellBall)
// adjust the width and height
width /= ballWidth
height-- // there is a 1 pixel border in my terminal
// create the board
board := make([][]bool, width)
for row := range board {
board[row] = make([]bool, height)
}
// 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
}
// check whether the ball goes beyond the borders
if px < width && py < height {
// remove the previous ball and put the new ball
board[px][py], board[ppx][ppy] = true, false
// save the previous positions
ppx, ppy = px, py
}
// 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)
}
}

View File

@ -0,0 +1,104 @@
// 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"
"github.com/mattn/go-runewidth"
)
func main() {
const (
cellEmpty = ' '
cellBall = '⚾'
maxFrames = 1200
speed = time.Second / 20
)
var (
px, py int // ball position
ppx, ppy int // previous ball position
vx, vy = 5, 2 // velocities
cell rune // current cell (for caching)
)
// you can get the width and height using the screen package easily:
width, height := screen.Size()
// get the rune width of the ball emoji
ballWidth := runewidth.RuneWidth(cellBall)
// adjust the width and height
width /= ballWidth
height-- // there is a 1 pixel border in my terminal
// create a single-dimensional board
board := make([]bool, width*height)
// 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
}
// check whether the ball goes beyond the borders
if px < width && py < height {
// calculate the new and the previous ball positions
pos := py*width + px
ppos := ppy*width + ppx
// remove the previous ball and put the new ball
board[pos], board[ppos] = true, false
// save the previous positions
ppx, ppy = px, py
}
// rewind the buffer (allow appending from the beginning)
buf = buf[:0]
// draw the board into the buffer
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
cell = cellEmpty
if board[y*width+x] {
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)
}
}

View File

@ -0,0 +1,13 @@
# Exercises
1. **[Adjust the width and height automatically](https://github.com/inancgumus/learngo/tree/master/18-bouncing-ball-project/exercises/01-width-and-height)**
In this exercise, your goal is getting the width and height of the terminal screen from your operating system (instead of setting the width and height manually).
2. **[Previous positions](https://github.com/inancgumus/learngo/tree/master/18-bouncing-ball-project/exercises/02-previous-positions)**
Let's optimize the program once more. This time you're going to optimize the clearing off the previous positions.
3. **[Use a single dimensional slice]()**
For the board slice, instead of using a multi-dimensional slice, let's use a single-dimensional slice. In this exercise, you'll understand and deeply internalize why I've used a multi-dimensional board slice.