Skip to content

Instantly share code, notes, and snippets.

@erap129
Created September 27, 2022 04:25
Show Gist options
  • Save erap129/feb75fc0ba3961902e6fa75227485bda to your computer and use it in GitHub Desktop.
Save erap129/feb75fc0ba3961902e6fa75227485bda to your computer and use it in GitHub Desktop.
Snake Part 2 - game.go
package main
import (
"time"
"github.com/gdamore/tcell"
)
type Game struct {
Screen tcell.Screen
snakeBody SnakeBody
}
func drawParts(s tcell.Screen, parts []SnakePart, style tcell.Style) {
for _, part := range parts {
s.SetContent(part.X, part.Y, ' ', nil, style)
}
}
func (g *Game) Run() {
defStyle := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
g.Screen.SetStyle(defStyle)
width, height := g.Screen.Size()
snakeStyle := tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorWhite)
for {
g.Screen.Clear()
g.snakeBody.Update(width, height)
drawParts(g.Screen, g.snakeBody.Parts, snakeStyle)
time.Sleep(40 * time.Millisecond)
g.Screen.Show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment