-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame-of-Snake.ps1
More file actions
265 lines (220 loc) · 5.47 KB
/
Copy pathGame-of-Snake.ps1
File metadata and controls
265 lines (220 loc) · 5.47 KB
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
#requires -version 2
#
# Powershell Snake Game
# Author : Kurt Jaegers
#
function SetEmptySquare($x, $y)
{
$matrix[$x, $y] = $emptysquare
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor White -BackgroundColor Black -NoNewline " "
}
function SetBodySquare($x, $y)
{
$matrix[$x, $y] = $bodysquare
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor White -BackgroundColor White -NoNewline " "
}
#
# Draws the snake to the screen, including cleaning up the last segment of the tail
#
function DrawTheSnake($x, $y)
{
$newPoint = New-Object System.Drawing.Point($x, $y)
$tail.Enqueue($newPoint)
SetBodySquare -x $x -y $y
if ($tail.Count -gt $script:maxTailLength)
{
$oldPoint = $tail.Dequeue()
SetEmptySquare -x $oldPoint.X -y $oldPoint.Y
}
}
#
# Generate a random location for the apple, making sure it isnt inside the snake
#
function MoveTheApple
{
do
{
$x = get-random -min 2 -max ($width - 2)
$y = get-random -min 2 -max ($height - 2)
}
until ($matrix[$x, $y] -eq $emptysquare )
$matrix[$x, $y] = $applesquare
DrawTheApple -x $x -y $y
}
#
# Draw the apple to the screen
#
function DrawTheApple($x, $y)
{
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -foregroundcolor red -backgroundcolor black "@"
}
#
# Check to see if the snake hits the apple
#
function CheckAppleHit($x, $y)
{
if ($matrix[$x, $y] -eq $applesquare)
{
# relocate the apple
MoveTheApple
SetEmptySquare -x $x -y $y
$script:score += 500
# Add to the snake's length
$script:maxTailLength++
}
}
#
# Check to see if the snake's head hits the walls of the screen
#
function CheckWallHits($x, $y)
{
if ($matrix[$x, $y] -eq $wallsquare)
{
cls
write-host -foregroundcolor red "You lost! Score was $script:score"
exit
}
}
function SetBorderSquare($x, $y)
{
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor Black -BackgroundColor White '#' -NoNewline
$matrix[$x, $y] = $wallsquare
}
#
# Draw a fence around the edges of the screen
#
function DrawScreenBorders
{
for ($x = 0; $x -lt $width; $x++)
{
SetBorderSquare -x $x -y 0
SetBorderSquare -x $x -y ($height - 1)
}
for ($y = 0; $y -lt $height; $y++)
{
SetBorderSquare -x 0 -y $y
SetBorderSquare -x ($width - 1) -y $y
}
}
function CheckSnakeBodyHits($x, $y)
{
if ($matrix[$x, $y] -eq $bodysquare)
{
cls
write-host -foregroundcolor red "You lost! Score was $script:score"
exit
}
}
function DrawScore($score)
{
$string = "Score: $score"
$xPos = [int](($script:width - $string.Length) / 2)
[console]::SetCursorPosition($xPos, 0)
Write-Host -ForegroundColor Red -BackgroundColor Black $string
}
# ---------------------------------
# ---------------------------------
# Main script block starts here
# ---------------------------------
# ---------------------------------
if ($host.name -ne "ConsoleHost")
{
write-host "This script should only be run in a ConsoleHost window (outside of the ISE)"
exit
$done=$true
}
Add-Type -AssemblyName System.Drawing
# Grab UI objects and set some colors
$ui=(get-host).ui
$rui=$ui.rawui
$rui.BackgroundColor="Black"
$rui.ForegroundColor="Red"
cls
# write out lines to make sure the buffer is big enough to cover the screen
for ($i=0; $i -lt $rui.screensize.height; $i++)
{
write-host ""
}
$cs = $rui.cursorsize
$rui.cursorsize=0
$script:score = 0
$width = $rui.WindowSize.Width - 2
$height = $rui.WindowSize.Height - 2
$emptysquare = 0
$bodysquare = 1
$applesquare = 2
$wallsquare = 3
$currentX = [int]($width / 2)
$currentY = [int]($height / 2)
$matrix = New-Object 'int[,]' -ArgumentList ($width, $height)
$tail = New-Object System.Collections.Queue
$script:maxTailLength = 5
$done = $false
$before = 0
$after = 15
$dir = 0
DrawScreenBorders;
DrawTheSnake -x $currentX -y $currentY
MoveTheApple;
while (!$done)
{
if ($rui.KeyAvailable)
{
$key = $rui.ReadKey()
if ($key.virtualkeycode -eq -27)
{
$done=$true
}
if ($key.keydown)
{
# Left
if ($key.virtualkeycode -eq 37)
{
$dir=0
}
# Up
if ($key.virtualkeycode -eq 38)
{
$dir=1
}
# Right
if ($key.virtualkeycode -eq 39)
{
$dir=2
}
# Down
if ($key.virtualkeycode -eq 40)
{
$dir=3
}
}
}
if ($dir -eq 0)
{
$currentX--;
}
if ($dir -eq 1)
{
$currentY--;
}
if ($dir -eq 2)
{
$currentX++;
}
if ($dir -eq 3)
{
$currentY++;
}
CheckWallHits -x $currentX -y $currentY
CheckSnakeBodyHits -x $currentX -y $currentY
CheckAppleHit -x $currentX -y $currentY
DrawTheSnake -x $currentX -y $currentY
$script:score += $script:maxTailLength
DrawScore -score $script:score
start-sleep -mil 100
}
$rui.cursorsize=$cs