Struct 结构中的值出错
我已经开始使用Struct 结构中的值出错,struct,racket,let,Struct,Racket,Let,我已经开始使用2htdp/universe库在racket中构建一个小游戏。到目前为止,我的代码是: #lang racket (require 2htdp/image 2htdp/universe) (define BIRD-IMG (scale .2 (object:image% ...)) ; Image URL: http://i.stack.imgur.com/Vz26B.png (struct world (bird) #:transparent) (struct pos (x
2htdp/universe
库在racket中构建一个小游戏。到目前为止,我的代码是:
#lang racket
(require 2htdp/image 2htdp/universe)
(define BIRD-IMG (scale .2 (object:image% ...)) ; Image URL: http://i.stack.imgur.com/Vz26B.png
(struct world (bird) #:transparent)
(struct pos (x y) #:transparent)
(struct bird (pos img) #:transparent)
(define-values (WIDTH HEIGHT)
(values 600 600))
(define DEFAULT-STATE (world (bird (pos (/ WIDTH 2) (/ HEIGHT 2)) BIRD-IMG)))
(define (move-bird w x y dir)
(world
(bird
(pos
(let
([new-x (+ (pos-x (bird-pos (world-bird w))) x)]
[new-y (+ (pos-y (bird-pos (world-bird w))) y)])
(values
(cond
[(> new-x WIDTH) WIDTH]
[(< new-x 0) 0]
[else new-x])
(cond
[(> new-y HEIGHT) HEIGHT]
[(< new-y 0) 0]
[else new-y]))))
(case dir
[("up") BIRD-IMG]
[("down") (rotate 180 BIRD-IMG)]
[("left") (rotate 90 BIRD-IMG)]
[("right") (rotate -90 BIRD-IMG)]
[else (bird-img (world-bird w))]))))
(define (render w)
(place-image (bird-img (world-bird w))
(pos-x (bird-pos (world-bird w)))
(pos-y (bird-pos (world-bird w)))
(empty-scene WIDTH HEIGHT)))
(define (handle-keys w key)
(case key
[("up") (move-bird w 0 -5 key)]
[("down") (move-bird w 0 5 key)]
[("left") (move-bird w -5 0 key)]
[("right") (move-bird w 5 0 key)]
[else w]))
(big-bang DEFAULT-STATE
(on-draw render)
(on-key handle-keys)
(on-tick (lambda (w) w) 20)
(name "Bird Simulator 2016"))
通过DrRacket的调试器运行它,我可以看到let中的变量已经超出了范围,但是程序的级别低于最外层的
world
struct。我很困惑,因为算术不匹配错误没有指定任何特定的位置/函数,而且我当然没有看到任何地方需要给出更多的值。world
结构只传递一个bird
结构,而bird
结构只传递一个pos
和一个img
。这可能与let
在pos
结构内部有关,但我不确定。请帮忙 很抱歉标题不明确,我想不出更好的了。请随意编辑。您将从let
返回两个值,但pos
构造函数需要两个单独的参数,而不是两个值的单个参数(存在差异)。移除包装pos
并将值替换为pos
,这将解决您的问题。很抱歉标题不明确,我想不出更好的方法了。请随意编辑。您将从let
返回两个值,但pos
构造函数需要两个单独的参数,而不是两个值的单个参数(存在差异)。取下包装pos
并将值
替换为pos
,这将解决您的问题。
result arity mismatch;
expected number of values not received
expected: 1
received: 2
values...: