Files

63 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2019-03-08 10:05:39 +03:00
# Bouncing Ball Challenge Tips
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
Use the following tips only when you get stuck. This document isn't in a particular order, please do not follow it like so.
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
## CALCULATING THE VELOCITY
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
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.
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
* On each loop step: Add velocities to ball's position. This will make the ball move.
2019-01-28 14:23:59 +03:00
* **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_
2019-03-08 10:05:39 +03:00
* **For more information on graphics and velocity:**
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
* [Youtube: Crash Course: 2D Graphics](https://www.youtube.com/watch?v=7Jr0SFMQ4Rs&t=529)
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
* [Youtube: Crash Course: Velocity](https://www.youtube.com/watch?v=ZM8ECpBuQYE)
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
## CREATING THE BOARD
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
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.
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
## CLEARING THE SCREEN
2019-03-08 10:05:39 +03:00
2019-03-14 21:16:15 +03:00
* Before the loop, clear the screen once by using my [screen package](https://github.com/inancgumus/screen), click on the link. You can find its [documentation here](https://godoc.org/github.com/inancgumus/screen).
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
* 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.
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
* You can find more information about the screen package and screen clearing in the [Retro Clock project section lectures](https://github.com/inancgumus/learngo/tree/master/15-project-retro-led-clock).
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
## DRAWING THE BOARD
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
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.
2019-01-28 14:23:59 +03:00
* Make a large enough rune slice named `buf` using the `make` function.
2019-03-08 10:05:39 +03:00
* **HINT:** `width * height` will give you a large enough buffer.
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
* **TIP:** You could also use `string` concatenation to draw into a `string` buffer but it would be inefficient.
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
* You will find more information about bytes and runes in the strings section.
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
```go
// TIP for converting the buffer
var buffer []rune
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
// For printing, you can convert a rune slice to a string like so:
str := string(buffer)
```
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
## SLOWING DOWN THE SPEED
2019-01-28 14:23:59 +03:00
2019-03-08 10:05:39 +03:00
Call the `time.Sleep` function to slow down the speed of the loop a little bit, so you can see the ball :)
2019-01-28 14:23:59 +03:00
2019-03-14 21:16:15 +03:00
```go
time.Sleep(time.Second / 20)
```