当点经过某一点时,从我的游戏中移除生命?

时间:2016-12-04 00:14:39

标签: racket

我正在为以下课程设计游戏:

  • 从屏幕右侧出现一个彩色圆点,并在固定的水平轴上向左移动屏幕。
  • 当点到达中间时,玩家需要按下与其颜色对应的键。如果在合适的时间完成,则分数会增加一。
  • 如果没有,玩家将失去一条生命。

我遇到以下问题:

  • [已解决]在游戏中以随机间隔向点列表添加新点。这样就可以使点不会呈现线性,并且可以一个接一个地生成两个点。
  • [已解决]从左到右移动屏幕上的点。
  • [已解决]在点击框区域内删除正确的点。
  • 如果点落在屏幕外,则删除生命。

此处使用的语言是具有lambda的中级学生。

目前,当我按1表示红色时,只要红点是列表中的第一个,它就会将其删除。我需要一个删除特定红点的函数。

到目前为止,这是代码;我知道它没有完成,因此无法按照我的意愿工作。

; Color Match


; Game structures
(define-struct dot [x color])
(define-struct cm [dots score lives state])

; Constants
(define width 800)
(define height (/ width (/ 16 9)))
(define h-w (/ width 2))
(define h-h (/ height 2))
(define arrow (beside (rectangle 25 10 "solid" "black")
                                             (rotate 135 (right-triangle 18 18 "solid" "black"))))

(define background (empty-scene width height))
(define bars (place-images
    (list
     (circle (/ width 25) "outline" "black")
     (rectangle (+ width 2) (- height 375) "outline" "black"))
    (list
     (make-posn h-w h-h)
     (make-posn h-w h-h))
  background))

; Game
(define (main ws)
  (big-bang (make-cm empty 0 3 "start")
            [on-tick tock]
            [to-draw render]
            [on-release interact]
            ))

;; random-color: number -> string
;; consumes a number and returns a color
;; string for the given number
(define (random-color n)
  (cond
    [(= n 0) "red"]
    [(= n 1) "blue"]
    [(= n 2) "green"]
    [(= n 3) "yellow"]
    [else (error "Invalid color chosen.")]))

;; draw-dot: structure -> image
;; consumes a dot structure and draws it as an image

(define (draw-dot struct)
  (cond
    [(string=? (dot-color struct) "red") (overlay
                                    (rotate 90 arrow)
                                    (circle (/ width 25) "outline" "black")
                                    (circle (/ width 25) "solid" (dot-color struct)))]
    [(string=? (dot-color struct) "blue") (overlay
                                    (rotate 270 arrow)
                                    (circle (/ width 25) "outline" "black")
                                    (circle (/ width 25) "solid" (dot-color struct)))]
    [(string=? (dot-color struct) "green") (overlay
                                    (flip-horizontal arrow)
                                    (circle (/ width 25) "outline" "black")
                                    (circle (/ width 25) "solid" (dot-color struct)))]
    [(string=? (dot-color struct) "yellow") (overlay
                                    arrow
                                    (circle (/ width 25) "outline" "black")
                                    (circle (/ width 25) "solid" (dot-color struct)))]))


;; tock: color-match -> color-match
;; placeholder for later worldstate
(define (tock ws)
  (cond
    [(string=? (cm-state ws) "play")
     (make-cm (move-dots (add-dot? (random 50) (cm-dots ws)))
              (+ (cm-score ws) 1)
              (cm-lives ws)
              "play")]
    [else ws]))

;; add-dot?: number, list of dots -> list of dots
;; consumes list of dots and number and adds a new dot
;; to the list of dots
(define (add-dot? n lod)
  (cond
    [(= n 1) (cons (make-dot (+ width 25) (random-color (random 4))) lod)]
    [else lod]))

;; render: color-match -> image
;; consumes the current color-match structure and calls the appropriate
;; helper function.
(define (render ws)
  (cond
    [(string=? (cm-state ws) "start") (render-start ws)]
    [(string=? (cm-state ws) "play") (render-play ws)]
    [(string=? (cm-state ws) "pause") (render-pause ws)]
    [else (error "Invalid gamestate chosen.")]))

;; render-start: color-match -> image
;; helps the render function by displaying the current state
;; as an image.
(define (render-start ws)
  (place-image
   (above
    (text "Instructions" (/ width 20) "red")
    (text "Colored shapes will begin appearing from the right of the screen." (/ width 50) "black")
    (text "Once they reach the bar in the middle, press the appropriate key." (/ width 50) "black")
    (text "1: Red" (/ width 50) "red")
    (text "2: Blue" (/ width 50) "blue")
    (text "3: Green" (/ width 50) "green")
    (text "4: Yellow" (/ width 50) "yellow"))
   h-w
   h-h
   background))


;; draw-dots: list of dots -> image
;; consumes a list of dots and draws them
;; according to their color
(define (draw-dots lod)
  (cond
    [(empty? lod) bars]
    [else (place-image (draw-dot (first lod))
                       (dot-x (first lod))
                       h-h
                       (draw-dots (rest lod)))]))

