gpt4 book ai didi

racket - 当点通过某个点时从我的游戏中删除一条生命?

转载 作者:行者123 更新时间:2023-12-01 06:05:14 25 4
gpt4 key购买 nike

我正在为一个类(class)设计一个游戏,其中:

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

  • 我遇到以下问题:
  • [已解决] 在游戏中以随机间隔向点列表中添加一个新点。这将使点不会线性出现,并且可以一个接一个地生成两个点。
  • [已解决] 在屏幕上从左到右移动点。
  • [已解决] 在命中框区域中删除正确的点。
  • 如果一个点离开屏幕,则删除一条生命。

  • 这里使用的语言是 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)

    最佳答案

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

    关于racket - 当点通过某个点时从我的游戏中删除一条生命?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40954003/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com