-
Notifications
You must be signed in to change notification settings - Fork 0
/
218.rkt
266 lines (205 loc) · 9.49 KB
/
218.rkt
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
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname 218ex) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; Exercise 218
;; ------------
;; Redesign your program from exercise 217 so that it stops if the worm has run
;; into the walls of the world or into itself. Display a message like the one
;; in exercise 216 to explain whether the program stopped because the worm hit
;; the wall or because it ran into itself.
;; -----------------------------------------------------------------------------
(require 2htdp/image)
(require 2htdp/universe)
(define SCENE-HEIGHT 500)
(define SCENE-WIDTH 500)
(define SNAKE-RADIUS 50)
(define FOOD-RADIUS 50)
(define BACKG-COLOR "Dark Yellow")
(define SNAKE-COLOR "Light Brown")
(define FOOD-COLOR"Light Goldenrod")
(define BACKG (rectangle SCENE-WIDTH SCENE-HEIGHT "solid" FOOD-COLOR))
(define SNAKE (circle SNAKE-RADIUS "solid" SNAKE-COLOR))
(define FOOD (circle FOOD-RADIUS "solid" FOOD-COLOR))
(define SNAKE-DIAMETER (* 2 SNAKE-RADIUS))
(define SNAKE-STEP SNAKE-DIAMETER)
; ----------
; Direction is one of:
; - "up"
; - "down"
; - "left"
; - "right"
; interpretation the current direction of the snake
(define UP "up")
(define DOWN "down")
(define LEFT "left")
(define RIGHT "right")
(define-struct snake-part [dir pos])
; SnakePart is a structure:
; (make-snake-part Direction Posn)
; interpretation the direction and current position of the snake part
(define SNAKE-PART-LEFT (make-snake-part RIGHT (make-posn 50 250)))
(define SNAKE-PART-RIGHT (make-snake-part RIGHT (make-posn 450 250)))
(define SNAKE-PART-TOP (make-snake-part DOWN (make-posn 250 50)))
(define SNAKE-PART-DOWN (make-snake-part DOWN (make-posn 50 450)))
(define SNAKE-PART-TOP-LEFT (make-snake-part LEFT (make-posn 50 50)))
(define SNAKE-PART-TOP-RIGHT (make-snake-part LEFT (make-posn 450 50)))
(define SNAKE-PART-BOTTOM-LEFT (make-snake-part UP (make-posn 50 450)))
(define SNAKE-PART-BOTTOM-RIGHT (make-snake-part UP (make-posn 450 450)))
(define SNAKE-PART-CENTER (make-snake-part RIGHT (make-posn 250 250)))
; A Snake is one of:
; - '()
; - (cons SnakePart Snake)
(define SNAKE-MID-LEFT
(list SNAKE-PART-CENTER
(make-snake-part "right" (make-posn 150 250))
(make-snake-part "right" (make-posn 50 250))))
; ----------
; The WorldState is a Snake
; Snake -> Image
; renders the full snake by rendering each part
(check-expect (render SNAKE-MID-LEFT)
(place-image SNAKE 250 250
(place-image SNAKE 150 250
(place-image SNAKE 50 250 BACKG))))
(define (render s)
(cond
[(empty? s) BACKG]
[else (render-snake-part (first s) (render (rest s)))]))
; SnakePart -> Image
; renders the snake s onto BACKG
(check-expect (render-snake-part SNAKE-PART-TOP-LEFT BACKG)
(place-image SNAKE 50 50 BACKG))
(check-expect (render-snake-part SNAKE-PART-CENTER BACKG)
(place-image SNAKE 250 250 BACKG))
(check-expect (render-snake-part SNAKE-PART-TOP-RIGHT BACKG)
(place-image SNAKE 450 50 BACKG))
(define (render-snake-part s bg)
(place-image SNAKE
(posn-x (snake-part-pos s)) (posn-y (snake-part-pos s))
bg))
; SnakePart -> SnakePart
; moves the snake in its direction
(check-expect (move-snake-part SNAKE-PART-BOTTOM-LEFT) (make-snake-part UP (make-posn 50 (- 450 SNAKE-STEP))))
(check-expect (move-snake-part SNAKE-PART-TOP) (make-snake-part DOWN (make-posn 250 (+ 50 SNAKE-STEP))))
(check-expect (move-snake-part SNAKE-PART-LEFT) (make-snake-part RIGHT (make-posn (+ 50 SNAKE-STEP ) 250)))
(check-expect (move-snake-part SNAKE-PART-TOP-RIGHT) (make-snake-part LEFT (make-posn (- 450 SNAKE-STEP) 50)))
(define (move-snake-part s)
(cond
[(equal? (snake-part-dir s) UP)
(make-snake-part UP (make-posn (posn-x (snake-part-pos s)) (- (posn-y (snake-part-pos s)) SNAKE-STEP)))]
[(equal? (snake-part-dir s) DOWN)
(make-snake-part DOWN (make-posn (posn-x (snake-part-pos s)) (+ (posn-y (snake-part-pos s)) SNAKE-STEP)))]
[(equal? (snake-part-dir s) LEFT)
(make-snake-part LEFT (make-posn (- (posn-x (snake-part-pos s)) SNAKE-STEP) (posn-y (snake-part-pos s))))]
[(equal? (snake-part-dir s) RIGHT)
(make-snake-part RIGHT (make-posn (+ (posn-x (snake-part-pos s)) SNAKE-STEP ) (posn-y (snake-part-pos s))))]))
; Snake -> Snake
; moves the whole snake by adding a moved part and removing the last part
(check-expect (tock SNAKE-MID-LEFT)
(list (make-snake-part "right" (make-posn 350 250))
(make-snake-part "right" (make-posn 250 250))
(make-snake-part "right" (make-posn 150 250))))
(define (tock s)
(cons (move-snake-part (first s)) (remove-last-part s)))
; NE-List -> NE-List
; removes the last element of the ne-list s
(define (remove-last-part s)
(cond
[(empty? (rest s)) '()]
[else (cons (first s) (remove-last-part (rest s)))]))
; SnakePart KeyEvent -> SnakePart
; changes the direction of the snake
(check-expect (key-h-snake-part SNAKE-PART-BOTTOM-LEFT "down") (make-snake-part DOWN (make-posn 50 450)))
(check-expect (key-h-snake-part SNAKE-PART-DOWN "up") (make-snake-part UP (make-posn 50 450)))
(check-expect (key-h-snake-part SNAKE-PART-RIGHT "left") (make-snake-part LEFT (make-posn 450 250)))
(check-expect (key-h-snake-part SNAKE-PART-LEFT "right") SNAKE-PART-LEFT)
(check-expect (key-h-snake-part SNAKE-PART-LEFT "w") SNAKE-PART-LEFT)
(define (key-h-snake-part s k)
(cond
[(key=? k "up") (make-snake-part UP (snake-part-pos s))]
[(key=? k "down") (make-snake-part DOWN (snake-part-pos s))]
[(key=? k "left") (make-snake-part LEFT (snake-part-pos s))]
[(key=? k "right") (make-snake-part RIGHT (snake-part-pos s))]
[else s]))
; Snake KeyEvent -> Snake
; changes the snake's direction according to the key pressed
(check-expect (key-h SNAKE-MID-LEFT "left")
(list (make-snake-part "left" (make-posn 250 250))
(make-snake-part "right" (make-posn 150 250))
(make-snake-part "right" (make-posn 50 250))))
(define (key-h s k)
(cons (key-h-snake-part (first s) k) (rest s)))
; SnakePart -> Boolean
; has the snake part hit the border?
(check-expect (face-hit-border? (make-snake-part "left" (make-posn -50 250))) #true)
(check-expect (face-hit-border? (make-snake-part "down" (make-posn 50 -1))) #true)
(check-expect (face-hit-border? (make-snake-part "right" (make-posn 501 250))) #true)
(check-expect (face-hit-border? (make-snake-part "right" (make-posn 400 550))) #true)
(check-expect (face-hit-border? (make-snake-part "down" (make-posn 450 450))) #false)
(check-expect (face-hit-border? (make-snake-part "up" (make-posn 250 250))) #false)
(check-expect (face-hit-border? (make-snake-part "left" (make-posn 451 451))) #true)
(define (face-hit-border? s)
(or (< (posn-x (snake-part-pos s)) (+ 0 SNAKE-RADIUS))
(< (posn-y (snake-part-pos s)) (+ 0 SNAKE-RADIUS))
(> (posn-x (snake-part-pos s)) (- SCENE-WIDTH SNAKE-RADIUS))
(> (posn-y (snake-part-pos s)) (- SCENE-HEIGHT SNAKE-RADIUS))))
; for making it eat itself
(define BIG-SNAKE
(list
(make-snake-part DOWN (make-posn 50 50))
(make-snake-part LEFT (make-posn 150 50))
(make-snake-part LEFT (make-posn 250 50))
(make-snake-part LEFT (make-posn 350 50))
(make-snake-part UP (make-posn 350 150))
(make-snake-part UP (make-posn 350 250))
(make-snake-part RIGHT (make-posn 250 250))
(make-snake-part RIGHT (make-posn 150 250))
(make-snake-part RIGHT (make-posn 50 250))))
; Snake -> Boolean
; has the face of the snake collided with any of its body parts?
(define (face-hit-body? f s)
(cond
[(empty? s) #false]
[else (or (too-close? f (first s))
(face-hit-body? f (rest s)))]))
; SnakePart SnakePart -> Boolean
; are the two snake parts (assuming same size) overlapping?
(check-expect (too-close? SNAKE-PART-CENTER (make-snake-part UP (make-posn 240 240))) #true)
(check-expect (too-close? SNAKE-PART-CENTER (make-snake-part UP (make-posn 150 150))) #false)
(define (too-close? s1 s2)
(< (distance (snake-part-pos s1) (snake-part-pos s2)) SNAKE-DIAMETER))
; Posn Posn -> Number
; distance between p1 and p2
(check-within (distance (make-posn 0 1) (make-posn 1 0)) (sqrt 2) 0.0001)
(check-within (distance (make-posn 1 1) (make-posn 1 1)) 0 0.0001)
(define (distance p1 p2)
(sqrt (+ (sqr (- (posn-x p1) (posn-x p2)))
(sqr (- (posn-y p1) (posn-y p2))))))
; Snake -> Boolean
; has the snake part hit the border?
(check-expect (end? SNAKE-MID-LEFT) #false)
(check-expect (end? (list (make-snake-part "right" (make-posn 451 250))
(make-snake-part "right" (make-posn 150 250))
(make-snake-part "right" (make-posn 50 250))))
#true)
(define (end? s)
(or (face-hit-border? (first s))
(face-hit-body? (first s) (rest s))))
; WS -> Image
; produces the last image from the WS
(check-expect (game-over SNAKE-MID-LEFT)
(overlay (text "game over" 20 "black")
(render SNAKE-MID-LEFT)))
(define (game-over ws)
(overlay (text "game over" 20 "black")
(render ws)))
; WorldState -> WorldState
; launches the program from some initial state
(define (main ws)
(big-bang ws
[on-tick tock 0.5]
[on-key key-h]
[to-draw render]
[stop-when end? game-over]))
#;
(main SNAKE-MID-LEFT)