Something went wrong. Try again.
a collection of games playable over ssh!
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849package snake
import ( "time"
common "github.com/CelestialCrafter/games/common" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea")
type tickMsg time.Time
func tick() tea.Cmd { return tea.Tick(tickRate, func(t time.Time) tea.Msg { return tickMsg(t) })}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tickMsg: if m.Finished { return m, nil }
m.process(m.lastTick)
m.lastTick = time.Now() return m, tick() case tea.KeyMsg: switch { case key.Matches(msg, m.keys.Quit): return m, func() tea.Msg { return common.BackMsg{} } case key.Matches(msg, m.keys.Help): m.help.ShowAll = !m.help.ShowAll case key.Matches(msg, m.keys.Up, m.keys.Down, m.keys.Left, m.keys.Right): m.inputBuffer = append(m.inputBuffer, msg) } case tea.WindowSizeMsg: m.help.Width = msg.Width m.width = msg.Width m.height = msg.Height }
return m, nil}