Compare commits

..

3 Commits

2 changed files with 43 additions and 22 deletions

View File

@ -1,27 +1,24 @@
package main
import (
"github.com/gdamore/tcell/v2"
"fmt"
"log"
"strconv"
"github.com/gdamore/tcell/v2"
)
func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
row := y1
col := x1
for _, r := range []rune(text) {
s.SetContent(col, row, r, nil, style)
col++
if col >= x2 {
row++
col = x1
}
if row > y2 {
break
}
func updateScore(screen tcell.Screen, style tcell.Style, score int) {
var x, y int
_, y = screen.Size()
x = 5
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 10) + " ]") {
screen.SetContent(x, y-1, r, nil, style)
x++
}
}
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
if y2 < y1 {
y1, y2 = y2, y1
}
@ -54,13 +51,13 @@ func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string)
s.SetContent(x2, y2, '╝', nil, style)
}
drawText(s, x1+1, y1+1, x2-1, y2-1, style, text)
//drawText(s, x1+1, y1+1, x2-1, y2-1, style, text)
updateScore(s, style, 10)
}
func main() {
// info, err := tcell.LookupTerminfo(os.Getenv("TERM"))
defStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
//boxStyle := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorPurple)
defaultStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
var err error
@ -77,7 +74,7 @@ func main() {
screen.Clear()
drawBox(screen, 0, 0, x-1, y-1, defStyle, "There should be worms here!")
drawBox(screen, 0, 0, x-1, y-1, defaultStyle)
quit := func() {
// You have to catch panics in a defer, clean up, and
@ -97,13 +94,14 @@ func main() {
switch ev := ev.(type) {
case *tcell.EventResize:
x, y := screen.Size()
drawBox(screen, 0, 0, x-1, y-1, defStyle, "There should be worms here!")
drawBox(screen, 0, 0, x-1, y-1, defaultStyle)
fmt.Println("size=ok:", checkSize(screen))
screen.Sync()
case *tcell.EventKey:
//fmt.Println("Key: ",ev.Rune())
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
return
} else if ev.Rune() == 'q' {
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
return
} else if ev.Key() == tcell.KeyCtrlL {
screen.Sync()

23
render.go Normal file
View File

@ -0,0 +1,23 @@
package main
import "github.com/gdamore/tcell/v2"
/*
func renderScreen(&screen tcell.Screen, event tcell.Event, score int) {
if checkSize(*screen) == false {
return
}
}
*/
func checkSize(screen tcell.Screen) bool {
var retval bool = true
x, y := screen.Size()
if x < 80 {
retval = false
}
if y < 24 {
retval = false
}
return retval
}