-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
96 lines (79 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"math/rand"
"time"
tl "github.com/JoelOtter/termloop"
"github.com/nsf/termbox-go"
)
var score = 0
var game *tl.Game
var border *Border
var scoreText *tl.Text
var isFullscreen *bool
type endgameScreen struct {
*tl.BaseLevel
}
// Handle events on the endLevel. Enter resets.
func (eg *endgameScreen) Tick(event tl.Event) {
if event.Type == tl.EventKey { // Is it a keyboard event?
if event.Key == tl.KeyEnter {
score = 0
game.Screen().SetLevel(newMainLevel(isFullscreen))
}
}
}
// IncreaseScore increases the score by the given amount and updates the
// score text.
func IncreaseScore(amount int) {
score += amount
scoreText.SetText(fmt.Sprint(" Score: ", score, " "))
}
// EndGame should be called when the game ends due to e.g. dying.
func EndGame() {
endLevel := tl.NewBaseLevel(tl.Cell{
Bg: tl.ColorRed,
})
el := new(endgameScreen)
el.BaseLevel = endLevel
var PromptQuestion, PromptText *tl.Text
PromptQuestion = tl.NewText(34, 17, " Play Again? ", tl.ColorBlue, tl.ColorWhite)
PromptText = tl.NewText(34, 18, " Press Enter ", tl.ColorBlue, tl.ColorWhite)
scoreText.SetPosition(35, 14)
scoreText.SetColor(tl.ColorBlue, tl.ColorWhite)
el.AddEntity(scoreText)
el.AddEntity(PromptQuestion)
el.AddEntity(PromptText)
game.Screen().SetLevel(el)
}
func newMainLevel(isFullscreen *bool) tl.Level{
mainLevel := tl.NewBaseLevel(tl.Cell{
Bg: tl.ColorBlack,
})
width, height := 80, 30
if *isFullscreen {
// Must initialize Termbox before getting the terminal size
termbox.Init()
width, height = termbox.Size()
}
border = NewBorder(width, height)
snake := NewSnake()
food := NewFood()
scoreText = tl.NewText(0, 0, " Score: 0", tl.ColorBlack, tl.ColorBlue)
mainLevel.AddEntity(border)
mainLevel.AddEntity(snake)
mainLevel.AddEntity(food)
mainLevel.AddEntity(scoreText)
return mainLevel
}
func main() {
isFullscreen = flag.Bool("fullscreen", false, "Play fullscreen!")
flag.Parse()
rand.Seed(time.Now().UnixNano())
game = tl.NewGame()
mainLevel := newMainLevel(isFullscreen)
game.Screen().SetLevel(mainLevel)
game.Screen().SetFps(10)
game.Start()
}