(require 2htdp/image) (require 2htdp/universe) ; CS 111 - Week 4 Labs - 1:00 lab ; Sharon Tuttle ; last modified: 2016-09-16 (define WIDTH 300) (define HEIGHT 200) (define BACKDROP (empty-scene WIDTH HEIGHT)) ;======== ; another big-bang clause: ; on-key ; * this gives us a way to specify how the ; current world should change when a key is ; typed ; * an on-key clause expects the name of a function ; as its argument, ; a function that expects the current world ; and a string representing a keystroke ; as its arguments, and returns the new value ; of the world that should result ; ; when big-bang is running, ; if the user types a key, ; big-bang calls on-key's function ; with the current world and a string that is ; based on the key typed, ; then it calls to-draw with the resulting new world ; value (I think) ;======== ; proof of concept: can I have a world that is ; JUST driven by keystrokes, ; even without on-tick? ;-------- ; signature: change-world-string: string string -> string ; purpose: expects a world string and a string ; representing a keystroke, ; and returns that keystroke string as the ; new world value (define (change-world-string curr-world key-typed) key-typed ) (check-expect (change-world-string "moo" "k") "k") (check-expect (change-world-string "steve" "g") "g") ;======== ; FROM W4-2 examples: ;-------- ; signature: draw-string-world: string -> scene ; purpose: expects a world-string, and returns ; a scene with that world-string in its center, ; in blue 40-point letters ;(define (draw-string-world world-string) ; ... ;) (check-expect (draw-string-world "Hi!") (place-image (text "Hi!" 40 "blue") (/ WIDTH 2) (/ HEIGHT 2) BACKDROP)) (check-expect (draw-string-world "Lo!") (place-image (text "Lo!" 40 "blue") (/ WIDTH 2) (/ HEIGHT 2) BACKDROP)) (define (draw-string-world world-string) (place-image (text world-string 40 "blue") (/ WIDTH 2) (/ HEIGHT 2) BACKDROP) ) ;======== end of part from W4-2 examples ; what if I now try draw-string-world and change-string-world ; with big-bang? (big-bang "platypus" (to-draw draw-string-world) (on-key change-world-string)) ;======== ; another example, now with enumeration data! ; say I'd like to affect the purple star's size ; in our number-based world from Week 3 Lecture 2 ;======== ; FROM W3-2 examples: ;-------- ;============ ; signature: purple-star: number -> image ; purpose: expects the desire distance in pixels ; between points of a 5-pointed star, ; and returns a solid purple star image ; of that size (check-expect (purple-star 15) (star 15 "solid" "purple")) (check-expect (purple-star 30) (star 30 "solid" "purple")) (define (purple-star size) (star size "solid" "purple") ) ;-------- ; signature: purple-star-scene: number -> scene ; purpose: expects a star-size, ; and returns a scene of a centered purple star of ; that size ; (in a scene of size WIDTH and HEIGHT, ; that already has a little circle, star, and triangle in it) ;(define (purple-star-scene star-size) ; ... ;) (check-expect (purple-star-scene 25) (place-image (purple-star 25) (/ WIDTH 2) (/ HEIGHT 2) BACKDROP)) (check-expect (purple-star-scene 160) (place-image (purple-star 160) (/ WIDTH 2) (/ HEIGHT 2) BACKDROP)) (define (purple-star-scene star-size) (place-image (purple-star star-size) (/ WIDTH 2) (/ HEIGHT 2) BACKDROP) ) ;======== end of part from W3-2 examples ; I decide I'd like to change the size of the star ; based on some of the arrow keys ;========= ; DATA DEFINITION: ; an ArrowKey is one of: ; "up", "down", "left", "right" (define SIZE-CHANGE 35) ;-------- ; signature: change-star-size: number ArrowKey -> number ; purpose: expects a world number and an arrow key, ; and if the arrow key is "left", it returns ; the world number decreased by SIZE-CHANGE, ; and if the arrow key is "right", it returns ; the world number increased by SIZE-CHANGE, ; and for anything else, it returns the current ; world number ;(define (change-star-size world-num an-arrow-key) ; ... ;) (check-expect (change-star-size 150 "left") (- 150 SIZE-CHANGE)) (check-expect (change-star-size 135 "right") (+ 135 SIZE-CHANGE)) (check-expect (change-star-size 60 "up") 60) (check-expect (change-star-size 600 "down") 600) (check-expect (change-star-size 6 "m") 6) ;(define (change-star-size world-num an-arrow-key) ; (cond ; [... ...] ; [... ...] ; [... ...] ; [... ...] ; [... ...] ; ) ;) ;(define (change-star-size world-num an-arrow-key) ; (cond ; [(string=? an-arrow-key "left") ; (- world-num SIZE-CHANGE)] ; [(string=? an-arrow-key "right") ; (+ world-num SIZE-CHANGE)] ; [(string=? an-arrow-key "up") world-num] ; [(string=? an-arrow-key "down") world-num] ; [else world-num] ; ) ;) ; REFACTORING - when you IMPROVE already-working code! ; (note that a GOOD set of tests helps you make ; sure you don't break the code when refactoring...) (define (change-star-size world-num an-arrow-key) (cond [(string=? an-arrow-key "left") (- world-num SIZE-CHANGE)] [(string=? an-arrow-key "right") (+ world-num SIZE-CHANGE)] [else world-num] ) ) (big-bang 0 (to-draw purple-star-scene) (on-tick add1) (on-key change-star-size))