-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-090.rkt
133 lines (100 loc) · 2.95 KB
/
ex-090.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 cat-image (bitmap "images/cat.png"))
(define BACKGROUND-WIDTH 300)
(define BACKGROUND-HEIGHT 180)
(define BACKGROUND (empty-scene BACKGROUND-WIDTH BACKGROUND-HEIGHT))
(define CAT-SPEED 3)
(define HAPPINESS-SPEED -1)
; A Direction is one of
; - "right"
; - "left"
; interpretation: direction to which the cat is moving
; 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 cat [pos direction happiness])
; Cat is (make-cat Position Direction Happiness)
; intepretation: state of the world defined by
; where is the cat, what direction is following, and how happy it is
; Cat -> Image
(define (render-world ws)
(place-image
(get-cat-image ws)
(cat-pos ws)
(/ BACKGROUND-HEIGHT 2)
(place-image/align
(make-happiness-bar (cat-happiness ws))
0
BACKGROUND-HEIGHT
"left"
"bottom"
BACKGROUND)))
; Cat -> Image
; Returns an image that represents cat in the current state
(define (get-cat-image cat)
(if (= (cat-happiness cat) 0)
(rotate 90 cat-image)
cat-image))
; Cat -> Cat
(define (tock cat)
(cond
[(unhappy? cat) cat]
[(exiting-right? cat) (make-cat BACKGROUND-WIDTH "left" (cat-happiness cat))]
[(exiting-left? cat) (make-cat 0 "right" (cat-happiness cat))]
[else
(make-cat
(if (going-right? cat)
(+ (cat-pos cat) CAT-SPEED)
(- (cat-pos cat) CAT-SPEED))
(cat-direction cat)
(+ (cat-happiness cat) HAPPINESS-SPEED))]))
; Cat -> Boolean
; Returns whether the cat is completely unhappy
(define (unhappy? cat)
(<= (cat-happiness cat) 0))
; Cat -> Boolean
; Returns whether the cat is completely happy
(define (fulfilled? cat)
(>= (cat-happiness cat) 100))
; Cat -> Boolean
; Whether the cat has reached the righmost point of the scenario
(define (exiting-right? cat)
(> (cat-pos cat) BACKGROUND-WIDTH))
; Cat -> Boolean
; Whether the cat has reached the leftmost point of the scenario
(define (exiting-left? cat)
(< (cat-pos cat) 0))
; Cat -> Boolean
; Whether the cat is moving to the right direction
(define (going-right? cat)
(string=? (cat-direction cat) "right"))
; 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")))
; Cat KeyEvent -> Cat
(define (on-key-press cat key)
(if
(fulfilled? cat) cat
(make-cat
(cat-pos cat)
(cat-direction cat)
(+
(cat-happiness cat)
(cond
[(string=? key "up") 5]
[(string=? key "down") 3]
[else 0])))))
(define (main cat)
(big-bang cat
[to-draw render-world]
[on-key on-key-press]
[on-tick tock]))
(main (make-cat 0 "right" 100))