英文:
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-image
或place-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])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论