[racket] Manipulation in World teachpack

From: Alexander D. Knauth (alexander at knauth.org)
Date: Fri Apr 18 13:48:13 EDT 2014

On Apr 17, 2014, at 11:31 PM, Zee Ken <udaybhasker552009 at gmail.com> wrote:

> Where do I use the place-image option to place the myself, shark, food1, food2 images at required positions? Do I give that in the initial-world?
You use the on-redraw option to big-bang to tell big-bang what image to display in the window, and you supply some kind of render-world function like this:
(define (render-world w) ; w should be an instance of the world structure
  ...)

(on-redraw render-world)

By the way this has nothing to do with the initial-world, except that the first image it displays is the result of calling (render-world initial-world), but you don’t have to worry about that.  

> The define-struct has to be exactly as you showed right? Or do I have to give the positions by using the make-posn there?
The define-struct does not have to be exactly as I showed.  It could be anything that can represent where the objects in the program are. It doesn’t even have to be a structure.  It could be a list of the posns, or a string that contains that information, or even a number that you can somehow decode to get the posns, but a structure is more convenient (at least for this program).  

And you do not have to give the positions by using the make-posn there either.  Instead, you put the initial positions into big-bang like this:
(big-bang width height 1/30
          (make-world (make-posn 550 550)
                      (make-posn 200 250)
                      (make-posn 100 550)
                      (make-posn 550 400)))
Or probably something more like this would be better:
(define myself-initialposition (make-posn 550 550))
(define food1-initialposition  (make-posn 200 250))
(define food2-initialposition  (make-posn 100 550))
(define shark1-initialposition (make-posn 550 400))

(define initial-world
  (make-world myself-initialposition
              food1-initialposition
              food2-initialposition
              shark1-initialposition))

(big-bang width height 1/30 initial-world)

> How do I manipulate images if all I am entering are the positions? Where do I instantiate the place-image procedure?
You manipulate instances of the world structure, and then make a render-world function to translate those world structs into images for it to display in the window:
(define (render-world w) ; w should be an instance of the world structure
  ...) ; this should produce the image that it would display

(on-redraw render-world) ; this tells big-bang to use the render-world function make the image (maybe that’s      
                         ; what you were thinking of when you asked where you instantiate the place-image 
                         ; procedure)

By the way the render-world function should use the place-image function somewhere to generate the image, but you don’t instantiate the place-image procedure or anything like that.  


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

Posted on the users mailing list.