How can i specify end of big bang using distance? (racket)

133 Views Asked by At

hi all i am working through the hdtp book,currently creating a simple animation which a moves car from left to right at a rate of three pixels 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 which passed sideways to the right .i have used this code from a previous chapter.

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

this checks wether the car which animated that moves from the left to the right has passed/dissapeared past the right hand side and which it should return true and abort the program

e,g the background image has a width of 200 ,depending on which state the car spawns at it should caulcate the distance between the start state and the number of pixels from left to right

(define (end? cw)
(> cw car-end))

this code is what i could think but im trying to find a predicate which i can use to dermine when the car gone past the right hand side
1

There are 1 best solutions below

0
On

You can introduce some variable for width of your image:

(require 2htdp/image)
(require 2htdp/batch-io)
(require 2htdp/universe)

(define scene-width 400)

Then create background of this width (tree + rectangle):

(define background (place-image (underlay/xy (circle 10 "solid" "darkgreen") 9 15
                                             (rectangle 2 20 "solid" "brown"))
                                50 15
                                (rectangle scene-width 200 "solid" "green")))

Use these definitions for car (no functional change):

(define wheel-radius 5)

(define wheel
  (circle wheel-radius "solid" "black"))

(define space
  (rectangle 20 wheel-radius (* wheel-radius 5) "white"))

(define red-car (above (rectangle 40 20 "solid" "red")
                       (beside wheel space wheel)))

In the main's clause stop-when, you need to compare car-x (state) with scene-width. Function looks like this (lambda (car-x) (> car-x scene-width)), but I increased scene-width to completely hide the car:

(define (main car-x)
  (big-bang
      car-x
    [on-tick
     (lambda (car-x) (+ car-x 3))]
    [to-draw
     (lambda (car-x) (place-image red-car car-x 10 background))]
    [stop-when
     (lambda (car-x) (> car-x 
                        (+ scene-width
                           (/ (image-width red-car) 2))))]))

After compilation, call (main 0).