-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.tiny
307 lines (273 loc) · 9.23 KB
/
snake.tiny
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#############################################################################
#
# A Tiny script that implements a variation on the arcade game 'Snake'.
#
#############################################################################
import utils
import console
#
# Process options for the game
#
__snake_options = Utils.GetOpts('snake', args, {
numPellets: 15 # number of pellets to eat.
delay: 150 # iteration interval in milliseconds.
time: 150 # game duration in seconds
})
# Option error. GetOpts will have alerted the user so just exit.
if (__snake_options == null) {
return null
}
#############################################################################
#
# Object modelling the snake
#
snake = {
#
# List of segments that make up the snake's body
#
SegmentList : [
[13, 25], [14, 25], [15, 25],
[16, 25], [17, 25], [18, 25],
[19, 25], [20, 25], [21, 25]
];
#
# The direction the snake is currently moving in
#
Direction : "e";
#
# Method to draw the snake on the screen.
#
Render : {
char = '='
segCount = this.SegmentList.Count
foreach (el in this.SegmentList) {
segcount -= 1
if (segCount == 0) {
char = '@'
}
console.printat(el[0], el[1], char)
}
};
#
# Move the snake one cell forwards in the current direction.
#
Move : {
# See if the snake ate a pellet.
atePellet = pellets.CheckForPellet(this.SegmentList[-1])
# If we didn't get a pellet, remove the last segment
if ( ! atePellet) {
last = this.SegmentList[0]
console.printat(last[0], last[1], " ")
this.SegmentList = tail(this.SegmentList)
}
# Duplicate the head of the snake so we can update it.
hd = this.SegmentList[-1].Copy()
match this.direction
| 'n' -> {
if (hd[1]+1 >= console.GetWindowHeight()+1) {
this.Direction = 'e'
}
else {
hd[1] += 1
}
this.SegmentList.Add(hd)
}
| 's' -> {
if (hd[1]-1 <= 0) {
this.direction = 'w'
}
else {
hd[1] -= 1
}
this.SegmentList.Add(hd)
}
| 'e' -> {
if (hd[0]+1 >= console.GetWindowWidth()) {
this.direction = 's'
}
else {
hd[0] += 1
}
this.SegmentList.Add(hd)
}
| 'w' -> {
if (hd[0]-1 <= 0) {
this.direction = 'n'
}
else {
hd[0] -= 1
}
this.SegmentList.Add(hd)
}
this.render()
}
}
#############################################################################
#
# Object modelling the pellets on the playing field.
#
pellets = {
Score: 0 # multiple of the number of pellets consumed
Pellets: [] # List of the pellet locations
StatusColor: 'cyan'
# Method to show the game status.
ShowStatus: {
oldFg = console.GetForeGroundColor();
secondsRemaining = (endTime - GetDate()).totalseconds as [<int>];
try {
this.StatusColor =
if (secondsRemaining <= 30) {
# If time is running out, blink the status bar red and yellow.
if (this.Statuscolor == 'yellow') { 'red' } else { 'yellow' }
}
else {
'cyan'
}
console.SetForegroundColor(this.Statuscolor)
console.PrintAt(0, console.GetWindowHeight()-1,
"Score: {0,3} Pellets remaining {1,2} Time remaining: {2} seconds ",
this.Score, this.Pellets.Count, secondsRemaining)
}
finally {
console.SetForegroundColor(oldFg)
}
}
#
# Initialize the pellet field
#
Initialize: {
# Generate the pellet locations by taking two random lists and zipping them together.
snake_len = Snake.SegmentList.Count
this.pellets =
getrandom(__snake_options.numPellets + snake_len, 1, console.GetWindowWidth()-1)
|> zip( getrandom(__snake_options.NumPellets + snake_len, 1, console.GetWindowHeight()-3))
# make sure none of them land on the snake
|> where {Snake.SegmentList !:> it}
|> first(__snake_options.numPellets)
# Then draw the pellets on the screen.
oldFg = console.GetForeGroundColor();
console.SetForegroundColor('green')
this.pellets.foreach{ console.printat(it[0], it[1], 'O') }
console.SetForegroundColor(oldFg)
this.ShowStatus()
}
ColorList: ['yellow', 'green', 'cyan', 'magenta', 'red']
ColorIndex: 0
NextColor: {
this.colorIndex = (this.colorIndex + 1) % this.ColorList.Count
this.ColorList[this.ColorIndex]
}
#
# Method that handles the case where the snake eats a pellet.
#
CheckForPellet: {
location ->
# See if the pellet list contains this location
if (this.Pellets :> location) {
# If so, show the eating animation
oldFg = console.GetForeGroundColor();
(1..4).foreach {
console.SetForegroundColor('red')
console.PrintAt(location[0], location[1], 'X');
sleep(75)
#console.SetForegroundColor('yellow')
console.PrintAt(location[0], location[1], '+');
sleep(75)
}
console.SetForegroundColor(oldFg)
# Increment the score
this.score += 10
# And remove the pellet from the list.
this.Pellets = this.Pellets.Where{it != location}
this.showStatus()
# Change the snakes color
console.SetForegroundColor(this.NextColor())
return true
}
else {
this.showStatus()
return false
}
}
}
#############################################################################
#
# The main game loop.
#
oldFg = console.GetForeGroundColor();
try {
cls()
console.SetCursorVisible(false)
key = console.Announce(
"Welcome to the Snake game. Your job is to guide the snake " +
"over the playing field so it can eat all of the food pellets. " +
"If you get all of the pellets before the time runs out, you " +
"win! In this game, you have to eat ${__snake_options.numPellets} pellets " +
"in ${__snake_options.time} seconds. Use the arrow keys to control the " +
"snake's direction, the space bar will cause the snake to have a burst of speed. " +
"You can quit at any time by pressing 'q'. Get ready to play!",
"green"
).Key
if (key == 'q') {
cls()
println("Thanks for playing! Bye bye!")
return
};
cls()
fastCount = 0 # The number of iterations to move the snake quickly.
console.SetForegroundColor('yellow')
endTime = GetDate().AddSeconds(__snake_options.time)
pellets.Initialize()
snake.Render();
while (true) {
snake.Move()
timeRemaining = endTime - getdate()
# If time runs out, you lose.
if (timeRemaining <= 0) {
msg = "Time has run out and the game is over! " +
"You earned ${pellets.Score} points; There were ${pellets.Pellets.Count} pellets left."
console.Announce(msg, 'yellow')
cls()
break;
}
# If the pellet count is zero, you win the game.
if (pellets.Pellets.Count == 0) {
msg = "Game over! You win with ${pellets.Score} points " +
"and ${timeRemaining.TotalSeconds as [<int>]} seconds left. " +
"Congratulations!"
console.Announce(msg, 'green')
cls()
break;
}
# Figure out if we should iterate slow or fast.
if (fastCount == 0) {
sleep(__snake_options.delay)
}
else {
fastCount -= 1
}
# Process the user key presses.
if (console.GetkeyAvailable()) {
match console.ReadKey(true).key
# change the snake's direction
| 'LeftArrow' -> snake.Direction = 'w'
| 'RightArrow' -> snake.Direction = 'e'
| 'UpArrow' -> snake.Direction = 's'
| 'DownArrow' -> snake.Direction = 'n'
# tell the snake to move quickly for 20 moves.
| 'f' -> fastCount = 20
| 'SpaceBar' -> fastCount = 20
# If the user types 'q', end the game.
| 'q' -> {
console.Announce("Thanks for playing! Hope you had a good time!", 'yellow')
cls();
break
}
}
}
}
finally {
console.SetCursorVisible(true)
console.SetForegroundColor(oldFg)
}
println("Bye bye!")