;; is-visible?: dot -> boolean
;; consumes a dot and determines if it is currently visible
;; on the canvas
(define (is-visible? dot)
  (cond
    [(> (dot-x dot) -25) #true]
    [else #false]))

;; move-dot: dot -> number
;; consumes a dot and returns its new
;; x coordinate
(define (move-dot dot)
  (- (dot-x dot) 10))

;; new-dot: dot -> dot
;; consumes a dot and returns a new dot by calling
;; the move-dot function
(define (new-dot dot)
  (make-dot (move-dot dot) (dot-color dot)))

;; move-dots: list of dots -> list of dots
;; consumes a list of dots and moves them across the
;; canvas as long as they are in view
(define (move-dots lod)
  (cond
    [(empty? lod) empty]
    [(not (is-visible? (first lod))) (rest lod)]
    [else (cons (new-dot (first lod)) (move-dots (rest lod)))]))


;; draw-bars: cm -> image
;; consumes the world state and returns the image of bars
(define (draw-bars ws)
  (place-image (draw-dots (cm-dots ws))
               h-w
               h-h
               bars))


;; render-play: color-match -> image
;; helps the render function by displaying the current state
;; as an image.
(define (render-play ws)
  (overlay/align "left" "top" (current-lives ws)
  (underlay/align "right" "top" (place-image (draw-bars ws)
                                             h-w
                                             h-h 
                                             background)
                  (current-score ws))))

;; current-lives: cm -> image
;; consumes the worldstate and displays the
;; current life count
(define (current-lives ws)
  (text (string-append "Lives: " (number->string (cm-lives ws))) 18 "red"))

;; current-score: cm -> image
;; consumes the worldstate and shows the
;; current score
(define (current-score ws)
  (text (string-append "Score: " (number->string (cm-score ws))) 18 "black"))

;; render-pause: color-match -> image
;; helps the render function by displaying the current state
;; as an image.
(define (render-pause ws)
  (place-image
  (text "paused" (/ width 20) "red")
  h-w
  (- h-h 75)
  (render-play ws)))

; (define (check-dot lod key)
;   (cond
;     [(empty? lod) empty]
;     [(and (< (- h-w (+ (/ width 25) 25)) (dot-x (first lod)) (+ h-w (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "red") (key=? key "1")) (remove (make-dot (dot-x (first lod)) "red") lod)]
;     [(and (< (- h-w (+ (/ width 25) 25)) (dot-x (first lod)) (+ h-w (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "blue") (key=? key "2")) (remove (make-dot (dot-x (first lod)) "blue") lod)]
;     [(and (< (- h-w (+ (/ width 25) 25)) (dot-x (first lod)) (+ h-w (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "green") (key=? key "3")) (remove (make-dot (dot-x (first lod)) "green") lod)]
;     [(and (< (- h-w (+ (/ width 25) 25)) (dot-x (first lod)) (+ h-w (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "yellow") (key=? key "4")) (remove (make-dot (dot-x (first lod)) "yellow") lod)]
;     [else lod]))


;; correct-dot? cm, lod, string -> list of dots
;; consumes the world state, its list of dots, a string, and sorts
;; out dots that have been correctly selected
(define (correct-dot? lod c)
  (cond
    [(empty? lod) empty]
    [(and (< (- h-w (+ (/ width 25) 1)) (dot-x (first lod)) (+ h-w (+ (/ width 25) 1))) (string=? (dot-color (first lod)) c)) (correct-dot? (rest lod) c)]
    [else (cons (first lod) (correct-dot? (rest lod) c))]))

;; is-it?: list of dots, key -> list of dots
;; consumes a list of dots and a key and returns
;; a new list of dots with one removed if the
;; conditions are met for it
(define (is-it? lod key)
  (cond
    [(empty? lod) empty]
    [(key=? key "up") (correct-dot? lod "red")]
    [(key=? key "down") (correct-dot? lod "blue")]
    [(key=? key "left") (correct-dot? lod "green")]
    [(key=? key "right") (correct-dot? lod "yellow")]
    [else lod]))

;; interact: color-match, key -> color-match
;; consumes the current color-match state and returns
;; a new one depending on which key is pressed.
(define (interact ws key)
  (cond
    [(string=? (cm-state ws) "play")
     (cond
       [(key=? key "p") (make-cm (cm-dots ws)
                                 (cm-score ws)
                                 (cm-lives ws)
                                 "pause")]
       [else (make-cm (is-it? (cm-dots ws) key)
                      (cm-score ws)
                      (cm-lives ws)
                      (cm-state ws))])]
    [(and (string=? (cm-state ws) "start") (key=? key "p")) (make-cm (cm-dots ws)
                                                                     (cm-score ws)
                                                                     (cm-lives ws)
                                                                     "play")]
    [(and (string=? (cm-state ws) "pause") (key=? key "p")) (make-cm (cm-dots ws)
                                                                     (cm-score ws)
                                                                     (cm-lives ws)
                                                                     "play")]
    [else ws]))

(main 200)

1 个答案:

答案 0 :(得分:0)

在ISL中,列表不可变,因此您唯一能做的就是使用除要删除的内容之外的所有内容创建一个新列表。您可以使用您的程序过滤列表,只接受您要保留的元素