s c h e m a t i c s : c o o k b o o k

/ NoParentSelected / Cookbook.GUIRecipeAnimation

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Animation

Problem

Displaying a window and animating its contents.

Solution

;;; Jumping PacMan

(define PAUSE 0.002)   ; determines the speed

;;; MODEL

;; Coordinate system

; (xmin,ymax) ... (xmax,ymax)
;    .                 .
;    .                 .
; (xmin,ymin) ... (xmax,ymin)


(define-values (xmin xmax) (values -1 1))
(define-values (ymin ymax) (values -1 1))

; At time t PacMan is at ( x(t), y(t) ).
(define (x t) (cos t))
(define (y t) (sin (* 3 t)))


;;; CONTROL

; model-coord->screen-coord : real real -> integer integer
(define (model-coord->screen-coord x y)
  ; convert the model coordinates to screen coordinates
  (values (floor (* (- WIDTH 50)  (/ (- x xmin) (- xmax xmin))))
          (floor (* (- HEIGHT 50) (/ (- y ymin) (- ymax ymin))))))

(define (control-loop)
  (let loop ([t (* -9 pi)])
    (update-view t)
    (sleep PAUSE)
    (loop (+ t .002))))


; VIEW

; The model is shown in a WIDTH x HEIGHT frame

; Dimensions
(define-values (WIDTH HEIGHT) (values 600 600))

; Instantiate frame and show it
(define frame (instantiate frame% 
                ("Jumping PacMan")))
(send frame show #t)

; Put a canvas inside the frame
(define my-canvas%
  (class canvas%
    (inherit get-dc)
    (define/override (on-paint)       
      (control-loop))
    (super-instantiate ())))

(define canvas (instantiate my-canvas% (frame) 
                 (min-width  WIDTH)
                 (min-height HEIGHT)))

; Make a PacMan bitmap by loading a gif file
(define pacman 
  (make-object bitmap% "pacman2.gif" 'gif))

; Update the view by drawing PacMan on his new position.
; Due to the white border around PacMan, we don't need
; to delete the old one, before drawing him again.
(define (update-view t)
  (let ([drawing-context (send canvas get-dc)])
    (let-values ([(sx sy) (model-coord->screen-coord (x t) (y t))])
      (send drawing-context draw-bitmap pacman sx sy 'solid))))

Discussion


Comments about this recipe

Contributors

-- JensAxelSoegaard - 14 May 2004

Attachments

Attachment: Action: Size: Date: Who: Comment:
PacMan2.GIF action 1375 14 May 2004 - 00:47 JensAxelSoegaard PacMan

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest