Skip to content

Commit 30e0105

Browse files
committed
✨ (mine-sweeper): add elapse time show
1 parent 970971f commit 30e0105

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

internal/game/game.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package game
22

3+
import "time"
4+
35
// Cell - 單一格子
46
type Cell struct {
57
IsMine bool // 是否為地雷
@@ -21,10 +23,10 @@ type Board struct {
2123

2224
// Game - 遊戲物件
2325
type Game struct {
24-
Board *Board // 棋盤物件
25-
IsGameOver bool // 是否遊戲結束
26-
IsPlayerWin bool // 玩家是否獲勝
27-
26+
Board *Board // 棋盤物件
27+
IsGameOver bool // 是否遊戲結束
28+
IsPlayerWin bool // 玩家是否獲勝
29+
startTime time.Time // 遊戲開始時間
2830
}
2931

3032
// coord - 紀錄該格字座標
@@ -46,6 +48,7 @@ func NewGame(rows, cols, mineCount int) *Game {
4648
Board: board,
4749
IsGameOver: false,
4850
IsPlayerWin: false,
51+
startTime: time.Now().UTC(),
4952
}
5053
}
5154

@@ -251,3 +254,8 @@ func (board *Board) CheckIsPlayerWin() bool {
251254
func (board *Board) GetRemainingFlags() int {
252255
return board.remainingFlags
253256
}
257+
258+
// GetElapsedTime - 取出從 startTime 之後到目前為止的時間
259+
func (g *Game) GetElapsedTime() int {
260+
return int(time.Since(g.startTime).Seconds())
261+
}

internal/layout/font.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ const (
3333
IsMine = iota + 100
3434
IsFlag
3535
IsButtonIcon
36+
IsClock
3637
)
3738

3839
func getTileColor(value int) color.Color {
3940
switch value {
4041
case 0:
4142
return color.RGBA{0x77, 0x6e, 0x65, 0xff}
42-
case IsFlag:
43-
return color.RGBA{0xf9, 0xf6, 0xf2, 0xff}
4443
case IsMine, IsButtonIcon:
4544
return color.Black
45+
case IsClock, IsFlag:
46+
return color.RGBA{0xff, 0, 0, 0xff}
4647
default:
4748
return color.RGBA{0xf9, 0xf6, 0xf2, 0xff}
4849
}

internal/layout/layout.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
PaddingY = 20 // 面板
2222
ScreenHeight = PanelHeight + gridSize*Rows
2323
ScreenWidth = gridSize * Cols
24-
MineCounts = 9
24+
MineCounts = 10
2525
)
2626

2727
var buttonRectRelativePos = image.Rect(0, 0, 32, 32) // 一個方格大小的 button
@@ -30,9 +30,12 @@ type Coord struct {
3030
Row int
3131
Col int
3232
}
33+
34+
// 遊戲畫面狀態
3335
type GameLayout struct {
34-
gameInstance *game.Game
35-
ClickCoord *Coord
36+
gameInstance *game.Game // 遊戲物件
37+
ClickCoord *Coord // 使用者點擊座標
38+
elapsedTime int // 經過時間
3639
}
3740

3841
func NewGameLayout(gameInstance *game.Game) *GameLayout {
@@ -50,6 +53,10 @@ func (g *GameLayout) Update() error {
5053
g.Restart()
5154
}
5255
}
56+
// 當遊戲還沒停止時,就更新經過時間
57+
if !g.gameInstance.IsGameOver && !g.gameInstance.IsPlayerWin {
58+
g.elapsedTime = g.gameInstance.GetElapsedTime()
59+
}
5360
// 當狀態為遊戲結束
5461
if g.gameInstance.IsGameOver || g.gameInstance.IsPlayerWin {
5562
return nil
@@ -163,7 +170,7 @@ func (g *GameLayout) drawFlag(screen *ebiten.Image, row, col int) {
163170
textXPos := col*gridSize + (gridSize)/2
164171
textYPos := PanelHeight + row*gridSize + (gridSize)/2
165172
textOpts := &text.DrawOptions{}
166-
textOpts.ColorScale.ScaleWithColor(getTileColor(IsFlag))
173+
textOpts.ColorScale.ScaleWithColor(getTileColor(-1))
167174
textOpts.PrimaryAlign = text.AlignCenter
168175
textOpts.SecondaryAlign = text.AlignCenter
169176
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
@@ -230,6 +237,8 @@ func (g *GameLayout) drawGamePanel(screen *ebiten.Image) {
230237
g.drawRemainingFlagInfo(screen)
231238
// 畫顯示狀態 button
232239
g.drawButtonWithIcon(screen, emojiIcon)
240+
// 畫出經過時間
241+
g.drawElaspedTimeInfo(screen)
233242
}
234243

235244
// drawButtonWithIcon - 繪製 buttonIcon
@@ -279,13 +288,42 @@ func (g *GameLayout) drawRemainingFlagInfo(screen *ebiten.Image) {
279288
emojiXPos := len(emojiValue)
280289
emojiYPos := PaddingY
281290
emojiOpts := &text.DrawOptions{}
282-
emojiOpts.ColorScale.ScaleWithColor(getTileColor(-1))
291+
emojiOpts.ColorScale.ScaleWithColor(getTileColor(IsFlag))
283292
emojiOpts.PrimaryAlign = text.AlignStart
284293
emojiOpts.SecondaryAlign = text.AlignCenter
285294
emojiOpts.GeoM.Translate(float64(emojiXPos), float64(emojiYPos))
286295
text.Draw(screen, emojiValue, &text.GoTextFace{
287296
Source: emojiFaceSource,
297+
Size: 30,
298+
}, emojiOpts)
299+
}
300+
301+
// drawElaspedTimeInfo
302+
func (g *GameLayout) drawElaspedTimeInfo(screen *ebiten.Image) {
303+
// 畫旗子面板(固定在左方)
304+
textValue := fmt.Sprintf("%03d", g.elapsedTime)
305+
textXPos := ScreenWidth - gridSize/2 + len(textValue)
306+
textYPos := PaddingY
307+
textOpts := &text.DrawOptions{}
308+
textOpts.ColorScale.ScaleWithColor(getTileColor(-1))
309+
textOpts.PrimaryAlign = text.AlignEnd
310+
textOpts.SecondaryAlign = text.AlignCenter
311+
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
312+
text.Draw(screen, textValue, &text.GoTextFace{
313+
Source: mplusFaceSource,
288314
Size: 20,
315+
}, textOpts)
316+
emojiValue := "⏰"
317+
emojiXPos := ScreenWidth - 3*gridSize + len(emojiValue)
318+
emojiYPos := PaddingY
319+
emojiOpts := &text.DrawOptions{}
320+
emojiOpts.ColorScale.ScaleWithColor(getTileColor(IsClock))
321+
emojiOpts.PrimaryAlign = text.AlignStart
322+
emojiOpts.SecondaryAlign = text.AlignCenter
323+
emojiOpts.GeoM.Translate(float64(emojiXPos), float64(emojiYPos))
324+
text.Draw(screen, emojiValue, &text.GoTextFace{
325+
Source: emojiFaceSource,
326+
Size: 30,
289327
}, emojiOpts)
290328
}
291329

0 commit comments

Comments
 (0)