编程新手,但我跟随了Racket的教程,我试图在登陆基地上安装火箭。让火箭上班,但我想添加更多的物体,如登陆基地。这就是我所拥有的:
; constants
(define width 100)
(define height 100)
(define background "sky blue")
(define mtscn (empty-scene width height background))
(define rocket.)
(define rocket-center-to-bottom
(- height (/ (image-height rocket) 2)))
(define base.)
(define base-center-to-bottom
(- height (/ (image-height rocket) 2)))
; functions
(define (create-rocket-scene.v6 h)
(cond
[(<= h rocket-center-to-bottom)
(place-image rocket 50 h mtscn)]
[(> h rocket-center-to-bottom)
(place-image rocket 50 rocket-center-to-bottom mtscn)]
[(<= h base-center-to-bottom)
(place-image base 50 h mtscn)]
[(> h base-center-to-bottom)
(place-image base 50 base-center-to-bottom mtscn)]))
(animate create-rocket-scene.v6)
基本上复制并粘贴火箭代码然后将火箭重命名为base然后制作基础图像。它说它的工作,但基地没有显示。我希望基础图像保持在底部,而火箭从顶部到底部所在的底部。谢谢你的帮助
答案 0 :(得分:1)
这就是问题所在:
(cond
[(<= h rocket-center-to-bottom) (place-image rocket 50 h mtscn)]
[(> h rocket-center-to-bottom) (place-image rocket 50 rocket-center-to-bottom mtscn)]
[(<= h base-center-to-bottom) (place-image base 50 h mtscn)]
[(> h base-center-to-bottom) (place-image base 50 base-center-to-bottom mtscn)])
cond
- 表达式找到第一个“问题”,
并使用随之而来的“答案”。
如果(<= h rocket-center-to-bottom)
为真,则使用第一个子句,并且
使用(place-image rocket 50 h mtscn)
。这意味着永远不会到达使用base
的第三个子句。
相反,你需要将山景和基地画成同一张图片:
(place-image base 50 h
(place-image rocket 50 h mtscn))
这将火箭置于山顶之上,并将其置于山顶之上。
也就是说,cond
只需要两个子句:
(cond
[(<= h rocket-center-to-bottom) (place-image base 50 h
(place-image rocket 50 h mtscn))]
[(> h rocket-center-to-bottom) ...draw-both-here...])