-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_world.rkt
104 lines (86 loc) · 2.92 KB
/
counter_world.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
;; 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 counter_world) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
(require 2htdp/image)
(require 2htdp/universe)
;; Counter World. Counter starts from 10 (or any starter number entered to Main)
;; and goes down till 0
;; =================
;; Constants:
(define WIDTH 180)
(define HEIGHT 120)
(define MTS (empty-scene WIDTH HEIGHT))
(define CTR-Y (/ HEIGHT 2)) ; CTR stands for centre
(define CTR-X (/ WIDTH 2))
(define FS 96) ; default font size
(define FC "blue") ; default font colour
(define STEP 1) ; how much smaller is the next counter step
(define SN 0) ; stop number
(define RN 10) ; add this number to current status of the counter
;; =================
;; Data definitions:
;; Counter is number
;; Interp. the number that shows the current status of the countdown
(define C1 0) ; to test one-digit numbers
(define C2 10) ; to test two-digit numbers
(define C3 100) ; to test three-digit numbers
#;
(define (fn-for-counter_world c)
(... c))
;; =================
;; Functions:
;; Counter -> Counter
;; start the world with ...
;;
(define (main c)
(big-bang c ; Counter
(on-tick countdown 1) ; Counter -> Counter
(to-draw render) ; Counter -> Image
(stop-when has-run-down?) ; Counter -> Boolean
(on-mouse click-mouse) ; Counter Integer Integer MouseEvent -> Counter
(on-key handle-key) ; Counter KeyEvent -> Counter
)
)
;; Counter -> Counter
;; produce the next ...
(check-expect (countdown 10) 9)
(check-expect (countdown 1) 0)
(check-expect (countdown SN) SN)
(define (countdown c)
(if (> c SN) (- c 1) SN)
)
;; Counter -> Image
;; render the number as an image
(check-expect (render 10) (place-image (text (number->string 10) FS FC) CTR-X CTR-Y MTS))
(define (render c)
(place-image
(text
(number->string c) FS FC) CTR-X CTR-Y MTS)
)
;; Number -> Boolean
;; shows if the countdown has reached the stop condition
(check-expect (has-run-down? (+ SN 10)) #f)
(check-expect (has-run-down? SN) #t)
(define (has-run-down? c)
(<= c SN)
)
;; Counter KeyEvent -> Counter
;; when space is pressed, counter resets to zero
(check-expect (handle-key 10 " ") SN)
(check-expect (handle-key 10 "a") 10)
(check-expect (handle-key SN " ") SN)
(check-expect (handle-key SN "a") SN)
(check-expect (handle-key SN "r") (+ SN RN))
(define (handle-key c kevt)
(cond [(key=? " " kevt) SN]
[(key=? "r" kevt) (+ c RN)]
[else c]
)
)
;; Counter Integer Integer MouseEvent -> Counter
;; when mouse is clicked, the
(define (click-mouse c x y me)
(cond [(mouse=? me "button-down") SN]
[else c]
)
)