Skip to content

Commit 1e14b51

Browse files
author
Bastian Isensee
committed
On game over show score and allow restart
1 parent 6c3c21b commit 1e14b51

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

Diff for: main.go

+50-26
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type model struct {
3131
playerRow int
3232
playerCol int
3333

34+
score int
3435
gameOver bool
3536
}
3637

@@ -84,7 +85,8 @@ func (m *model) movePlayer(row, col int) {
8485

8586
if m.table[row][col] == item {
8687
// We collected an item. A new item and enemy needs to be
87-
// spawned.
88+
// spawned. Increase the score.
89+
m.score++
8890
m.spawnItem()
8991
m.spawnEnemy()
9092
}
@@ -131,39 +133,45 @@ func (m *model) playerRight() {
131133
m.movePlayer(m.playerRow, m.playerCol+1)
132134
}
133135

134-
// newModel is responsible for creating an initial model that is ready to use.
135-
func newModel() *model {
136+
// init is responsible for initializing or resetting a model that is ready
137+
// to use for a new game.
138+
func (m *model) init() {
139+
// Clear and reset all fields as init() is also used for restarting
140+
// an existing game. Therefore our model needs to be fresh.
136141
// By default, all entries in our table have value zero as type rune
137-
// is based on int and represents symbols. We only set non-empty fields
138-
// explicitly.
139-
model := &model{}
142+
// is based on int and represents symbols. Initially, all cells are
143+
// empty.
144+
var table [tableHeight][tableWidth]rune
145+
m.table = table
146+
m.playerRow = 0
147+
m.playerCol = 0
148+
m.score = 0
149+
m.gameOver = false
140150

141151
// Set the four corners.
142-
model.table[0][0] = corner
143-
model.table[0][tableWidth-1] = corner
144-
model.table[tableHeight-1][0] = corner
145-
model.table[tableHeight-1][tableWidth-1] = corner
152+
m.table[0][0] = corner
153+
m.table[0][tableWidth-1] = corner
154+
m.table[tableHeight-1][0] = corner
155+
m.table[tableHeight-1][tableWidth-1] = corner
146156

147157
// Draw horizontal borders at the top and bottom.
148158
for col := 1; col < tableWidth-1; col++ {
149-
model.table[0][col] = lineHorizontal
150-
model.table[tableHeight-1][col] = lineHorizontal
159+
m.table[0][col] = lineHorizontal
160+
m.table[tableHeight-1][col] = lineHorizontal
151161
}
152162

153163
// Draw vertical borders on the left and right side.
154164
for row := 1; row < tableHeight-1; row++ {
155-
model.table[row][0] = lineVertical
156-
model.table[row][tableWidth-1] = lineVertical
165+
m.table[row][0] = lineVertical
166+
m.table[row][tableWidth-1] = lineVertical
157167
}
158168

159169
// Spawn our player near the top left corner.
160-
model.playerRow = 1
161-
model.playerCol = 1
162-
model.table[model.playerRow][model.playerCol] = player
163-
164-
model.spawnItem()
170+
m.playerRow = 1
171+
m.playerCol = 1
172+
m.table[m.playerRow][m.playerCol] = player
165173

166-
return model
174+
m.spawnItem()
167175
}
168176

169177
// Init can be used to setup initial command to perform.
@@ -180,9 +188,17 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
180188

181189
case tea.KeyMsg:
182190

183-
// If our game is lost, any key shall terminate the program.
191+
// If our game is lost, any key shall restart the game.
184192
if m.gameOver {
185-
return m, tea.Quit
193+
194+
switch msg.String() {
195+
196+
case "ctrl+c", "q":
197+
return m, tea.Quit
198+
case "enter":
199+
m.init()
200+
return m, nil
201+
}
186202
}
187203

188204
switch msg.String() {
@@ -207,13 +223,20 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
207223
// View is required for building what we want to show on the screen.
208224
// That means we need to translate our model data into a string for displaying.
209225
func (m *model) View() string {
226+
builder := strings.Builder{}
227+
210228
if m.gameOver {
211229
// Just inform about game over and don't continue.
212-
return "Player died, Game Over!"
230+
builder.WriteString("\n\n\n\n\n")
231+
builder.WriteString(" You died, Game Over!")
232+
builder.WriteString("\n\n")
233+
builder.WriteString(fmt.Sprintf(" Your score: %d", m.score))
234+
builder.WriteString("\n\n")
235+
builder.WriteString(" Press enter to restart or q to quit")
236+
237+
return builder.String()
213238
}
214239

215-
builder := strings.Builder{}
216-
217240
// Iterate our table (2d array) and print non-empty fields as set in
218241
// our model. Empty (zero) fields shall be printed with a blank space.
219242
for _, row := range m.table {
@@ -235,7 +258,8 @@ func (m *model) View() string {
235258

236259
func main() {
237260
// Create our initial model.
238-
model := newModel()
261+
model := &model{}
262+
model.init()
239263

240264
// Program setup to initialize bubbletea and use full screen.
241265
program := tea.NewProgram(model, tea.WithAltScreen())

0 commit comments

Comments
 (0)