DrRacket – 渲染列表中的每个结构

huangapple go评论64阅读模式
英文:

DrRacket - render every structure in a list

问题

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

(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])

让我们假设我只想在SCENE上渲染列表中的所有ball-struct元素。我知道如何在没有列表的情况下做到这一点,但使用列表时我遇到了一些问题。

我正在使用Racket的初学者版本。

我对每个答案都感到感激。

我尝试了递归但无法解决它。

英文:
(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

答案1

得分: 1

请参阅2.3.4放置图像和场景,了解如何使用place-imageplace-images等功能。

函数place-images需要使用posn结构。如果您想使用自己的pos结构,您将需要使用place-image

初学者语言还不提供foldl,因此您必须使用递归,逐个在场景上放置图片:

(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])
英文:

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])

huangapple
  • 本文由 发表于 2023年5月28日 04:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定