[racket] Could this be done another way

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Mar 12 10:37:01 EDT 2012

#lang racket

(require 2htdp/image 2htdp/universe)

; main : CarState -> CarState
; launch the program from some initial state
(define (main r)
   (big-bang 0 (on-tick tock r) (to-draw render)))

(define wheel-radius 5)
(define cab
  (beside (rectangle (* 6 wheel-radius)(* 4 wheel-radius) "solid" "red") 
          (right-triangle (* 2 wheel-radius) (* 4 wheel-radius) "solid" "red")))
(define body
  (rectangle (* 20 wheel-radius) (* 6 wheel-radius) "solid" "red"))
(define top
  (above/align "right" cab body))
(define tire
  (circle wheel-radius "solid" "black"))
(define bot
  (beside tire
          (rectangle (* 5 wheel-radius) (* 2 wheel-radius) "solid" "white")
          tire))
(define auto
  (above top bot))

(define WIDTH 1000)
(define SPEED (* 0.54 (image-width auto)))
(define Y-CAR 50)
(define BACKGROUND (empty-scene WIDTH 200))


; CarState -> CarState
; the clock ticked; move the car by three pixels
; example:
; given: 20, expected 23
; given: 78, expected 81
(define (tock ws) 
  (modulo (+ ws SPEED) WIDTH)
  #;
  (if (< ws (-(image-width BACKGROUND) (* 0.54 (image-width auto))))
      (+ ws 3)
      ( - ws (image-width BACKGROUND))))

; PositiveNumber -> Image
; run the animation at the specified rate [1/28 is decent, try 1/100 too]
(define (render ws)
  (place-image auto ws Y-CAR BACKGROUND))


On Mar 12, 2012, at 10:24 AM, ROELOF WOBBEN wrote:

> Hello, 
> 
> I have tried to make a function which display a car running on a empty-screen.
> When the car  is at the end. The car has to begin at the beginning.
> 
> I have made this : 
> 
> (define wheel-radius 5)
> (define cab (beside (rectangle (* 6 wheel-radius)(* 4 wheel-radius) "solid" "red")(right-triangle (* 2 wheel-radius) (* 4 wheel-radius) "solid" "red")))
> (define body (rectangle (* 20 wheel-radius) (* 6 wheel-radius) "solid" "red"))
> (define top (above/align "right" cab body))
> (define tire (circle wheel-radius "solid" "black"))
> (define bot (beside tire (rectangle (* 5 wheel-radius) (* 2 wheel-radius) "solid" "white") tire))
> (define auto (above top bot))
> 
> (define Y-CAR 50)
> (define BACKGROUND (empty-scene 1000 200))
> 
> ; CarState -> CarState
> ; the clock ticked; move the car by three pixels
> ; example:
> ; given: 20, expected 23
> ; given: 78, expected 81
> (define (tock ws) 
>   (if (< ws (-(image-width BACKGROUND)(* 0.54  (image-width auto))))(+ ws 3) ( - ws (image-width BACKGROUND))))
> 
> ; CarState -> Image
> ; place the car into a scene, according to the given world state
> (define (render ws)
>   (place-image auto ws Y-CAR BACKGROUND))
> 
> ; main : CarState -> CarState
> ; launch the program from some initial state
> (define (main ws)
>    (big-bang ws (on-tick tock) (to-draw render)))
> 
> But now I wonder if I can do the same with setting the variable ws to zero.
> So like this : If ws is smaller then the image-width increase with the half width of the car. then ws = ws+ 3 else ws = 0 
> 
> Regards,
> 
> Roelof
> 
> 
> 
> 
> 
>                     
> 
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120312/aa04b44a/attachment-0001.html>

Posted on the users mailing list.