A Go version of the https://lcthw.dev/learn-code-the-hard-way/curseyou-python-rogue that makes a tiny Rogue in Go.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
1 week ago
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
|
||
|
"github.com/gdamore/tcell/v2"
|
||
|
"github.com/gdamore/tcell/v2/encoding"
|
||
|
|
||
|
"github.com/mattn/go-runewidth"
|
||
|
)
|
||
|
|
||
|
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
|
||
|
for _, c := range str {
|
||
|
var comb []rune
|
||
|
w := runewidth.RuneWidth(c)
|
||
|
if w == 0 {
|
||
|
comb = []rune{c}
|
||
|
c = ' '
|
||
|
w = 1
|
||
|
}
|
||
|
s.SetContent(x, y, c, comb, style)
|
||
|
x += w
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func displayHelloWorld(s tcell.Screen) {
|
||
|
w, h := s.Size()
|
||
|
s.Clear()
|
||
|
style := tcell.StyleDefault.Foreground(tcell.ColorCadetBlue.TrueColor()).Background(tcell.ColorWhite)
|
||
|
emitStr(s, w/2-7, h/2, style, "Hello, World!")
|
||
|
emitStr(s, w/2-9, h/2+1, tcell.StyleDefault, "Press ESC to exit.")
|
||
|
s.Show()
|
||
|
}
|
||
|
|
||
|
// This program just prints "Hello, World!". Press ESC to exit.
|
||
|
func main() {
|
||
|
encoding.Register()
|
||
|
|
||
|
s, e := tcell.NewScreen()
|
||
|
if e != nil {
|
||
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
if e := s.Init(); e != nil {
|
||
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
defStyle := tcell.StyleDefault.
|
||
|
Background(tcell.ColorBlack).
|
||
|
Foreground(tcell.ColorWhite)
|
||
|
s.SetStyle(defStyle)
|
||
|
|
||
|
displayHelloWorld(s)
|
||
|
|
||
|
for {
|
||
|
switch ev := s.PollEvent().(type) {
|
||
|
case *tcell.EventResize:
|
||
|
s.Sync()
|
||
|
displayHelloWorld(s)
|
||
|
case *tcell.EventKey:
|
||
|
if ev.Key() == tcell.KeyEscape {
|
||
|
s.Fini()
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|