2017-12-30 22 views
-1

BSL에서 '브레이크 아웃'을 코딩하려고하는데, 은 내 코드에 환영 및 게임 오버 스크린을 추가하는 방법을 모르므로 막혀 있습니다. 게임이 시작 화면으로 시작한다고 가정하면 이고 화면에서 마우스로 클릭하면 일 때 실제 게임이 시작됩니다. 볼이 화면의 하단 가장자리와 충돌하면 game-over-screen 이 표시됩니다. 나는 어떤 조언을 주셔서 감사합니다!BSL에서 브레이크 아웃 프로그래밍

(define WIDTH 200) 

    (define HEIGHT 200) 

(define BALL-RADIUS 10) 

(define BALL-IMG (circle BALL-RADIUS "solid" "red")) 

(define MT (empty-scene WIDTH HEIGHT)) 

    (define GAME-OVER 
    (place-image (text "Game-over" 30 "black") 
       100 100 
       MT)) 

    (define WELCOME 
     (place-image (text "Welcome" 30 "black") 
       100 100 
       MT)) 

    (define-struct vel (delta-x delta-y)) 
    ; a Vel is a structure: (make-vel Number Number) 
    ; interp. the velocity vector of a moving object 


    (define-struct ball (loc velocity)) 
    ; a Ball is a structure: (make-ball Posn Vel) 
    ; interp. the position and velocity of a object 

    (define RACKET (rectangle 30 10 "solid" "grey")) 
    (define-struct world-state (ball racket)) 
    ; A WorldState is a structure. (make-world-state Ball Location of 
    Racket) 
    ; interp. current velocity and location of ball, current location of 
    racket 


    ; Posn Vel -> Posn 
    ; applies q to p and simulates the movement in one clock tick 
    (check-expect (posn+vel (make-posn 5 6) (make-vel 1 2)) 
       (make-posn 6 8)) 
    (define (posn+vel p q) 
     (make-posn (+ (posn-x p) (vel-delta-x q)) 
      (+ (posn-y p) (vel-delta-y q)))) 


    ; Ball -> Ball 
    ; computes movement of ball in one clock tick 

    (define (move-ball ball) 
     (make-ball (posn+vel (ball-loc ball) 
         (ball-velocity ball)) 
      (ball-velocity ball))) 

    ; A Collision is either 
    ; - "top" 
    ; - "down" 
    ; - "left" 
    ; - "right" 
    ; - "racket" 
    ; - "none" 
    ; interp. the location where a ball collides with a wall 

    ; Posn -> Collision 
    ; detects with which of the walls (if any) or the racket the ball 
    collides 

    (define (collision world-state) 
     (cond 
     [(<= (posn-x (ball-loc (world-state-ball world-state))) BALL- 
    RADIUS) "left"] 
     [(<= (posn-y (ball-loc (world-state-ball world-state))) BALL- 
    RADIUS) "top"] 
     [(>= (posn-x (ball-loc (world-state-ball world-state))) (- WIDTH 
    BALL-RADIUS)) "right"] 
     [(>= (posn-y (ball-loc (world-state-ball world-state))) (- HEIGHT 
    BALL-RADIUS)) "down"] 
     [(and (>= (posn-y (ball-loc (world-state-ball world-state))) (- 
    HEIGHT BALL-RADIUS 10)) 
       (<= (- (posn-x (world-state-racket world-state)) 15) 
        (posn-x (world-state-racket world-state)) 
        (+ (posn-x (world-state-racket world-state)) 15))) 
    "racket"] 
     [else "none"])) 

    ; Vel Collision -> Vel 
    ; computes the velocity of an object after a collision 
    (define (bounce vel collision) 
     (cond [(or (string=? collision "left") 
       (string=? collision "right")) 
      (make-vel (- (vel-delta-x vel)) 
        (vel-delta-y vel))] 
      [(or (string=? collision "top") 
       (string=? collision "racket")) 
      (make-vel (vel-delta-x vel) 
         (- (vel-delta-y vel)))] 
     [else vel])) 


    ; render 

    ; WorldState -> Image 
    ; renders ball and 



    racket at their position 
    (check-expect (image? (render INITIAL-STATE)) #true) 
    (define (render world-state) 
     (place-image BALL-IMG 
        (posn-x (ball-loc (world-state-ball world-state))) 
        (posn-y (ball-loc (world-state-ball world-state))) 
        (place-image RACKET 
           (posn-x (world-state-racket world-state)) 
           195 
           (empty-scene WIDTH HEIGHT)))) 

    ;tick 

    ; WorldState -> WorldState 
    ; moves ball to its next location 
    (check-expect (tick INITIAL-STATE) (make-world-state (make-ball (make-posn 21 14) (make-vel 1 2)) (make-posn 20 195))) 

    (define (tick world-state) 
     (make-world-state (move-ball (make-ball (ball-loc (world-state-ball world-state)) 
          (bounce (ball-velocity (world-state-ball world-state)) 
            (collision world-state)))) 
         (world-state-racket world-state))) 



    ; A Location is a structure: (make-posn Number Number) 
    ; interp. x and y coordinate of a location on screen. 
    (define Loc (make-posn 1 1)) 

    ; A MouseEvent is one of 
    ; - "button-down" 
    ; - "button-up" 
    ; - "drag" 
    ; - "move" 
    ; - "enter" 
    ; - "leave" 
    ; interp. mouse events, e.g., mouse movements or mouse clicks 
    (define MOUSE-CLICK "button-down") 

    ; mouse 
    ; 
    ; Game Number Number MouseEvent -> WorldState 
    ; Update position of racket when the mouse moves 

    ;mouse-template 
    (define (mouse-template world-state mouse-loc-x mouse-loc-y MouseEvent) 
     (cond 
     ((string=? MouseEvent "button-down")...) 
     ((string=? MouseEvent "button-up")...) 
     ((string=? MouseEvent "drag")...) 
     ((string=? MouseEvent "move")...) 
     ((string=? MouseEvent "enter")...) 
     ((string=? MouseEvent "leave")...) 
     (else ...))) 



    (define (mouse world-state mouse-loc-x mouse-loc-y MouseEvent) 
     (cond 
     [(and (string=? MouseEvent "move") 
       (>= mouse-loc-y 180)) 
     (make-world-state (world-state-ball world-state) 
          (make-posn mouse-loc-x 195))] 
     [else world-state])) 


    (define INITIAL-BALL (make-ball (make-posn 20 12) 
            (make-vel 1 2))) 

    (define INITIAL-RACKET (make-posn 20 195)) 


    (define INITIAL-STATE (make-world-state INITIAL-BALL INITIAL-RACKET)) 


    ; WorldState -> WorldState 
    ; Starts the game 

    (define (main state) 
      (big-bang state 
       (on-tick tick 0.01) 
       (to-draw render) 
       (on-mouse mouse))) 

    ; start with INITIAL-STATE 

답변

2

이제 세 가지 상태가 있습니다

  • 게임 오버

지금까지 당신은 단지 "재생"있었다 재생

  • 환영을 당신은 당신의 사용 그것을 대표하는 세계 구조.

    이제 환영과 게임 오버의 두 가지 새로운 구조를 소개해야합니다.

    이전 렌더 함수 이름을 render-world (또는 render-playing)으로 변경하십시오. 는 다음과 같이 쓰기 : 당신의 초기 상태는 환영의 인스턴스가되어야 물론

    (define (render w) 
        (cond 
         [(welcome? w) (render-welcome w)] 
         [(world? w)  (render-world w)] 
         [(game-over? w) (render-game-over w)])) 
    

    을, 당신은 세 가지 틱 기능이 필요합니다.

  • +0

    이제 화면 위쪽에 표시된 벽돌을 코딩하고 싶습니다. 그래서 나는 새로운 세계 구조에 추가 된 '벽돌'구조를 도입했다. 이 매개 변수는 벽돌의 위치뿐 아니라 벽돌이 그려지는지 아닌지에 따라 달라 지므로 볼을 때리면 사라지게합니다. 벽돌의 다른 색상을 추가하는 방법에 대한 조언이 있으며 벽돌을 표시할지 여부를 결정하는 부울을 반환하여 제대로 사라지게하는 함수를 정의하는 방법은 무엇입니까? 나는 어떤 도움을 주셔서 감사합니다! –

    +0

    보이지 않는 브릭을 렌더링하지 않는 것은 공백을 렌더링하는 것과 같습니다. (다른 방법으로는 보이는 벽돌 목록을 유지하고 그것이 칠 때 목록에서 벽돌을 제거하는 것입니다). 다른 색상의 벽돌 : 벽돌 구조에 색상 필드를 추가하고'render-brick' 함수에서'(brick-color a-brick)'을 사용하십시오. – soegaard

    +0

    좋아, 색상있어! 그러나 공이 벽돌을 쳤을 때 공이 벽돌에서 튀어 나오게하기 위해 "벽돌"을 대안으로 추가하여 위에서 충돌 기능을 변경하는 방법을 잘 모르겠습니다. 당신은 그것에 대한 어떤 힌트를 가지고 있습니까? –