-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (133 loc) · 3.21 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
package main
/*
* The author: @MayconParanhos || @mayconparanhos (Twitter)
* This Console Snake Game, written in Go, is based on the following game: (https://github.com/serene-dev/snake-c)
*/
import (
"fmt"
"math/rand"
"os"
"os/exec"
"time"
)
const COLS int = 60
const ROWS int = 30
func main() {
// HIDE CURSOR
fmt.Print("\033[?25l")
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() // the same as -icanon
exec.Command("stty", "-F", "/dev/tty", "-echo").Run() // do not display entered characters on the screen
var key string
var char = make(chan string)
go func(channel chan string) {
var c []byte = make([]byte, 1)
for {
os.Stdin.Read(c)
channel <- string(c)
}
}(char)
var x, y = make([]int, 1000), make([]int, 1000)
var quit bool = false
for !quit {
RenderTable()
// MOVE CURSOR BACK TO TOP
fmt.Printf("\033[%dA", ROWS+2)
var head, tail int = 0, 0
x[head] = COLS / 2
y[head] = ROWS / 2
var gameOver bool = false
var xdir, ydir int = 1, 0
var applex, appley int = -1, 0
for !quit && !gameOver {
if applex < 0 {
// CREATE NEW APPLE
applex = rand.Intn(COLS)
appley = rand.Intn(ROWS)
for i := tail; i != head; i = (i + 1) % 1000 {
if x[i] == applex && y[i] == appley {
applex = -1
}
}
if applex >= 0 {
// DRAW APPLE
fmt.Printf("\033[%dB\033[%dC❤", appley+1, applex+1)
fmt.Printf("\033[%dF", appley+1)
}
}
// CLEAR SNAKE TAIL
fmt.Printf("\033[%dB\033[%dC·", y[tail]+1, x[tail]+1)
fmt.Printf("\033[%dF", y[tail]+1)
if x[head] == applex && y[head] == appley {
applex = -1
fmt.Print("\a") // THE BELL
} else {
tail = (tail + 1) % 1000
}
var newhead int = (head + 1) % 1000
x[newhead] = (x[head] + xdir + COLS) % COLS
y[newhead] = (y[head] + ydir + ROWS) % ROWS
head = newhead
for i := tail; i != head; i = (i + 1) % 1000 {
if x[i] == x[head] && y[i] == y[head] {
gameOver = true
}
}
/// DRAW HEAD
fmt.Printf("\033[%dB\033[%dC▓", y[head]+1, x[head]+1)
fmt.Printf("\033[%dF", y[head]+1)
time.Sleep(time.Microsecond * 84000)
/// READ KEYBOARD
select {
case key = <-char:
if key == "q" {
quit = true
exec.Command("stty", "-F", "/dev/tty", "echo").Run()
} else if key == "a" && xdir != 1 {
xdir = -1
ydir = 0
} else if key == "d" && xdir != -1 {
xdir = 1
ydir = 0
} else if key == "s" && ydir != -1 {
xdir = 0
ydir = 1
} else if key == "w" && ydir != 1 {
xdir = 0
ydir = -1
}
default:
}
/// END READ KEYBOARD
}
if !quit {
/// SHOW GAMEOVER
fmt.Printf("\033[%dB\033[%dC Game Over! ", ROWS/2, COLS/2-5)
fmt.Printf("\033[%dF", ROWS/2)
time.Sleep(time.Second * 2)
}
}
// SHOW CURSOR
fmt.Print("\033[?25h")
}
func RenderTable() {
fmt.Print("┌")
for i := 0; i < COLS; i++ {
fmt.Print("─")
}
fmt.Print("┐\n")
////////////////////////////////////////
for j := 0; j < ROWS; j++ {
fmt.Print("│")
for i := 0; i < COLS; i++ {
fmt.Print("·")
}
fmt.Print("│\n")
}
////////////////////////////////////////
fmt.Print("└")
for i := 0; i < COLS; i++ {
fmt.Print("─")
}
fmt.Print("┘\n")
}