-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtDP Ex 57.rkt
127 lines (101 loc) · 4.92 KB
/
HtDP Ex 57.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
;; 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-abbr-reader.ss" "lang")((modname |HtDP Ex 57|) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp")) #f)))
; 7 Jan 21: Countdown works fine on spacebar, then images all disappear.
#|
Exercise 57. Recall that the word “height” forced us to choose one of two possible interpretations.
Now that you have solved the exercises in this section, solve them again using the first interpretation of the word.
Compare and contrast the solutions.
"While the interpretation of "resting" is obvious, the interpretation of numbers is ambiguous in its notion of height:
1. the word “height” could refer to the distance between the ground and the rocket’s point of reference, say, its center; or
2. it could mean the distance between the top of the canvas and the reference point."
|#
; if height refers to dist bet ground (top of canvas) and center of rocket, then height = canvasHeight - y-coordinate
(define WIDTH 200)
(define CANVAS-HEIGHT 300)
(define YDELTA 3) ;speed of rocket
(define MTSCN (empty-scene WIDTH CANVAS-HEIGHT))
(define ROCKET (rectangle 5 20 "solid" "red"))
(define CENTER (/ (image-height ROCKET) 2))
; A LRCD (Launch Rocket Count Down) consists of
; - "resting"
;- "A number from -3 to -1
; Non-negative number
;interpretation ->
; grounded rocket in countdown mode
; Number represents canvasHeight- ycoordinate
; PJ note - rocket goes from 0 to CANVAS-HEIGHT
; given: , expect:
(check-expect (rocket-height 10) (- CANVAS-HEIGHT 10))
(check-expect (rocket-height 100) (- CANVAS-HEIGHT 100))
(define (rocket-height y) ;calculates the rocket height's y coord
(- CANVAS-HEIGHT y))
;Wishlist of functions:
; render function, to show image as resting or flying rocket
; LRCD -> Image
; given y = "resting", expect: (place-image ROCKET (/ WIDTH 2) HEIGHT MTSCN)
; given y = -2, expect: (place-image(text "-2" 20 "red") 10 (* 3/4 WIDTH)(place-image ROCKET (/ WIDTH 2) HEIGHT MTSCN)
; given: y = 53 , expect: (place-image ROCKET (/ WIDTH 2) 53 MTSCN)
(check-expect (draw "resting") (place-image ROCKET (/ WIDTH 2) (- (rocket-height 0) CENTER) MTSCN))
(check-expect (draw -2) (place-image(text "-2" 20 "red") 10 (* 3/4 WIDTH)(place-image ROCKET (/ WIDTH 2) (- (rocket-height 0) CENTER) MTSCN)))
(check-expect (draw 53) (place-image ROCKET (/ WIDTH 2) (- (rocket-height 53) CENTER) MTSCN))
(define (draw y)
(cond
[(string? y)
(place-rocket (rocket-height 0))] ;replaced y with rocket-height
[(<= -3 y -1 )
(place-image(text (number->string y) 20 "red") 10 (* 3/4 WIDTH)(place-rocket (rocket-height 0)))];replaced y with rocket-height
[(>= y 0)
(place-rocket (rocket-height y))]));replaced y with rocket-height
(define (place-rocket y)
(place-image ROCKET (/ WIDTH 2) (- y CENTER) MTSCN))
; key event handler, to trigger count-down [PJ note: additional condition in book "if rocket still resting"]
; LRCD ke -> number
; given: , expect:
;given: press any key, expect: remain at -3
;given: press spacebar, expect: -3
; given:
(check-expect (launch "resting" " ") -3)
(check-expect (launch "resting" "a") "resting")
(check-expect (launch -3 " ") -3)
(check-expect (launch -1 " ") -1)
(check-expect (launch 33 " ") 33)
(check-expect (launch 33 "a") 33)
(define (launch y ke)
(cond
[(string? y) (if (string=? " " ke) -3 y)]
[(<= -3 y -1) y]
[(>= y 0) y]))
; tick, to move image by YDELTA ticks i.e. for rocket to fly
; LRCD -> LRCD
; raises the rocket by YDELTA if it is moving already
(check-expect (fly "resting") "resting")
(check-expect (fly -3) -2)
(check-expect (fly -2) -1)
(check-expect (fly -1) 0)
(check-expect (fly 10) (+ 10 YDELTA))
(check-expect (fly 22) (+ 22 YDELTA))
(define (fly x)
(cond
[(string? x) x]
[(<= -3 x -1) (if (= x -1) 0 (+ x 1))] ; countdown
[(>= x 0) (+ x YDELTA)])) ;sign change to account for different definition of height
;LRCD -> LRCD
; interpretation end state of world
(check-expect (end? "resting") #false)
(check-expect (end? -3) #false)
(check-expect (end? 100) #false)
(check-expect (end? (+ CANVAS-HEIGHT CENTER)) #true)
(define (end? y)
(cond
[(string? y) #false]
[(<= -3 y -1) #false]
[(>= (- y CENTER) CANVAS-HEIGHT) #true] ;change from previous code given new height definition.
[(< (- y CENTER) CANVAS-HEIGHT) #false]))
; LRCD -> LRCD
(define (main3 s) ;s needs to be "resting" in order for program to run properly.
(big-bang s
[on-tick fly] ;control tick rate for troubleshooting purpose
[to-draw draw]
[on-key launch]
[stop-when end?]))