Skip to content

Commit

Permalink
feat: implement lives, win and lose
Browse files Browse the repository at this point in the history
  • Loading branch information
ary82 committed Jul 10, 2024
1 parent cc758ed commit e107ae0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.board[prevX][prevY] = 3
m.board[newX][newY] = 2
}

if m.score == 300 {
return m, tea.Quit
}
return m, movePacman()

case updateGhostsPosition:
Expand Down Expand Up @@ -210,16 +214,34 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
ghost.isCurrentPositionPallet = false
}

if m.board[newX][newY] == 2 {
m.pacman.xPosition = 1
m.pacman.yPosition = 1
m.lives -= 1
}

if m.lives == 0 {
return m, gameOver
}

m.board[newX][newY] = ghost.viewCode
}
return m, moveGhosts()
case gameOverMsg:
return m, tea.Quit
}

return m, nil
}

type gameOverMsg int

func gameOver() tea.Msg {
return gameOverMsg(0)
}

func (m model) View() string {
s := fmt.Sprintf("\nSCORE: %v\n\n", m.score)
s := fmt.Sprintf("\nSCORE: %v\tLIVES: %v\n\n", m.score, m.lives)

for _, v := range m.board {
for _, num := range v {
Expand Down
8 changes: 4 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ func CalculatePosibbleNextTile(
possibleMoves := [][]int{}

if xPosition < 30 {
if newPos := board[xPosition+1][yPosition]; newPos == 1 || newPos == 3 {
if newPos := board[xPosition+1][yPosition]; newPos == 1 || newPos == 3 || newPos == 2 {
possibleMoves = append(possibleMoves, []int{xPosition + 1, yPosition})
}
}
if xPosition > 0 {
if newPos := board[xPosition-1][yPosition]; newPos == 1 || newPos == 3 {
if newPos := board[xPosition-1][yPosition]; newPos == 1 || newPos == 3 || newPos == 2 {
possibleMoves = append(possibleMoves, []int{xPosition - 1, yPosition})
}
}

if yPosition < 27 {
if newPos := board[xPosition][yPosition+1]; newPos == 1 || newPos == 3 {
if newPos := board[xPosition][yPosition+1]; newPos == 1 || newPos == 3 || newPos == 2 {
possibleMoves = append(possibleMoves, []int{xPosition, yPosition + 1})
}
}

if yPosition > 0 {
if newPos := board[xPosition][yPosition-1]; newPos == 1 || newPos == 3 {
if newPos := board[xPosition][yPosition-1]; newPos == 1 || newPos == 3 || newPos == 2 {
possibleMoves = append(possibleMoves, []int{xPosition, yPosition - 1})
}
}
Expand Down

0 comments on commit e107ae0

Please sign in to comment.