如何在Racket中使用按键功能?
How to use on-key function in Racket?
我正在尝试制作门模拟器。通过使用钥匙,我可以改变门的状态,我查看了 Racket 中的文档。主要讲的是“上”“下”“左”“右”等一些常用键。我想问一下如何自定义按键。这是我的代码:
(define DOOR-OPEN-iamge)
(define DOOR-CLOSED-image)
(define DOOR-LOCKED-image)
(define DS-OPEN "open")
(define DS-CLOSED "closed")
(define DS-LOCKED "locked")
(define (door-simulator initial-ds)
(big-bang initial-ds
[to-draw draw-ds]
[on-key change]))
(check-expect (draw-ds DS-OPEN) DOOR-OPEN)
(check-expect (draw-ds DS-CLOSED) DOOR-CLOSED)
(check-expect (draw-ds DS-LOCKED) DOOR-LOCKED)
(define (draw-ds ds)
(cond
[(string=? ds DS-OPEN) DOOR-OPEN]
[(string=? ds DS-CLOSED) DOOR-CLOSED]
[(string=? ds DS-LOCKED) DOOR-LOCKED]))
;门状态键
;"o"-关门 => 开门
;"c"-开门 => 关门
;"l"-关门 => 门锁
;"u"-门锁 => 门开
(check-expect (change DOOR-CLOSED) DOOR-OPEN)
(check-expect (change DOOR-OPEN) DOOR-CLOSED)
(check-expect (change DOOR-CLOSED) DOOR-LOCKED)
(check-expect (change DOOR-LOCKED) DOOR-OPEN)
(define (change w a-key)
(cond
[(key=? a-key "o") (draw-ds w)]
[(key=? a-key "c") (draw-ds w)]
[(key=? a-key "l") (draw-ds w)]
[(key=? a-key "u") (draw-ds w)]))
另外,关键部分的check-expect没有起作用。不过我应该为那部分使用什么函数或变量。
需要这些库(planetcute
只是包含了一些用于绘图的图片,您可以稍后删除它):
(require 2htdp/planetcute)
(require 2htdp/universe)
(require 2htdp/image)
前六个定义不会改变,我只是添加了一些来自planetcute
的图片:
(define door-open chest-open)
(define door-closed chest-closed)
(define door-locked chest-lid)
(define ds-open "open")
(define ds-closed "closed")
(define ds-locked "locked")
door-simulator
的定义不会改变:
(define (door-simulator initial-ds)
(big-bang initial-ds
[to-draw draw-ds]
[on-key change]))
draw-ds
将状态转换为图片并使用 place-image
:
将其绘制到背景
(define (ds->image ds)
(cond ((string=? ds ds-open) door-open)
((string=? ds ds-closed) door-closed)
((string=? ds ds-locked) door-locked)
(#true door-open)))
(define bg (rectangle 500 500 "solid" "white"))
(define (draw-ds state)
(place-image (ds->image state) 50 50 bg))
函数 change
不会调用 draw-ds
,而只是 returns 新状态,基于当前状态和键:
(define (change state a-key)
(cond
[(and (key=? a-key "o")
(string=? state ds-closed))
ds-open]
[(and (key=? a-key "c")
(string=? state ds-open))
ds-closed]
[(and (key=? a-key "l")
(string=? state ds-closed))
ds-locked]
[(and (key=? a-key "u")
(string=? state ds-locked)) ds-open]
(#true state)))
您可能需要重新排序这些定义,然后使用 (door-simulator ds-closed)
启动动画并按一些键(o
、c
...)。
我正在尝试制作门模拟器。通过使用钥匙,我可以改变门的状态,我查看了 Racket 中的文档。主要讲的是“上”“下”“左”“右”等一些常用键。我想问一下如何自定义按键。这是我的代码:
(define DOOR-OPEN-iamge)
(define DOOR-CLOSED-image)
(define DOOR-LOCKED-image)
(define DS-OPEN "open")
(define DS-CLOSED "closed")
(define DS-LOCKED "locked")
(define (door-simulator initial-ds)
(big-bang initial-ds
[to-draw draw-ds]
[on-key change]))
(check-expect (draw-ds DS-OPEN) DOOR-OPEN)
(check-expect (draw-ds DS-CLOSED) DOOR-CLOSED)
(check-expect (draw-ds DS-LOCKED) DOOR-LOCKED)
(define (draw-ds ds)
(cond
[(string=? ds DS-OPEN) DOOR-OPEN]
[(string=? ds DS-CLOSED) DOOR-CLOSED]
[(string=? ds DS-LOCKED) DOOR-LOCKED]))
;门状态键
;"o"-关门 => 开门
;"c"-开门 => 关门
;"l"-关门 => 门锁
;"u"-门锁 => 门开
(check-expect (change DOOR-CLOSED) DOOR-OPEN)
(check-expect (change DOOR-OPEN) DOOR-CLOSED)
(check-expect (change DOOR-CLOSED) DOOR-LOCKED)
(check-expect (change DOOR-LOCKED) DOOR-OPEN)
(define (change w a-key)
(cond
[(key=? a-key "o") (draw-ds w)]
[(key=? a-key "c") (draw-ds w)]
[(key=? a-key "l") (draw-ds w)]
[(key=? a-key "u") (draw-ds w)]))
另外,关键部分的check-expect没有起作用。不过我应该为那部分使用什么函数或变量。
需要这些库(planetcute
只是包含了一些用于绘图的图片,您可以稍后删除它):
(require 2htdp/planetcute)
(require 2htdp/universe)
(require 2htdp/image)
前六个定义不会改变,我只是添加了一些来自planetcute
的图片:
(define door-open chest-open)
(define door-closed chest-closed)
(define door-locked chest-lid)
(define ds-open "open")
(define ds-closed "closed")
(define ds-locked "locked")
door-simulator
的定义不会改变:
(define (door-simulator initial-ds)
(big-bang initial-ds
[to-draw draw-ds]
[on-key change]))
draw-ds
将状态转换为图片并使用 place-image
:
(define (ds->image ds)
(cond ((string=? ds ds-open) door-open)
((string=? ds ds-closed) door-closed)
((string=? ds ds-locked) door-locked)
(#true door-open)))
(define bg (rectangle 500 500 "solid" "white"))
(define (draw-ds state)
(place-image (ds->image state) 50 50 bg))
函数 change
不会调用 draw-ds
,而只是 returns 新状态,基于当前状态和键:
(define (change state a-key)
(cond
[(and (key=? a-key "o")
(string=? state ds-closed))
ds-open]
[(and (key=? a-key "c")
(string=? state ds-open))
ds-closed]
[(and (key=? a-key "l")
(string=? state ds-closed))
ds-locked]
[(and (key=? a-key "u")
(string=? state ds-locked)) ds-open]
(#true state)))
您可能需要重新排序这些定义,然后使用 (door-simulator ds-closed)
启动动画并按一些键(o
、c
...)。