-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-092.rkt
133 lines (102 loc) · 2.78 KB
/
ex-092.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
#lang htdp/bsl
(require 2htdp/image)
(require 2htdp/universe)
(define cham-image (bitmap "images/cham.png"))
(define BACKGROUND-WIDTH 300)
(define BACKGROUND-HEIGHT 180)
(define BACKGROUND (empty-scene BACKGROUND-WIDTH BACKGROUND-HEIGHT))
(define SPEED 3)
(define HAPPINESS-SPEED -1)
; A ChamColour is one of
; - "red"
; - "blue"
; - "green"
; interpretation: colour of the chameleon
; A Position is a Number in [0, 100]
; interpretation: 0 leftmost position 100 rightmost position
; A Happiness is a Number in [0, 100]
; interpretation: 0 minimum happiness, 100 maximum
(define-struct cham [pos colour happiness])
; VCham is (make-cham Position ChamColour Happiness)
; intepretation: state of the world defined by
; where is the chameleon, what colour it has and how happy it is
; VCham -> Image
(define (render-world ws)
(place-image
(get-cham-image ws)
(cham-pos ws)
(/ BACKGROUND-HEIGHT 2)
(place-image/align
(make-happiness-bar (cham-happiness ws))
0
BACKGROUND-HEIGHT
"left"
"bottom"
BACKGROUND)))
; VCham -> Image
; Returns an image that represents cham in the current state
(define (get-cham-image cham)
(overlay
(if (= (cham-happiness cham) 0)
(rotate 90 cham-image)
cham-image)
(rectangle
(image-width cham-image)
(image-height cham-image)
"solid"
(cham-colour cham))))
; VCham -> VCham
(define (tock cham)
(cond
[(unhappy? cham) cham]
[else
(make-cham
(modulo (+ (cham-pos cham) SPEED) BACKGROUND-WIDTH)
(cham-colour cham)
(+ (cham-happiness cham) HAPPINESS-SPEED))]))
; VCham -> Boolean
; Returns whether the cham is completely unhappy
(define (unhappy? cham)
(<= (cham-happiness cham) 0))
; VCham -> Boolean
; Returns whether the cham is completely happy
(define (fulfilled? cham)
(>= (cham-happiness cham) 100))
; Happiness -> Image
; Given a level of happiness, returns an image representing it
(define (make-happiness-bar h)
(rectangle
(* BACKGROUND-WIDTH (/ h 100))
10
"solid"
(if (>= h 20) "olivedrab" "red")))
; VCham KeyEvent -> VCham
(define (on-key-press cham key)
(cond
[(and (key=? key "down") (not (fulfilled? cham)))
(make-cham
(cham-pos cham)
(cham-colour cham)
(+ (cham-happiness cham) 2))]
[(key=? key "r")
(make-cham
(cham-pos cham)
"red"
(cham-happiness cham))]
[(key=? key "b")
(make-cham
(cham-pos cham)
"blue"
(cham-happiness cham))]
[(key=? key "g")
(make-cham
(cham-pos cham)
"green"
(cham-happiness cham))]
[else cham]))
(define (main cham)
(big-bang cham
[to-draw render-world]
[on-key on-key-press]
[on-tick tock]))
(main (make-cham 0 "green" 100))