Skip to content

Commit

Permalink
feat(snake): Add color to game pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaamkiya committed Jan 12, 2025
1 parent 7e5c70b commit e8be803
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/app/snake/snake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type moveMsg struct{}
Expand Down Expand Up @@ -35,6 +36,7 @@ var (
type player struct {
body []vector
dir vector
style lipgloss.Style
}

func (p *player) move(m model, foodPos vector) {
Expand All @@ -60,6 +62,7 @@ func (p player) headChar() rune {

type model struct {
foodPos vector
foodStyle lipgloss.Style
player player
}

Expand Down Expand Up @@ -133,16 +136,16 @@ func (m model) View() string {
for i, b := range m.player.body {
if b.x == x && b.y == y {
if i == 0 {
s += string(m.player.headChar())
s += m.player.style.Render(string(m.player.headChar()))
} else {
s += "*"
s += m.player.style.Render("*")
}
drew = true
}
}
if !drew {
if x == m.foodPos.x && y == m.foodPos.y {
s += "0"
s += m.foodStyle.Render("0")
drew = true
}
}
Expand All @@ -164,9 +167,11 @@ func initialModel() tea.Model {
x: rand.IntN(20),
y: rand.IntN(20),
},
foodStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")),
player: player{
body: []vector{{6, 6}},
dir: dirRight,
style: lipgloss.NewStyle().Foreground(lipgloss.Color("32")),
},
}
}
Expand Down

0 comments on commit e8be803

Please sign in to comment.