1
(require 2htdp/image)
(require 2htdp/universe)

(define (render t)
  (text (number->string t) 10 "red"))

(define (ball-image t)
  (place-image (circle 10 "solid" "red")
              150
              150

               (empty-scene 300 300)))



(define (change w a-key)
  (cond
    [(key=? a-key "left")  (ball-image w)]
    [(key=? a-key "right") (ball-image w )]
    [(= (string-length a-key) 1) w] 
    [(key=? a-key "up")    (ball-image w )]
    [(key=? a-key "down")  (ball-image w )]
    [else w]))



(big-bang 100
          (on-tick sub1 )
          (to-draw ball-image)
          (on-key change))

真ん中に置いた赤いボールを上下左右に動かそうとしています。矢印キーのいずれかを押すと、数値を期待しているが画像が指定されていると表示されます。私は何を間違っていますか?

4

1 に答える 1

4

まず第一に、このメイン サークルで世界がどのように処理されるかを理解する必要があります。

  • システムは 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
于 2013-01-28T09:01:04.023 に答える