diff --git a/food.go b/apple.go similarity index 100% rename from food.go rename to apple.go diff --git a/game.go b/game.go index 7d0ab53..bcbec6a 100644 --- a/game.go +++ b/game.go @@ -1,63 +1,14 @@ package main import ( - "fmt" "log" - "strconv" + "time" "github.com/gdamore/tcell/v2" ) -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) { - if y2 < y1 { - y1, y2 = y2, y1 - } - if x2 < x1 { - x1, x2 = x2, x1 - } - - // Fill background - for row := y1; row <= y2; row++ { - for col := x1; col <= x2; col++ { - s.SetContent(col, row, ' ', nil, style) - } - } - - // Draw borders - for col := x1; col <= x2; col++ { - s.SetContent(col, y1, '═', nil, style) - s.SetContent(col, y2, '═', nil, style) - } - for row := y1 + 1; row < y2; row++ { - s.SetContent(x1, row, '║', nil, style) - s.SetContent(x2, row, '║', nil, style) - } - - // Only draw corners if necessary - if y1 != y2 && x1 != x2 { - s.SetContent(x1, y1, '╔', nil, style) - s.SetContent(x2, y1, '╗', nil, style) - s.SetContent(x1, y2, '╚', nil, style) - s.SetContent(x2, y2, '╝', nil, style) - } - - //drawText(s, x1+1, y1+1, x2-1, y2-1, style, text) - updateScore(s, style, 10) -} - -func main() { - - defaultStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset) +func initilize() (tcell.Screen, tcell.Style, error) { + style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset) var err error @@ -69,33 +20,48 @@ func main() { if err != nil { log.Fatalln("Error initializing screen: ", err) } + return screen, style, err +} + +func quit(screen tcell.Screen) { + maybePanic := recover() + screen.Fini() + if maybePanic != nil { + panic(maybePanic) + } +} + +func game_director(screen tcell.Screen, style tcell.Style) { + + //var score int + //var scoreCounter int = 1 + //var field [80][24]int + // fill up the field: + // 0: empty + // 1: snake + // 2: apple + // 3: border x, y := screen.Size() + drawBox(screen, 0, 0, x-1, y-1, style) - screen.Clear() - - drawBox(screen, 0, 0, x-1, y-1, defaultStyle) - - quit := func() { - // You have to catch panics in a defer, clean up, and - // re-raise them - otherwise your application can - // die without leaving any diagnostic trace. - maybePanic := recover() - screen.Fini() - if maybePanic != nil { - panic(maybePanic) - } - } - defer quit() + var snakex int = 3 + var snakey int = 12 for { - screen.Show() + + // Clean the screen + screen.Clear() + + // Poll for an event ev := screen.PollEvent() + + // Fetch the type, and check if any other actions are required. switch ev := ev.(type) { case *tcell.EventResize: x, y := screen.Size() - drawBox(screen, 0, 0, x-1, y-1, defaultStyle) - fmt.Println("size=ok:", checkSize(screen)) + drawBox(screen, 0, 0, x-1, y-1, style) + updateScore(screen, style, snakex) screen.Sync() case *tcell.EventKey: //fmt.Println("Key: ",ev.Rune()) @@ -108,6 +74,23 @@ func main() { } else if ev.Rune() == 'C' || ev.Rune() == 'c' { screen.Clear() } + default: + time.Sleep(500 * time.Millisecond) + } + + drawBox(screen, 0, 0, x-1, y-1, style) + updateScore(screen, style, snakex) + updateSnakePos(&snakex, &snakey) + drawSnake(screen, style, snakex, snakey) + + // Draw the screen + screen.Show() + + if snakex > 75 { + return + } + } + } diff --git a/main.go b/main.go new file mode 100644 index 0000000..c70f469 --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "log" +) + +func main() { + + screen, style, err := initilize() + if err != nil { + log.Fatalln("Initlization error: ", err) + } + + game_director(screen, style) + + quit(screen) + +} diff --git a/render.go b/render.go index c1453ca..0d88f03 100644 --- a/render.go +++ b/render.go @@ -1,6 +1,10 @@ package main -import "github.com/gdamore/tcell/v2" +import ( + "strconv" + + "github.com/gdamore/tcell/v2" +) /* func renderScreen(&screen tcell.Screen, event tcell.Event, score int) { @@ -21,3 +25,50 @@ func checkSize(screen tcell.Screen) bool { } return retval } + +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) { + if y2 < y1 { + y1, y2 = y2, y1 + } + if x2 < x1 { + x1, x2 = x2, x1 + } + + // Fill background + for row := y1; row <= y2; row++ { + for col := x1; col <= x2; col++ { + s.SetContent(col, row, ' ', nil, style) + } + } + + // Draw borders + for col := x1; col <= x2; col++ { + s.SetContent(col, y1, '═', nil, style) + s.SetContent(col, y2, '═', nil, style) + } + for row := y1 + 1; row < y2; row++ { + s.SetContent(x1, row, '║', nil, style) + s.SetContent(x2, row, '║', nil, style) + } + + // Only draw corners if necessary + if y1 != y2 && x1 != x2 { + s.SetContent(x1, y1, '╔', nil, style) + s.SetContent(x2, y1, '╗', nil, style) + s.SetContent(x1, y2, '╚', nil, style) + s.SetContent(x2, y2, '╝', nil, style) + } + + //drawText(s, x1+1, y1+1, x2-1, y2-1, style, text) + updateScore(s, style, 10) +} diff --git a/snake.go b/snake.go new file mode 100644 index 0000000..79dd005 --- /dev/null +++ b/snake.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/gdamore/tcell/v2" +) + +func updateSnakePos(x *int, y *int) { + *x = *x + 1 +} + +func drawSnake(screen tcell.Screen, style tcell.Style, x int, y int) { + screen.SetContent(x, y, '🐍', nil, style) +}