-
Notifications
You must be signed in to change notification settings - Fork 10
/
Snake Game Using Go Language
199 lines (174 loc) · 3.69 KB
/
Snake Game Using Go Language
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
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"math/rand"
"time"
termbox "github.com/nsf/termbox-go"
)
// Constants
const (
width = 20
height = 15
)
// Point represents a 2D coordinate.
type Point struct {
x, y int
}
// Direction represents the current direction of the snake.
type Direction int
const (
Up Direction = iota
Down
Left
Right
)
// Snake represents the snake in the game.
type Snake struct {
Body []Point
Direction Direction
}
var (
food Point
score int
gameOver bool
)
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
// Initialize game variables
snake := Snake{
Body: []Point{{2, 2}},
}
food = generateFood(snake)
score = 0
gameOver = false
// Set up the game loop
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
// Start listening for keyboard events in a separate goroutine
go func() {
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyArrowUp:
if snake.Direction != Down {
snake.Direction = Up
}
case termbox.KeyArrowDown:
if snake.Direction != Up {
snake.Direction = Down
}
case termbox.KeyArrowLeft:
if snake.Direction != Right {
snake.Direction = Left
}
case termbox.KeyArrowRight:
if snake.Direction != Left {
snake.Direction = Right
}
case termbox.KeyEsc:
gameOver = true
}
}
}
}()
// Main game loop
for !gameOver {
select {
case <-ticker.C:
if !gameOver {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
updateGame(&snake)
drawGame(&snake)
termbox.Flush()
}
}
}
// Display game over message
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
printMessage("Game Over! Your Score: "+fmt.Sprint(score), width/2, height/2)
termbox.Flush()
// Wait for a key press to exit
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey {
break
}
}
}
func generateFood(snake Snake) Point {
rand.Seed(time.Now().UnixNano())
var food Point
for {
food = Point{rand.Intn(width), rand.Intn(height)}
collided := false
for _, segment := range snake.Body {
if food == segment {
collided = true
break
}
}
if !collided {
break
}
}
return food
}
func updateGame(snake *Snake) {
// Move the snake
head := snake.Body[0]
var newHead Point
switch snake.Direction {
case Up:
newHead = Point{head.x, head.y - 1}
case Down:
newHead = Point{head.x, head.y + 1}
case Left:
newHead = Point{head.x - 1, head.y}
case Right:
newHead = Point{head.x + 1, head.y}
}
// Check for collision with food
if newHead == food {
snake.Body = append([]Point{newHead}, snake.Body...)
food = generateFood(*snake)
score++
} else {
// Move snake
snake.Body = append([]Point{newHead}, snake.Body[:len(snake.Body)-1]...)
}
// Check for collision with walls or itself
if newHead.x < 0 || newHead.x >= width || newHead.y < 0 || newHead.y >= height {
gameOver = true
} else {
for _, segment := range snake.Body[1:] {
if newHead == segment {
gameOver = true
break
}
}
}
}
func drawGame(snake *Snake) {
// Draw food
termbox.SetCell(food.x, food.y, 'F', termbox.ColorRed, termbox.ColorDefault)
// Draw snake
for i, segment := range snake.Body {
if i == 0 {
termbox.SetCell(segment.x, segment.y, 'O', termbox.ColorGreen, termbox.ColorDefault)
} else {
termbox.SetCell(segment.x, segment.y, 'o', termbox.ColorGreen, termbox.ColorDefault)
}
}
// Display score
printMessage("Score: "+fmt.Sprint(score), 0, height)
}
func printMessage(message string, x, y int) {
for i, ch := range message {
termbox.SetCell(x+i, y, ch, termbox.ColorWhite, termbox.ColorDefault)
}
}