-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex054.rkt
56 lines (48 loc) · 2.16 KB
/
ex054.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
;; 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-reader.ss" "lang")((modname ex054) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp")) #f)))
(define HEIGHT 300) ; distances in pixels
(define WIDTH 100)
(define YDELTA 3)
(define BACKG (empty-scene WIDTH HEIGHT))
(define ROCKET (rectangle 5 30 "solid" "red"))
(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 0)
(place-image ROCKET 10 (- CENTER) BACKG))
(check-expect (show 53)
(place-image ROCKET 10 (- 53 CENTER) BACKG))
(define (show x)
(cond
[(and (string? x) (string=? 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)]))
; LRCD KeyEvent -> LRCD
; starts the countdown when space bar is pressed,
; if the rocket is still resting
(define (launch x ke)
x)
; LRCD -> LRCD
; raises the rocket by YDELTA,
; if it is moving already
(define (fly x)
x)