如何在球拍中使用鼠标功能?
How to use on-mouse function in Racket?
我正在尝试制作一个文本移动器,即文本将移动到我单击的位置。另外,当我按下“左”键时,我可以改变文本的颜色。我想我已经完成了程序需要的每个部分,但我现在不知道如何将它们组合在一起 :( 我仍然对如何使用鼠标功能感到困惑。
这是正文部分的位置:
(require 2htdp/image)
(require 2htdp/universe)
(define POSN-0 (make-posn 50 50))
(define POSN-1 (make-posn 200 200))
(define POSN-2 (make-posn 100 100))
(define (posn-temp p)
(... (posn-x p) ...
(posn-y p) ...))
这是颜色部分:
(define Red "red")
(define Black "black")
(define Purple "purple")
(check-expect (RBP Red) "red")
(check-expect (RBP Black) "black")
(check-expect (RBP Purple) "purple")
(define (RBP c)
(cond
[(string=? c Red) "red"]
[(string=? c Black) "black"]
[(string=? c Purple) "purple"]))
这部分提供了text-mover应用程序所需的所有信息,但我不知道如何使用它??
(define-struct tm [str pos col])
(define T1(make-tm "apple" 10 "red"))
(define T2(make-tm "banana" 20 "black"))
(define (tm-temp tm)
(... (tm-str tm) ...
(tm-pos tm) ...
(tm-col tm) ...))
待绘制:
(define bg (square 400 "solid" "white"))
(check-expect
(draw-tm POSN-0)
(place-image
(text "POG!"
25
"red")
50 50
bg))
(define (draw-tm c)
(place-image
(text "POG!"
25
(RBP c))
50 50
bg))
按键:
(define (change-c c ke)
(cond
[(key=? ke "left") (RBP c)]
[else c]))
鼠标上:
(define (move-tm c )
(cond
[(mouse=? ) ]
[else c]))
大爆炸:
(define (move-text initial-tm)
(big-bang initial-tm
[to-draw draw-tm]
[on-mouse move-tm]
[on-key change-c]))
阅读on-mouse
description: function called with on-mouse
has arguments state x y event
and different types of mouse events are described in Mouse event docs。在您的情况下,您将需要 "button-up"
或 "drag"
.
此外,当 x
或 y
或 color
更改时,您必须使用旧实例中的数据创建表示状态的结构的新实例。
这里是示例代码 - 运行 它与 (move-text "FooBar")
,您可以将显示的文本拖动到新位置或使用 left
键更改颜色。
(require 2htdp/image)
(require 2htdp/universe)
(define-struct text-state [text position color])
(define (next-color color)
(second (assoc color (list (list "black" "red")
(list "red" "purple")
(list "purple" "black")))))
(define bg (square 400 "solid" "white"))
(define (draw-state state)
(place-image
(text (text-state-text state)
25
(text-state-color state))
(posn-x (text-state-position state))
(posn-y (text-state-position state))
bg))
(define (change-color state key)
(if (key=? key "left")
(make-text-state (text-state-text state)
(text-state-position state)
(next-color (text-state-color state)))
state))
(define (mouse-fn state x y event)
(if (string=? "drag" event)
(make-text-state
(text-state-text state)
(make-posn x y)
(text-state-color state))
state))
(define (move-text text)
(big-bang (make-text-state text (make-posn 50 50) "black")
[to-draw draw-state]
[on-mouse mouse-fn]
[on-key change-color]))
我正在尝试制作一个文本移动器,即文本将移动到我单击的位置。另外,当我按下“左”键时,我可以改变文本的颜色。我想我已经完成了程序需要的每个部分,但我现在不知道如何将它们组合在一起 :( 我仍然对如何使用鼠标功能感到困惑。 这是正文部分的位置:
(require 2htdp/image)
(require 2htdp/universe)
(define POSN-0 (make-posn 50 50))
(define POSN-1 (make-posn 200 200))
(define POSN-2 (make-posn 100 100))
(define (posn-temp p)
(... (posn-x p) ...
(posn-y p) ...))
这是颜色部分:
(define Red "red")
(define Black "black")
(define Purple "purple")
(check-expect (RBP Red) "red")
(check-expect (RBP Black) "black")
(check-expect (RBP Purple) "purple")
(define (RBP c)
(cond
[(string=? c Red) "red"]
[(string=? c Black) "black"]
[(string=? c Purple) "purple"]))
这部分提供了text-mover应用程序所需的所有信息,但我不知道如何使用它??
(define-struct tm [str pos col])
(define T1(make-tm "apple" 10 "red"))
(define T2(make-tm "banana" 20 "black"))
(define (tm-temp tm)
(... (tm-str tm) ...
(tm-pos tm) ...
(tm-col tm) ...))
待绘制:
(define bg (square 400 "solid" "white"))
(check-expect
(draw-tm POSN-0)
(place-image
(text "POG!"
25
"red")
50 50
bg))
(define (draw-tm c)
(place-image
(text "POG!"
25
(RBP c))
50 50
bg))
按键:
(define (change-c c ke)
(cond
[(key=? ke "left") (RBP c)]
[else c]))
鼠标上:
(define (move-tm c )
(cond
[(mouse=? ) ]
[else c]))
大爆炸:
(define (move-text initial-tm)
(big-bang initial-tm
[to-draw draw-tm]
[on-mouse move-tm]
[on-key change-c]))
阅读on-mouse
description: function called with on-mouse
has arguments state x y event
and different types of mouse events are described in Mouse event docs。在您的情况下,您将需要 "button-up"
或 "drag"
.
此外,当 x
或 y
或 color
更改时,您必须使用旧实例中的数据创建表示状态的结构的新实例。
这里是示例代码 - 运行 它与 (move-text "FooBar")
,您可以将显示的文本拖动到新位置或使用 left
键更改颜色。
(require 2htdp/image)
(require 2htdp/universe)
(define-struct text-state [text position color])
(define (next-color color)
(second (assoc color (list (list "black" "red")
(list "red" "purple")
(list "purple" "black")))))
(define bg (square 400 "solid" "white"))
(define (draw-state state)
(place-image
(text (text-state-text state)
25
(text-state-color state))
(posn-x (text-state-position state))
(posn-y (text-state-position state))
bg))
(define (change-color state key)
(if (key=? key "left")
(make-text-state (text-state-text state)
(text-state-position state)
(next-color (text-state-color state)))
state))
(define (mouse-fn state x y event)
(if (string=? "drag" event)
(make-text-state
(text-state-text state)
(make-posn x y)
(text-state-color state))
state))
(define (move-text text)
(big-bang (make-text-state text (make-posn 50 50) "black")
[to-draw draw-state]
[on-mouse mouse-fn]
[on-key change-color]))