-
Notifications
You must be signed in to change notification settings - Fork 0
/
47.rkt~
executable file
·45 lines (37 loc) · 1.45 KB
/
47.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
;; 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 |47|) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp")) #f)))
(require 2htdp/universe)
(define BACKGROUND-WIDTH 800)
(define BACKGROUND-HEIGHT 50)
(define BACKGROUND
(rectangle (+ BACKGROUND-WIDTH 5) (+ BACKGROUND-HEIGHT 5) "solid" "black"))
; An WorldState is a Number
; interpretation happines level
; WorldState -> WorldState
; decreases cw by 0.1 per tick
(define (clock-tick-handler cw) (if (> cw 100) 100 (- cw 0.1)))
; WorldState -> Imagej
; renders image of happines gauge
(define (render cw)
(rectangle cw BACKGROUND-HEIGHT "solid" "red"))
; WorldState -> Boolean
; evaluates after each event
(define (end? cw)(<= cw 0))
; WorldState, String -> WorldState
; changes cw for pressed keys
(define (keyboard-handler cw a-key)
(cond
[(key=? a-key "up") (+ cw (* cw 1/3))]
[(key=? a-key "down") (- cw (* cw 1/5))]
[else cw]))
; WorldState -> WorldState
; launches the program from some initial state
(define (main ws)
(big-bang ws
[to-draw render]
[on-tick clock-tick-handler]
[on-key keyboard-handler]
[stop-when end?]))
; start with the maximum number of happines
(main 100)