2015-01-27 2 views
0

이 프로그램은 컴퓨터 과학 수업을위한 프로젝트입니다. 이유가 무엇인지 혼란 스럽기 때문에이 시점에서 미완성입니다. (벡터 -ref (level-entities 현재) 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

REPL은 무슨 일이 일어나고 있는지 알아 내는데 탁월합니다. 상호 작용 창에서'(v (벡터 1 2 3) 정의)'를 입력하십시오. 그런 다음'(vector (vector-set! v 0 9) (vector-set! v 1 10))'의 출력을 봅니다. – molbdnilo

답변

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)))) 
관련 문제