我在旋转按键上的动画图像时遇到问题。我不太了解大爆炸函数并且已经失去了希望。
这是我的代码:
(require 2htdp/image)
(require 2htdp/universe)
;; Found the png of the rocketship online. Couldn't find anything smaller so, we adjust the background to not make it look gigantic.
(define SHIP "rocketship png")
;; Restricts the landing to the bottom of the scene's boundary
(define (create-rocket-scene height)
(place-image SHIP 150 (min height 550) (rectangle 300 600 'solid 'black)))
(define (key-press w ke)
(cond
\[(key=? ke "u") (make-posn 0)\]
\[(key=? ke "d") (make-posn (rotate 180))\]
\[else w\]))
(big-bang 0
\[to-draw render\]
\[on-key key-press\])
这会导致许多错误。我需要让火箭飞船降落在场景中并着陆(这都是正确的)。然而,一旦我添加了 on-key 和 big-bang 功能以允许我将图像旋转 180 度,它就不起作用了。我很沮丧。请指教。
为了以正确的位置和正确的方向(旋转)绘制火箭,您需要两条信息:1) 火箭的位置和 2) 火箭的方向。
目前您的世界仅由位置组成,因此您使用的是包含
posn
和 x
值的 y
结构。
缺少的是存储方向的地方。
一个选项是:
(define-struct world [x y r])
这里
x
和 y
和以前一样工作。 r
是以弧度为单位测量的火箭角度。
另一种选择是:
(define-struct world [p r])
这里
p
将以 posn
结构的形式持有仓位,并且
r
是以弧度表示的火箭角度。