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

时间:2015-01-27 02:50:31

标签: scheme racket

这个程序适用于我的计算机科学课程 到目前为止,它还没有完成,因为我很困惑为什么 (posn-x(vector-ref(level-entities current)0))返回void

我提前道歉,如果这段代码难以理解,或者说很糟糕,我最近才开始学习球拍/计划。

(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))

1 个答案:

答案 0 :(得分:4)

由于这段代码:

(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))))
相关问题