hi all i am working through the hdtp book,currenyly creating a simple animation which a moves car from left to right at a rate of three pixles per clock tick.i am not sure but i have tried to but not sure how to piece it all together.my objective is to end the program when the car dissapears out of the background image.i have used this code from a previous chapter
(define car-end
(image-width CAR))
;WorldState -> Boolean
; after each event, big-bang evaluates (end? cw)
;stops program when the car touches the right hand side
(define (end? cw)
(> cw car-end))
this is all i could come to ,how can i find the distance from when i run the main program depending on the input for the number pixels,the program shuts down.
here is my full code
(require 2htdp/image)
(require 2htdp/batch-io)
(require 2htdp/universe)
;physical constants
(define WIDTH-OF-WORLD 20)
(define WHEEL-RADIUS 5)
(define WHEEL-DISTANCE (* WHEEL-RADIUS 5))
(define Y-TREE 15)
(define X-CAR 20)
(define TREE
(underlay/xy (circle 10 "solid" "green")
9 15
(rectangle 2 20 "solid" "brown")))
(define BACKGROUND (rectangle 400 200 "solid" "green"))
(define background1 (place-image TREE 50 Y-TREE BACKGROUND))
(define body (rectangle 40 20 "solid" "red"))
(define Y-CAR 10) ;this chnages how high or low the car is
;grpahical constants
(define WHEEL
(circle WHEEL-RADIUS "solid" "black"))
(define SPACE
(rectangle WIDTH-OF-WORLD WHEEL-RADIUS WHEEL-DISTANCE "white"))
(define BOTH-WHEELS
(beside WHEEL SPACE WHEEL))
(define CAR (above (rectangle 40 20 "solid" "red") (beside WHEEL SPACE WHEEL)))
;A WorldState is a Number.
; interpretation the number of pixels between
; the left border of the scene and the car
; WorldState -> WorldState
; launches the program from some initial state
(define (main ws)
(big-bang ws
[on-tick tock]
[to-draw render]
[stop-when end?]))
; WorldState -> WorldState
; moves the car by 3 pixels for every clock tick
; examples:
; given: 20, expect 23
; given: 78, expect 81
(define (tock cw)
(+ cw 3))
(define car-end
(image-width CAR))
; WorldState -> Image
; places the car into the BACKGROUND scene,
; according to the given world state
(define (render cw)
(place-image CAR cw Y-CAR background1))
;WorldState -> Boolean
; after each event, big-bang evaluates (end? cw)
;stops program when the car touches the right hand side
(define (end? cw)
(> cw car-end))