-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (121 loc) · 2.57 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"errors"
"fmt"
"log"
"os"
"golang.org/x/term"
)
const (
mStart = iota
mEnd
mBoth
)
type ExitError struct {
msg string
}
func (e *ExitError) SetError(msg error) {
e.msg = msg.Error()
}
func (e *ExitError) Error() string {
return e.msg
}
type Inst struct {
Mode int
Code string
}
func clearScreen(mode int) {
instrs := []Inst{
{mBoth, "\x1b[2J"}, // clear the screen
{mBoth, "\x1b[H"}, // CUP - get the cursor UP (top left)
//{mStart, "\x1b[2J"}, // clear the screen
//{mStart, "\x1b[H"}, // CUP - get the cursor UP (top left)
{mStart, "\x1b[?25l"}, // hide the cursor
{mEnd, "\x1b[?25h"}, // display the cursor
}
for _, inst := range instrs {
currMode := inst.Mode
if currMode == mBoth || currMode == mode {
fmt.Fprint(os.Stdout, inst.Code)
}
}
}
var stdinFd int
var state *term.State
var initError error
var exitError *ExitError
var f *os.File
func shutDown(stdinFd int, state *term.State, err error) {
term.Restore(stdinFd, state)
clearScreen(mEnd)
fmt.Println(err)
}
func enableLogging() {
// called in 'init' if required
var fErr error
f, fErr = os.OpenFile("/tmp/snake.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fErr != nil {
log.Fatalf("error opening file: %v", fErr)
}
log.SetOutput(f)
}
func init() {
clearScreen(mStart)
stdinFd = int(os.Stdout.Fd())
state, initError = term.MakeRaw(stdinFd)
exitError = new(ExitError)
}
func processKeyPress() (chan error, chan byte) {
out := make(chan error)
keyChan := make(chan byte)
go func() {
for {
buf := make([]byte, 1)
_, err := os.Stdin.Read(buf) // dirty way to read from stdin
if buf[0] == 3 || buf[0] == 4 {
out <- errors.New("player quit the game")
} else if err != nil {
out <- err
} else {
keyChan <- buf[0]
}
}
}()
return out, keyChan
}
func main() {
if initError != nil {
fmt.Fprintln(os.Stderr, "err:", initError)
return
}
defer shutDown(stdinFd, state, exitError)
defer f.Close()
c, r, err := term.GetSize(stdinFd)
if err != nil {
fmt.Fprintln(os.Stderr, "couldn't get screen size, err:", err)
return
}
if r < 21 || c < 44 {
exitError.SetError(errors.New("screen size too small to play"))
return
}
exitChan, keyChan := processKeyPress()
snake := NewSnake()
game := NewGame(snake, r, c, keyChan)
game.Start()
OUT:
for _ = range game.Tick() {
select {
case msg := <-exitChan:
exitError.SetError(msg)
close(exitChan)
close(keyChan)
game.Stop()
// NOTE: now that we're calling game.Stop()
// can we get rid of the break statement?
break OUT
default:
}
game.Refresh()
}
}