-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-106.rkt
230 lines (182 loc) · 4.38 KB
/
ex-106.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
#lang htdp/bsl
(require 2htdp/image)
(require 2htdp/universe)
(require test-engine/racket-tests)
(define BACKGROUND-WIDTH 300)
(define BACKGROUND-HEIGHT 180)
(define BACKGROUND (empty-scene BACKGROUND-WIDTH BACKGROUND-HEIGHT))
(define SPEED 3)
(define HAPPINESS-SPEED -1)
(define HAPPINESS-FOOD 2)
(define HAPPINESS-PET 5)
(define MIN-HAPP 0)
(define MAX-HAPP 100)
; Happiness is a Number in [0, 100]
; interpretation: 0 absolute misery, 100 utter bliss
; Position is a Number
; interpretation: position in the x-coordinate
; Direction is one of
; - "left"
; - "right"
; An VCat is a structure (make-cat Position Direction Happiness)
; interpretation: x is the Position of the cat, d its Direction and h its Happiness
(define-struct cat [x d h])
(define cat-image (bitmap "images/cat.png"))
; Colour is one of
; - "r"
; - "b"
; - "g"
; VCham is a structure (make-cham Position Colour Happiness)
; intepretation: state of the world defined by
; where is the chameleon, what colour it has and how happy it is
(define-struct cham [x c h])
(define cham-image (bitmap "images/cham.png"))
; A VAnimal is one of
; - a VCat
; - a VCham
; VAnimal -> Image
(define (render-world a)
(cond
[(cat? a) (render-cat a BACKGROUND)]
[(cham? a) (render-cham a BACKGROUND)]
))
; VCat Image -> Image
(define (render-cat c img)
(place-image
cat-image
(cat-x c)
(/ BACKGROUND-HEIGHT 2)
(make-happiness-bar (cat-h c) img)
))
; VCham Image -> Image
(define (render-cham c img)
(place-image
cham-image
(cham-x c)
(/ BACKGROUND-HEIGHT 2)
(place-image
(rectangle
(image-width cham-image)
(image-height cham-image)
"solid"
(normalize-colour (cham-c c))
)
(cham-x c)
(/ BACKGROUND-HEIGHT 2)
(make-happiness-bar (cham-h c) img)
)))
; Colour -> image-colour?
; converts an item of the Colour enumeration into a image-colour?
(define (normalize-colour c)
(cond
[(string=? c "r") "red"]
[(string=? c "g") "green"]
[(string=? c "b") "blue"]
))
; VAnimal -> VAnimal
(define (tock a)
(cond
[(cat? a) (tock-cat a)]
[(cham? a) (tock-cham a)]
))
(define (tock-cat c)
(make-cat
(if
(> (cat-h c) 0)
(cond
[(string=? (cat-d c) "right") (+ (cat-x c) SPEED)]
[(string=? (cat-d c) "left") (- (cat-x c) SPEED)]
)
(cat-x c)
)
(cond
[(<= (cat-x c) 0) "right"]
[(>= (cat-x c) BACKGROUND-WIDTH) "left"]
[else (cat-d c)]
)
(happiness-sum (cat-h c) HAPPINESS-SPEED)
))
(define (tock-cham c)
(make-cham
(modulo
(+
(cham-x c)
(if (> (cham-h c) 0) SPEED 0)
)
BACKGROUND-WIDTH)
(cham-c c)
(happiness-sum (cham-h c) HAPPINESS-SPEED)
))
(define (happiness-sum a b)
(cap-num
MIN-HAPP
MAX-HAPP
(+ a b)
))
; VAnimal -> VAnimal
(define (key-handler a ke)
(cond
[(cat? a) (cat-key-handler a ke)]
[(cham? a) (cham-key-handler a ke)]
))
(define (cat-key-handler c ke)
(make-cat
(cat-x c)
(cat-d c)
(+
(cat-h c)
(cond
[(key=? ke "down") HAPPINESS-FOOD]
[(key=? ke "up") HAPPINESS-PET]
[else 0]
))))
(define (cham-key-handler c ke)
(cond
[(key=? ke "down")
(make-cham
(cham-x c)
(cham-c c)
(happiness-sum (cham-h c) HAPPINESS-FOOD)
)]
[(or (key=? ke "r") (key=? ke "b") (key=? ke "g"))
(make-cham (cham-x c) ke (cham-h c))]
[else c]))
; Number Number Number -> Number
; given a min, a max and a number, it returns the number capped between
; the min and the max
(check-expect (cap-num 0 10 10) 10)
(check-expect (cap-num 0 10 12) 10)
(check-expect (cap-num 0 10 -4) 0)
(check-expect (cap-num 0 10 3) 3)
(define (cap-num min max num)
(cond
[(< num min) min]
[(> num max) max]
[else num]
))
; Happiness Image -> Image
; Given a level of happiness and an image, renders a gauge on that image
(define (make-happiness-bar h img)
(place-image/align
(rectangle
(* (image-width img) (/ h 100))
10
"solid"
(if (>= h 20) "olivedrab" "red")
)
0
BACKGROUND-HEIGHT
"left"
"bottom"
img
))
(define (main a)
(big-bang
a
[to-draw render-world]
[on-key key-handler]
[on-tick tock]
))
(test)
(main (make-cham 0 "g" 100))
(main (make-cat 100 "right" 100))