Rename files for later

This commit is contained in:
Sacha Ligthert 2023-11-06 23:43:45 +01:00
parent f2c3cfd891
commit 8e10c82b67

View File

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