-
Notifications
You must be signed in to change notification settings - Fork 0
/
055.rkt
122 lines (99 loc) · 3.25 KB
/
055.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
#lang htdp/bsl
; Exercise 55.
; ------------
;
; (define (show x)
; (cond
; [(string? x)
; (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)]
; [(<= -3 x -1)
; (place-image (text (number->string x) 20 "red")
; 10 (* 3/4 WIDTH)
; (place-image ROCKET
; 10 (- HEIGHT CENTER)
; BACKG))]
; [(>= x 0)
; (place-image ROCKET 10 (- x CENTER) BACKG)]))
;
; Take another look at `show`. It contains three instances of an expression with
; the approximate shape:
;
; (place-image ROCKET 10 (- ... CENTER) BACKG)
;
; This expression appears three times in the function: twice to draw a resting
; rocket and once to draw a flying rocket. Define an auxiliary function that
; performs this work and thus shorten show. Why is this a good idea? You may
; wish to reread Prologue: How to Program.
; ------------------------------------------------------------------------------
(require 2htdp/image)
(require 2htdp/universe)
#;
(define ROCKET (bitmap "rocket.png"))
; dimensions of the canvas
(define HEIGHT 300) ; distances in pixels
(define WIDTH 100)
; how fast the rocket moves (along y-axis)
(define YDELTA 3)
(define BACKG (empty-scene WIDTH HEIGHT))
(define ROCKET (rectangle 5 30 "solid" "red"))
; "computed" center of the rocket (vertically)
(define CENTER (/ (image-height ROCKET) 2))
; An LRCD (for launching rocket countdown) is one of:
; – "resting"
; – a Number between -3 and -1
; – a NonnegativeNumber
; interpretation a grounded rocket, in countdown mode,
; a number denotes the number of pixels between the
; top of the canvas and the rocket (its height)
; LRCD -> Image
; renders the state as a resting or flying rocket
(check-expect
(show "resting")
(place-image ROCKET 10 (- HEIGHT CENTER) BACKG))
(check-expect
(show -2)
(place-image (text "-2" 20 "red")
10 (* 3/4 WIDTH)
(place-image ROCKET
10 (- HEIGHT CENTER)
BACKG)))
(check-expect
(show 53)
(place-image ROCKET 10 (- 53 CENTER) BACKG))
(check-expect
(show HEIGHT)
(place-image ROCKET 10 (- HEIGHT CENTER) BACKG))
(check-expect
(show HEIGHT)
(place-image ROCKET 10 (- HEIGHT CENTER) BACKG))
(check-expect
(show 0)
(place-image ROCKET 10 (- 0 CENTER) BACKG))
#;; old definition
(define (show x)
(cond
[(equal? x "resting")
(place-image ROCKET 10 (- HEIGHT CENTER) BACKG)]
[(<= -3 x -1)
(place-image (text (number->string x) 20 "red")
10 (* 3/4 WIDTH)
(place-image ROCKET
10 (- HEIGHT CENTER)
BACKG))]
[(>= x 0)
(place-image ROCKET 10 (- x CENTER) BACKG)]))
; new
(define (show x)
(cond
[(equal? x "resting") (place-rocket-bottom HEIGHT)]
[(<= -3 x -1)
(place-image (text (number->string x) 20 "red")
10 (* 3/4 WIDTH)
(place-rocket-bottom HEIGHT))]
[(>= x 0) (place-rocket-bottom x)]))
; Number -> Image
; places the bottom of the rocket at y in scn
(check-expect (place-rocket-bottom 20)
(place-image ROCKET 10 (- 20 CENTER) BACKG))
(define (place-rocket-bottom y)
(place-image ROCKET 10 (- y CENTER) BACKG))