From e107ae0ca777f2b379d2eed9b7feabd34916c9a2 Mon Sep 17 00:00:00 2001 From: Aryan Goyal <137564277+ary82@users.noreply.github.com> Date: Wed, 10 Jul 2024 20:49:18 +0530 Subject: [PATCH] feat: implement lives, win and lose --- main.go | 24 +++++++++++++++++++++++- utils/utils.go | 8 ++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 03a4ccd..521ae97 100644 --- a/main.go +++ b/main.go @@ -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: @@ -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 { diff --git a/utils/utils.go b/utils/utils.go index c58b679..014d91e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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}) } }