listrecursionracketracket-student-languages

DrRacket - render every structure in a list


(define SCENE (empty-scene 500 500))
(define-struct pos (x y))
(define-struct ball (img pos))

;(define (render lst))



(define game (list
               (make-ball (circle 10 "solid" "blue") (make-pos 250 250))
               (make-ball (circle 10 "solid" "red") (make-pos 150 150))))

(big-bang game
  [to-draw render])

Lets say i just want to render all my ball-struct-elements in the list on the SCENE. How can i do this? I know how to do it without a list, but with a list i got some problems.

Im using the beginner student version of racket.

I am thankful for every answer

I tried it recursive but just cant work it out


Solution

  • See 2.3.4 Placing Images & Scenes for functions like place-image or place-images.

    Function place-images requires using posn structure. If you want to use your own pos structure, you will have to use place-image.

    Beginning Student Language also doesn't provide foldl, so you have to use some recursion, placing pictures on the scene one by one:

    (require 2htdp/image)
    (require 2htdp/universe)
    
    (define scene (empty-scene 500 500))
    (define-struct pos (x y))
    (define-struct ball (img pos))
    
    (define (place-on-scene scene lst)
      (if (empty? lst)
          scene
          (place-on-scene
           (place-image (ball-img (first lst))
                        (pos-x (ball-pos (first lst)))
                        (pos-y (ball-pos (first lst)))
                        scene)
           (rest lst))))
    
    (define (render lst)
      (place-on-scene scene lst))
    
    (define game (list
                  (make-ball (circle 10 "solid" "blue") (make-pos 250 250))
                  (make-ball (circle 10 "solid" "red") (make-pos 150 150))))
    
    (big-bang game
      [to-draw render])