[racket] Making animations in racket (or, why racket is hard to transition to from scratch)

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Wed Feb 2 07:54:15 EST 2011

On Feb 2, 2011, at 7:20 AM, I wrote:

> There are a number of other things that would simplify the code,  
> some of which would also make things (slightly) more efficient.
>
> Your sprite switches between its two images every 10 clock-ticks,  
> where a clock-tick defaults to 1/28 second.  Instead, just set the  
> tick interval to what you want, and skip the 0.1 and the rounding.
>
> (big-bang ...
>       (on-tick tick (/ 1 2.8))   ; or (on-tick tick 1/3) or (on- 
> tick tick .382) or whatever
>       ... )

On second thought, this may help more than I thought: every clock- 
tick requires redrawing the screen, so doing one tenth as many clock  
ticks should significantly reduce the delay and flicker.

> And since there are only two possible images in the world, I would  
> probably use a boolean or a pair of symbols or strings to choose  
> between them:
> ...
> (Symbols or booleans would be slightly more efficient, but I  
> introduce strings much earlier in my course than symbols or booleans.)

Obviously, you're using booleans already, so you could make this  
simpler and more efficient by saying

(define-struct world (pos mouse-pos fred?))
(define initial-world (make-world CENTER CENTER true))
...
(define (sprite-image w)
    (if (world-fred? w) pic1 pic2)))
(define (draw w)
    (place-image
       (rotate (angle-between (world-pos w) (world-mouse-pos w))
                    (sprite-image w))
       (posn-x (world-pos w)) (posn-y (world-pos w))
       BACKGROUND)))

(define (tick w)
    (make-world
       (world-pos w)
       (world-mouse-pos w)
       (not (world-fred? w))))


Stephen Bloch
sbloch at adelphi.edu



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110202/19ca9884/attachment.html>

Posted on the users mailing list.