move: bouncing ball
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const (
|
||||
width = 50
|
||||
height = 10
|
||||
|
||||
cellEmpty = ' '
|
||||
cellBall = '⚾'
|
||||
)
|
||||
|
||||
var cell rune // current cell (for caching)
|
||||
|
||||
// create the board
|
||||
board := make([][]bool, width)
|
||||
for row := range board {
|
||||
board[row] = make([]bool, height)
|
||||
}
|
||||
|
||||
// draw a smiley
|
||||
board[12][2] = true
|
||||
board[16][2] = true
|
||||
board[14][4] = true
|
||||
board[10][6] = true
|
||||
board[18][6] = true
|
||||
board[12][7] = true
|
||||
board[14][7] = true
|
||||
board[16][7] = true
|
||||
|
||||
// print the board directly to the console
|
||||
for y := range board[0] {
|
||||
for x := range board {
|
||||
cell = cellEmpty
|
||||
if board[x][y] {
|
||||
cell = cellBall
|
||||
}
|
||||
fmt.Print(string(cell), " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const (
|
||||
width = 50
|
||||
height = 10
|
||||
|
||||
cellEmpty = ' '
|
||||
cellBall = '⚾'
|
||||
)
|
||||
|
||||
var 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)
|
||||
|
||||
// draw a smiley
|
||||
board[12][2] = true
|
||||
board[16][2] = true
|
||||
board[14][4] = true
|
||||
board[10][6] = true
|
||||
board[18][6] = true
|
||||
board[12][7] = true
|
||||
board[14][7] = true
|
||||
board[16][7] = true
|
||||
|
||||
// use the loop for measuring the performance difference
|
||||
for i := 0; i < 1000; i++ {
|
||||
// rewind the buffer so that the program reuses it
|
||||
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
|
||||
}
|
||||
// fmt.Print(string(cell), " ")
|
||||
buf = append(buf, cell, ' ')
|
||||
}
|
||||
// fmt.Println()
|
||||
buf = append(buf, '\n')
|
||||
}
|
||||
|
||||
// print the buffer
|
||||
fmt.Print(string(buf))
|
||||
}
|
||||
}
|
@@ -1,93 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@@ -1,100 +0,0 @@
|
||||
// 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/inancgumus/screen"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const (
|
||||
cellEmpty = ' '
|
||||
cellBall = '⚾'
|
||||
|
||||
maxFrames = 1200
|
||||
speed = time.Second / 20
|
||||
)
|
||||
|
||||
// get the width and height of the terminal dynamically ***
|
||||
width, height, err := terminal.GetSize(int(os.Stdout.Fd()))
|
||||
if err != nil {
|
||||
fmt.Println("cannot get width and height", err)
|
||||
return
|
||||
}
|
||||
width /= 2 // our emoji is 2 chars wide
|
||||
|
||||
var (
|
||||
px, py int // ball position
|
||||
ppx, ppy int // previous 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
# Bouncing Ball Challenge Tips
|
||||
|
||||
Use the following tips only when you get stuck. This document isn't in a particular order, please do not follow it like so.
|
||||
|
||||
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||
|
||||
## Ball position and velocity
|
||||
|
||||
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:**
|
||||
* Youtube: Crash Course: 2D Graphics
|
||||
-> https://www.youtube.com/watch?v=7Jr0SFMQ4Rs&t=529
|
||||
|
||||
* Youtube: Crash Course: Velocity
|
||||
-> https://www.youtube.com/watch?v=ZM8ECpBuQYE
|
||||
|
||||
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||
|
||||
## CREATE THE BOARD
|
||||
|
||||
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.
|
||||
|
||||
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||
|
||||
## CLEAR THE SCREEN
|
||||
|
||||
* Before the loop, clear the screen once by using my screen package.
|
||||
|
||||
* 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.
|
||||
|
||||
* It's [here](https://github.com/inancgumus/screen).
|
||||
* Its documentation is [here](https://godoc.org/github.com/inancgumus/screen).
|
||||
* You can find more information about it in the Retro Clock project section.
|
||||
|
||||
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||
|
||||
## DRAW THE BOARD
|
||||
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` buffer because `rune` can store an emoji character, and I use a slice of runes because I want to draw empty cells and a ball in the end.
|
||||
|
||||
* Make a large enough rune slice named `buf` using the `make` function.
|
||||
|
||||
* **HINT:** `width * height` will give you a large enough buffer.
|
||||
|
||||
* **TIP:** You could also use `string` concatenation to draw into a `string` buffer but it would be inefficient.
|
||||
|
||||
* You will find more information about bytes and runes in the strings section.
|
||||
|
||||
```go
|
||||
// TIP for converting the buffer
|
||||
var buffer []rune
|
||||
|
||||
// For printing, you can convert a rune slice to a string like so:
|
||||
str := string(buffer)
|
||||
```
|
||||
|
||||
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||
|
||||
## SLOW DOWN THE SPEED
|
||||
|
||||
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)`
|
Reference in New Issue
Block a user