Created
September 27, 2022 04:25
-
-
Save erap129/feb75fc0ba3961902e6fa75227485bda to your computer and use it in GitHub Desktop.
Snake Part 2 - game.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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