まず第一に、このメイン サークルで世界がどのように処理されるかを理解する必要があります。
- システムは big-bang - 100 の最初の引数を取り、それをWorldStateとして記憶します。
- 次に、各ティックに存在する場合、それをオンティック ( sub1 ) 関数に渡します。
- キーが押されると、on-key ( change ) が呼び出され、そこにwoldStateが aw 引数として渡されます。
- そこにいくつかの絵を描き、矢印キーが押された場合にそれを返します。したがって、矢印が押されると、ball-imageの結果 = place-imageの結果- imageが返されます。
- システムはそれを現在のworldStateとして記憶し、次のティックで新しい値を古いプロシージャ sub1 に渡します。
- 値がimageになったため、sub1はそれを拒否します。
--
ボールを 2 方向に動かしたい場合は、少なくとも 2 つの座標 (x . y) を保存する必要があります。それでは、WorldStateを 2 つの数値のペアにします。それ自体では何も変化しないため、on-tick 関数は必要ありません。また、キーボード プロセッサでボールを描画する必要がないため、ペア ( worldState ) の対応する値を単純に変更し、ボールを新しい場所に配置する呼び出し ( ball-image ) 中にのみ描画します ( x = (car t)、y = (cdr t)、および (x . y) = (cons xy)) を思い出してください。
(require 2htdp/image)
(require 2htdp/universe)
(define (ball-image t) ;<-- the t-parameter is our WorldState
(place-image (circle 10 "solid" "red")
(car t) ;<-- here now x variable coordinate
(cdr t) ;<-- here now y variable, instead of 150
(empty-scene 300 300)))
(define (change w a-key)
(cond ;w - is the previous worldState, V here we change it
[(key=? a-key "left") (cons (sub1 (car w)) (cdr w))];and
[(key=? a-key "right") (cons (add1 (car w)) (cdr w))];return
[(= (string-length a-key) 1) w] ;<-- this line is excess
[(key=? a-key "up") (cons (car w) (sub1 (cdr w)))]
[(key=? a-key "down") (cons (car w) (add1 (cdr w)))]
[else w])) ;<-- If the key of no interest, just
return the previous WorldState
(big-bang '(150 . 150) ;<-- initial state
(to-draw ball-image) ;<-- redraws the world
(on-key change)) ;<-- process the event of key press