forked from danicat/pacgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (157 loc) · 2.86 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"github.com/danicat/simpleansi"
)
type sprite struct {
row int
col int
}
var player sprite
var maze []string
func loadMaze(file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}
for row, line := range maze {
for col, char := range line {
switch char {
case 'P':
player = sprite{row, col}
}
}
}
return nil
}
func printScreen() {
simpleansi.ClearScreen()
for _, line := range maze {
for _, chr := range line {
switch chr {
case '#':
fmt.Printf("%c", chr)
default:
fmt.Print(" ")
}
}
fmt.Println()
}
simpleansi.MoveCursor(player.row, player.col)
fmt.Print("P")
// Move cursor outside of maze drawing area
simpleansi.MoveCursor(len(maze)+1, 0)
}
func readInput() (string, error) {
buffer := make([]byte, 100)
cnt, err := os.Stdin.Read(buffer)
if err != nil {
return "", err
}
if cnt == 1 && buffer[0] == 0x1b {
return "ESC", nil
} else if cnt >= 3 {
if buffer[0] == 0x1b && buffer[1] == '[' {
switch buffer[2] {
case 'A':
return "UP", nil
case 'B':
return "DOWN", nil
case 'C':
return "RIGHT", nil
case 'D':
return "LEFT", nil
}
}
}
return "", nil
}
func makeMove(oldRow, oldCol int, dir string) (newRow, newCol int) {
newRow, newCol = oldRow, oldCol
switch dir {
case "UP":
newRow = newRow - 1
if newRow < 0 {
newRow = len(maze) - 1
}
case "DOWN":
newRow = newRow + 1
if newRow == len(maze) {
newRow = 0
}
case "RIGHT":
newCol = newCol + 1
if newCol == len(maze[0]) {
newCol = 0
}
case "LEFT":
newCol = newCol - 1
if newCol < 0 {
newCol = len(maze[0]) - 1
}
}
if maze[newRow][newCol] == '#' {
newRow = oldRow
newCol = oldCol
}
return
}
func movePlayer(dir string) {
player.row, player.col = makeMove(player.row, player.col, dir)
}
func initialise() {
cbTerm := exec.Command("stty", "cbreak", "-echo")
cbTerm.Stdin = os.Stdin
err := cbTerm.Run()
if err != nil {
log.Fatalln("unable to activate cbreak mode:", err)
}
}
func cleanup() {
cookedTerm := exec.Command("stty", "-cbreak", "echo")
cookedTerm.Stdin = os.Stdin
err := cookedTerm.Run()
if err != nil {
log.Fatalln("unable to activate cooked mode:", err)
}
}
func main() {
// initialise game
initialise()
defer cleanup()
// load resources
err := loadMaze("maze01.txt")
if err != nil {
log.Println("failed to load maze:", err)
return
}
// game loop
for {
// update screen
printScreen()
// process input
input, err := readInput()
if err != nil {
log.Println("error reading input:", err)
break
}
// process movement
movePlayer(input)
// process collisions
// check game over
if input == "ESC" {
break
}
// repeat
}
}