Scheme 当调用向量时,racket代码返回void

Scheme 当调用向量时,racket代码返回void,scheme,racket,Scheme,Racket,这个项目是为我的计算机科学课设计的 现在还没有完成,因为我不明白为什么 (posn-x(矢量参考(当前级别实体)0))返回void 如果这个代码很难理解或者很糟糕,我提前表示歉意,我只是最近才开始学习racket/scheme (require picturing-programs) (require 2htdp/planetcute) (define WIDTH 707) (define HEIGHT 400) (define GRAVITY 10) (define SPEED 20) (d

这个项目是为我的计算机科学课设计的 现在还没有完成,因为我不明白为什么 (posn-x(矢量参考(当前级别实体)0))返回void

如果这个代码很难理解或者很糟糕,我提前表示歉意,我只是最近才开始学习racket/scheme

(require picturing-programs)
(require 2htdp/planetcute)

(define WIDTH 707)
(define HEIGHT 400)
(define GRAVITY 10)
(define SPEED 20)
(define gem .)

;entities vector 
;INVENTORY
;gem1-posn
;gem2_posn
;gem3_posn
;time_posn

;blocks inside the world
(define grass-land 
  (overlay/xy grass-block
              0 40
              dirt-block))

;background of the animation
(define bottom
  (beside grass-land
          grass-land
          grass-land
          grass-land
          grass-land
          grass-land
          grass-land))

;animation window
(define world
  (overlay/align "middle" "bottom"
                 bottom
                 (rectangle WIDTH HEIGHT "solid" "white")))

;model for this game is game
;xy returns a posn showing main characters location
;score returns the amount of stars collected by the end of the game
;time returns the amount of time left in a game
;entitites is a vector that contains all the posn of the gems and game timer
(define-struct level (score time entities))

;make-level: posn
;           number
;           number
;        -> game

;game-score: game
;         -> number

;game-time: game 
;        -> number 

;game-entities: game
;            -> vector

;entities
;gem1
;gem2
;gem3
;player

(define test (make-level 50 20 (vector (make-posn 72 239) (make-posn 101 128) (make-posn 606 237) (make-posn 33 200))))

;checks to call the game score
(check-expect (level-score test) 50)

;checks to call the vector component in the struct
(check-expect (level-entities test)
              (vector (make-posn 72 239) (make-posn 101 128) (make-posn 606 237) (make-posn 33 200)))

;checks to call a posn from the vector component of the struct
(check-expect (vector-ref (level-entities test) 3)
              (make-posn 33 200))

;checks to call posn-x from the a posn in the vector component of the struct
(check-expect (posn-x (vector-ref (level-entities test) 3)) 33)

(define game (make-level 0 180 (vector (make-posn 72 239) (make-posn 101 128) (make-posn 606 237) (make-posn 33 200))))

;RANDOMIZER
;takes in a number and returns a set of random coordinates as a posn
;20<=x<=673 
;90<=y<=150
;random-posn: number(x)
;             posn

(define (random-posn x)
  (make-posn (+ 20 (random (- WIDTH 20))) 
             (+ 90 (random (- HEIGHT 250)))))

(check-random (random-posn 1)
              (make-posn (+ 20 (random (- WIDTH 20))) 
                         (+ 90 (random (- HEIGHT 250)))))

;TICK HANDLER
;takes in a game struct and keeps or returns the y value of the 3rd vector component to 200
;randomizes the locations of the 3 gems using the vector component
;subracts 1 second from the time
;handle-tick: current(game)
;          -> game
(define (handle-tick current)
  (cond [(and (<= (posn-y (vector-ref (level-entities current) 3)) 200)
              (and (<= (level-time current) 180)
                   (> (level-time current) 0)))
         (make-level (level-score current)
                     (- (level-time current) 1)
                     (vector (vector-set! (level-entities current) 
                                          0
                                          (random-posn 1))
                             (vector-set! (level-entities current) 
                                          1
                                          (random-posn 1))
                             (vector-set! (level-entities current) 
                                          2
                                          (random-posn 1))
                             (vector-set! (level-entities current) 
                                          3
                                          (make-posn (posn-x (vector-ref (level-entities current) 3))
                                                     (- (posn-y (vector-ref (level-entities current) 3)) GRAVITY)))))]
[else  (make-level (level-score current)
                   (- (level-time current) 1)
                   (level-entities current))]))

;DRAW HANDLER
;takes in a game struct and draws character-boy based on the entities vector(entities)
;handle-tick: current(game)
;          -> img
(define (handle-draw current)
  ;score             
  (place-image (text (string-append "SCORE: "
                                    (number->string (level-score current)))
                     25 "black")
               630 60
               ;time 
               (place-image (text (string-append "TIME: " 
                                                 (number->string (level-time current)))
                                  25 "black")
                            630 25
                            ;gem1
                            (place-image gem
                                         (posn-x (vector-ref (level-entities current) 0))
                                         (posn-y (vector-ref (level-entities current) 0))
                                         ;gem2 
                                         (place-image gem
                                                      (posn-x (vector-ref (level-entities current) 1))
                                                      (posn-y (vector-ref (level-entities current) 1))
                                                      ;gem3
                                                      (place-image gem
                                                                   (posn-x (vector-ref (level-entities current) 2))
                                                                   (posn-y (vector-ref (level-entities current) 2))
                                                                   ;character
                                                                   (place-image character-boy
                                                                                (posn-x (vector-ref (level-entities current) 3))
                                                                                (posn-y (vector-ref (level-entities current) 3))
                                                                                world)))))))

;KEY-HANDLER
;takes in game struct and the "left" "right" "up" keys to move the character 
;handle-key: current(game)
;            game
;(define (handle-key current key)
 ; (cond [(key=? "left" key)
  ;       (make-game 

(big-bang game
          (on-tick handle-tick)
;          (on-key handle-key)
          (on-draw handle-draw))
(需要绘制程序)
(需要2htdp/刨刀)
(定义宽度707)
(定义高度400)
(定义重力10)
(定义速度20)
(定义gem。)
;实体向量
;库存
;gem1-posn
;gem2_posn
;gem3_posn
;时间
;世界内部的街区
(定义草地)
(覆盖层/xy草块)
0 40
污垢块)
;动画背景
(定义底部)
(草地旁)
草地
草地
草地
草地
草地
草地)
;动画窗口
(定义世界)
(覆盖/对齐“中间”和“底部”
底部
(矩形宽度高度“实心”“白色”))
;这个游戏的模型是游戏
;xy返回显示主要字符位置的posn
;score返回游戏结束时收集的星星数
;时间返回游戏中剩余的时间量
;entitites是一个包含所有宝石和游戏计时器位置的向量
(定义结构级别(得分时间实体))
;制造级别:posn
;           数
;           数
;        -> 游戏
;游戏分数:游戏
;         -> 数
;游戏时间:游戏
;        -> 数
;游戏实体:游戏
;            -> 矢量
;实体
;gem1
;gem2
;gem3
;运动员
(定义测试(生成等级5020(向量(生成位置72239)(生成位置101128)(生成位置606237)(生成位置33200)))
;检查以调用游戏分数
(检查期望值(水平分数测试)50)
;检查以调用结构中的向量组件
(选中expect(级别实体测试)
(向量(生成位置72239)(生成位置101128)(生成位置606237)(生成位置33200)))
;检查以从结构的向量组件调用posn
(检查expect(向量参考(级别实体测试)3)
(使位置33 200)
;检查以从结构的向量组件中的a posn调用posn-x
(检查expect(posn-x(向量参考(级别实体测试)3))33)
(定义游戏(制造等级0180(向量(制造位置72239)(制造位置101128)(制造位置606237)(制造位置33200)))
;随机发生器
;接受一个数字并返回一组随机坐标作为posn
;20由于该代码:

(vector (vector-set! (level-entities current) 
                     0
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     1
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     2
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     3
                     (make-posn (posn-x (vector-ref (level-entities current) 3))
                                (- (posn-y (vector-ref (level-entities current) 3)) GRAVITY))))
这不是初始化向量的正确方法。你可能想要的是:

(vector (random-posn 1)
        (random-posn 1)
        (random-posn 1)
        (let ((current-posn (vector-ref (level-entities current) 3)))
          (make-posn (posn-x current-posn)
                     (- (posn-y current-posn) GRAVITY))))
由于该代码:

(vector (vector-set! (level-entities current) 
                     0
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     1
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     2
                     (random-posn 1))
        (vector-set! (level-entities current) 
                     3
                     (make-posn (posn-x (vector-ref (level-entities current) 3))
                                (- (posn-y (vector-ref (level-entities current) 3)) GRAVITY))))
这不是初始化向量的正确方法。你可能想要的是:

(vector (random-posn 1)
        (random-posn 1)
        (random-posn 1)
        (let ((current-posn (vector-ref (level-entities current) 3)))
          (make-posn (posn-x current-posn)
                     (- (posn-y current-posn) GRAVITY))))

REPL非常适合了解正在发生的事情。在交互窗口中,输入
(定义v(向量1 2 3))
。然后查看
(vector(vector(vector set!v0 9)(vector set!v1 10))的输出。
。REPL非常适合找出发生了什么。在交互窗口中,输入
(定义v(向量1 2 3))
。然后查看
(向量(向量集!v09)(向量集!v110))的输出