Racket – When you have some point, remove your life from my game?

I’m designing a game for a class, where:

>A colored dot appears from the right side of the screen and moves to the left on a fixed horizontal axis Screen.
>When the point reaches the middle, the player needs to press the key corresponding to its color. If it is completed at the correct time, the score will increase by 1.
>If not, the player will lose a life.

I encountered the following problem:

> [Solved] Adding new points to the point list at random intervals in the game. This will prevent the points from appearing linearly, and one after another Generate two points.
> [Solved] Move the point on the screen from left to right.
> [Solved] Delete the correct point in the hitbox area.
>If the point deviates from the screen, then Delete life.

The language used here is an intermediate student of lambda.

Currently, when I press 1 to indicate red, as long as the red dot is the first in the list, it is Will delete it. I need a function to delete a specific red dot.

This is the code so far; knowing that it is not completed, so I did not work as I wanted.

; 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 hw (/ width 2))
(define hh (/ 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 hw hh)
(make-posn hw hh))
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
(rot ate 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")
(c ircle (/ 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"))
hw
hh
background))
< br />
;; 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))
hh
(draw-dots (rest lod))))))

;; is-visible?: dot -> boolean
;; consumes a dot and determines if it is currently visible< br />;; 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))
hw
hh
bars))< br />

;; 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)
hw
hh
background)
(current-score ws))))

;; current-lives: cm -> image
;; consumes the worldstate and displays th e
;; 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")
hw
(- hh 75)
(render-play ws)))

; (define (check-dot lod key)
; (cond
; [(empty? lod) empty]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x (first lod)) (+ hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "red") (key =? key "1")) (remove (make-dot (dot-x (first lod)) "red") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x ( first lod)) (+ hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "blue") (key=? key "2")) (remove (make- dot (dot-x (first lod)) "blue") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x (first lod)) ( + hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "green") (key=? key "3")) (remove (make-dot (dot-x (first lod)) "green") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x (first lod)) (+ hw (+ ( / 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 (< (- hw (+ (/ width 25) 1)) (dot-x (first lod)) (+ hw (+ (/ width 25) 1))) (string=? (dot-color (first lod)) c)) (corre ct-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)

In ISL, the list is immutable, so the only thing you can do is to create a A new list of content. You can use your process to filter the list and only accept the elements you want to keep

I am designing a game for a class, where:

< p>> A colored dot appears from the right side of the screen and moves the screen to the left on a fixed horizontal axis.
>When the dot reaches the middle, the player needs to press the key corresponding to its color. If it is done at the right time, The score will increase by 1.
>If not, the player will lose a life.

I have the following problems:

> [Solved] Add a new point to the point list. This will make the point not appear linearly, and two points can be generated one after the other.
> [Solved] Move the point on the screen from left to right.
> [Already Solution] Delete the correct point in the hitbox area.
>If the point deviates from the screen, delete the life.

The language used here is an intermediate student of lambda.

Currently, When I press 1 to indicate red, as long as the red dot is the first in the list, it will delete it. I need a function to delete a specific red dot.

This is the code so far; Knowing that it was not completed, so I did not work as I wanted.

; 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 hw (/ width 2))
(define hh (/ 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 hw hh)
( make-posn hw hh))
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< br /> [(= 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)]< br /> [(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"))
hw
hh
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))
hh
(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 m oves 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))
hw
hh
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)
hw
hh
background)
(c urrent-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")
hw
(- hh 75)
(render-play ws)))

; (define (check-dot lod key)
; (cond
; [(empty? lod) empty]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x ( first lod)) (+ hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "red") (key=? key "1")) (remove (make-dot (dot-x (first lod)) "red") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x ( first lod)) (+ hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "blue") (key=? key "2")) (remove (make- dot (dot-x (first lod)) "blue") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x (first lod)) ( + hw (+ (/ width 25) 25))) (string=? (dot-color (first lod)) "green") (key=? key "3")) (remove (make-dot (dot-x (first lod)) "green") lod)]
; [(and (< (- hw (+ (/ width 25) 25)) (dot-x (first lod)) (+ hw (+ ( / 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 (< (- hw (+ ( / width 25) 1)) (dot-x (first lod)) (+ hw (+ (/ 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]))
< br />;; 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)

In ISL, list Immutable, so the only thing you can do is to create a new list that contains everything except what you want to delete. You can use your process to filter the list and only accept the elements you want to keep

< /p>

Leave a Comment

Your email address will not be published.