This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
snake.lisp
230 lines (207 loc) · 9.06 KB
/
snake.lisp
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
[let TILE 16]
[let GRID_X 20]
[let GRID_Y 12]
[let snake nil]
[let direction nil]
[let direction-id nil]
[let apple nil]
[let game-running nil]
[let score nil]
[let tile-head-left [q [1 1 1 3 3 3 1 1 1 2 3 4 5 4 3 3 1 3 4 6 5 5 5 5 1 3 4 4 5 5 5 5 1 3 4 4 5 5 5 5 1 3 4 6 5 5 5 5 1 2 3 4 5 4 3 3 1 1 1 3 3 3 1 1]]]
[let tile-head-right [q [1 1 3 3 3 1 1 1 3 3 4 5 4 3 2 1 5 5 5 5 6 4 3 1 5 5 5 5 4 4 3 1 5 5 5 5 4 4 3 1 5 5 5 5 6 4 3 1 3 3 4 5 4 3 2 1 1 1 3 3 3 1 1 1]]]
[let tile-head-down [q [1 3 5 5 5 5 3 1 1 3 5 5 5 5 3 1 3 4 5 5 5 5 4 3 3 5 5 5 5 5 5 3 3 4 6 4 4 6 4 3 1 3 4 4 4 4 3 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7 8 8 7 7 2 2 7 7 8 2 7 7 8 2 2 7 7 7 7 8 2 2 7 7 8 8 7 7 2 2 7 7 8 2 7 7 8 2 7 7 8 2 7 7 8 2 2 7 7 7 7 8 2]]]
[let tile-digit-9 [q [2 2 7 7 7 7 2 2 2 7 7 8 8 7 7 2 2 7 7 8 2 7 7 8 2 2 7 7 7 7 7 8 2 2 2 8 8 7 7 8 2 2 2 2 2 7 7 8 2 7 7 2 2 7 7 8 2 2 7 7 7 7 8 2]]]
[let tile-digits [list tile-digit-0 tile-digit-1 tile-digit-2 tile-digit-3 tile-digit-4 tile-digit-5 tile-digit-6 tile-digit-7 tile-digit-8 tile-digit-9]]
[defun tile-overlap [x y] [and [is [car x] [car y]] [is [cdr x] [cdr y]]]]
[defun draw-tile [x y tile] [do
[let i 0]
[while [< i TILE] [do
[let p [+ [* x TILE] [* [+ i [* y TILE]] 320]]]
[let j 0]
[let ts tile]
[while [< j TILE] [do
[let col [car tile]]
[= tile [cdr tile]]
[poke 10 p col]
[inc j]
[inc p]
[poke 10 p col]
[inc j]
[inc p]
]]
[inc i]
[= p [+ [* x TILE] [* [+ i [* y TILE]] 320]]]
[= j 0]
[= tile ts]
[while [< j TILE] [do
[let col [car tile]]
[= tile [cdr tile]]
[poke 10 p col]
[inc j]
[inc p]
[poke 10 p col]
[inc j]
[inc p]
]]
[inc i]
]]
]]
[defun draw-apple [] [do
[draw-tile [car apple] [cdr apple] tile-apple]
]]
[defun draw-snake-body [before pos after] [do
[draw-tile [car pos] [cdr pos] [if
[is [car before] [car after]] tile-body-v
[is [cdr before] [cdr after]] tile-body-h
[and [is [car before] [- [car pos] 1]] [is [cdr after] [- [cdr pos] 1]]] tile-body-nw
[and [is [car after] [- [car pos] 1]] [is [cdr before] [- [cdr pos] 1]]] tile-body-nw
[and [is [car before] [- [car pos] 1]] [is [cdr after] [+ [cdr pos] 1]]] tile-body-sw
[and [is [car after] [- [car pos] 1]] [is [cdr before] [+ [cdr pos] 1]]] tile-body-sw
[and [is [car before] [+ [car pos] 1]] [is [cdr after] [- [cdr pos] 1]]] tile-body-ne
[and [is [car after] [+ [car pos] 1]] [is [cdr before] [- [cdr pos] 1]]] tile-body-ne
tile-body-se
]]
]]
[defun draw-snake-head [p] [do
[draw-tile [car p] [cdr p] [if
[is direction-id 0] tile-head-left
[is direction-id 1] tile-head-down
[is direction-id 2] tile-head-right
tile-head-up]]
]]
[defun draw-snake-tail [before pos] [do
[draw-tile [car pos] [cdr pos] [if
[is [car before] [- [car pos] 1]] tile-tail-left
[is [car before] [+ [car pos] 1]] tile-tail-right
[is [cdr before] [- [cdr pos] 1]] tile-tail-up
tile-tail-down
]]
]]
[defun draw-background-piece [p] [do
[draw-tile [car p] [cdr p] tile-background]
]]
[defun draw-background [] [do
[let i 0]
[while [< i GRID_X] [do
[let j 0]
[while [< j GRID_Y] [do
[draw-background-piece [cons i j]]
[inc j]
]]
[inc i]
]]
]]
[defun move-apple [] [do
[= apple [cons [mod [random] GRID_X] [mod [random] GRID_Y]]]
[inc score]
]]
[defun wrap [x y] [if [< x 0] -1 [is x y] -1 x]]
[defun move-snake [] [do
[let head [car snake]]
[let moved-head [cons [wrap [+ [car head] [car direction]] GRID_X] [wrap [+ [cdr head] [cdr direction]] GRID_Y]]]
[= snake [cons moved-head snake]]
]]
[defun process-input [] [do
[let x [last-scancode]]
[if [is x 72] [do [= direction [cons 0 -1]] [= direction-id 3]] 0]
[if [is x 77] [do [= direction [cons 1 0]] [= direction-id 2]] 0]
[if [is x 80] [do [= direction [cons 0 1]] [= direction-id 1]] 0]
[if [is x 75] [do [= direction [cons -1 0]] [= direction-id 0]] 0]
]]
[defun game-over [] [do
[if game-running [= game-running nil] 0]
]]
[defun check-collision [] [do
[let s snake]
[let head [car s]]
[if [or [is [car head] -1] [is [cdr head] -1]] [game-over] 0]
[while s [do
[let t [cdr s]]
[while t [do
[if [tile-overlap [car s] [car t]]
[game-over] 0
]
[= t [cdr t]]
]]
[= s [cdr s]]
]]
]]
[defun show-score [] [do
[draw-tile 9 4 [nth tile-digits [mod [/ score 100] 10]]]
[draw-tile 10 4 [nth tile-digits [mod [/ score 10 ] 10]]]
[draw-tile 11 4 [nth tile-digits [mod [/ score 1 ] 10]]]
]]
[defun set-color [i r g b] [do
[outb 968 i]
[outb 969 r]
[outb 969 g]
[outb 969 b]
]]
[defun set-palette [] [do
[set-color 1 24 20 20]
[set-color 2 30 25 26]
[set-color 3 27 44 31]
[set-color 4 32 48 31]
[set-color 5 36 52 31]
[set-color 6 12 7 11]
[set-color 7 57 36 29]
[set-color 8 44 23 20]
[set-color 9 56 48 43]
]]
[defun before-last [x] [if [cdr [cdr x]] [before-last [cdr x]] x]]
[defun start-game [] [do
[set-graphics 1]
[set-palette]
[= game-running 1]
[= score 0]
[= snake [list [q [8 . 5]] [q [7 . 5]] [q [6 . 5]] [q [5. 5]]]]
[= direction [q [1 . 0]]]
[= direction-id 2]
[draw-background]
[move-apple]
[while game-running [do
[let tail [last snake]]
[draw-background-piece tail]
[move-snake]
[let head [car snake]]
[let body [car [cdr snake]]]
[let body-after [car [cdr [cdr snake]]]]
[draw-snake-body head body body-after]
[draw-snake-head head]
[if [tile-overlap head apple] [move-apple] [del-last snake]]
[let tail-before [before-last snake]]
[draw-snake-tail [car tail-before] [car [cdr tail-before]]]
[draw-apple]
[process-input]
[check-collision]
[pause]
[pause]
]]
[show-score]
[wait-key]
[set-graphics nil]
[print "Type [start-game] to play again!"]
]]
[start-game]