restructure: arrays and slices
This commit is contained in:
56
15-project-retro-led-clock/03-animating-the-clock/README.md
Normal file
56
15-project-retro-led-clock/03-animating-the-clock/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
# GOAL 3: Animate the Clock
|
||||
|
||||
## Notes
|
||||
|
||||
* Note main.go file contains the solution of the previous step.
|
||||
* "solution" folder contains the solution for this step.
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Create an infinite loop to update the clock
|
||||
|
||||
2. Update the clock every second
|
||||
|
||||
[time.Sleep(time.Second)](https://golang.org/pkg/time/#Sleep) will stop the world for 1 second
|
||||
|
||||
3. Clear the screen before the infinite loop
|
||||
|
||||
1. Get my library for clearing the screen:
|
||||
|
||||
`go get -u github.com/inancgumus/screen`
|
||||
|
||||
2. Then, import it and call it in your code like this:
|
||||
|
||||
`screen.Clear()`
|
||||
|
||||
3. If you're using Go Playground instead, do this:
|
||||
|
||||
`fmt.Println("\f")`
|
||||
|
||||
4. Move the cursor to the top-left corner of the screen, before each step
|
||||
of the infinite loop
|
||||
|
||||
* Call this in your code like this:
|
||||
|
||||
`screen.MoveTopLeft()`
|
||||
|
||||
* If you're using Go Playground instead, do this again:
|
||||
|
||||
`fmt.Println("\f")`
|
||||
|
||||
---
|
||||
|
||||
## SIDE NOTE FOR THE CURIOUS
|
||||
|
||||
If you're curious about how my screen clearing package works, read on.
|
||||
|
||||
**On bash**, it uses special commands, if you open the code, you can see that.
|
||||
|
||||
* `\033` is a special control code:
|
||||
* `[2J` clears the screen and the cursor
|
||||
* `[H` moves the cursor to 0, 0 screen position
|
||||
* [See for more information](https://bluesock.org/~willkg/dev/ansi.html).
|
||||
|
||||
**On Windows**, I'm directly calling the Windows API functions. This is way advanced at this stage of the course, however, I'll probably explain it afterward.
|
||||
|
||||
So, my package automatically adjusts itself depending on where it is compiled. On Windows, it uses the special Windows API calls; On other operating systems, it uses the bash special commands that I've explained above.
|
131
15-project-retro-led-clock/03-animating-the-clock/main.go
Normal file
131
15-project-retro-led-clock/03-animating-the-clock/main.go
Normal file
@ -0,0 +1,131 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type placeholder [5]string
|
||||
|
||||
zero := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
one := placeholder{
|
||||
"██ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
two := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
three := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
four := placeholder{
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
five := placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
six := placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
seven := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
eight := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
nine := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
colon := placeholder{
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
}
|
||||
|
||||
digits := [...]placeholder{
|
||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||
|
||||
fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec)
|
||||
|
||||
// [8][5]string
|
||||
clock := [...]placeholder{
|
||||
// extract the digits: 17 becomes, 1 and 7 respectively
|
||||
digits[hour/10], digits[hour%10],
|
||||
colon,
|
||||
digits[min/10], digits[min%10],
|
||||
colon,
|
||||
digits[sec/10], digits[sec%10],
|
||||
}
|
||||
|
||||
for line := range clock[0] {
|
||||
for digit := range clock {
|
||||
fmt.Print(clock[digit][line], " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
// 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() {
|
||||
type placeholder [5]string
|
||||
|
||||
zero := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
one := placeholder{
|
||||
"██ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
two := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
three := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
four := placeholder{
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
five := placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
six := placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
seven := placeholder{
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
eight := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
nine := placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
colon := placeholder{
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
}
|
||||
|
||||
digits := [...]placeholder{
|
||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||
}
|
||||
|
||||
// For Go Playground, do not use this.
|
||||
screen.Clear()
|
||||
|
||||
// Go Playground will not run an infinite loop.
|
||||
// So, instead, you may loop for 1000 times:
|
||||
// for i := 0; i < 1000; i++ {
|
||||
for {
|
||||
// For Go Playground, use this instead:
|
||||
// fmt.Print("\f")
|
||||
screen.MoveTopLeft()
|
||||
|
||||
now := time.Now()
|
||||
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||
|
||||
clock := [...]placeholder{
|
||||
digits[hour/10], digits[hour%10],
|
||||
colon,
|
||||
digits[min/10], digits[min%10],
|
||||
colon,
|
||||
digits[sec/10], digits[sec%10],
|
||||
}
|
||||
|
||||
for line := range clock[0] {
|
||||
for digit := range clock {
|
||||
fmt.Print(clock[digit][line], " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// pause for 1 second
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user