Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 使用新列表调用函数_Recursion_Racket - Fatal编程技术网

Recursion 使用新列表调用函数

Recursion 使用新列表调用函数,recursion,racket,Recursion,Racket,大家好,我想用Drracket制作一个动画,4个不同的对象以不同的方式移动,比如“左上右下”,但在勾号部分我遇到了一个问题。 所以问题是我给函数一个列表,我想取另一个新的列表,用这个“新”列表再次调用函数,这样它会自动递归,形状会不停地移动 我的程序是这样的 (define R 300) (define L (* 2 R)) (define U (* 2.5 R)) (define MYSCN (empty-scene L U)) ;Structure: (define-struct

大家好,我想用Drracket制作一个动画,4个不同的对象以不同的方式移动,比如“左上右下”,但在勾号部分我遇到了一个问题。 所以问题是我给函数一个列表,我想取另一个新的列表,用这个“新”列表再次调用函数,这样它会自动递归,形状会不停地移动

我的程序是这样的

    (define R 300)
(define L (* 2 R))
(define U (* 2.5 R))
(define MYSCN (empty-scene L U))


;Structure:
(define-struct SHAPE (type posn direction color size))
;type is a shape of the object and it can only be circle or square (Image)
;posn is the initial point of the shape is drawn (Number)
;direction can be "left right up down" and represents the direction of the shape moves (String)
;color and size are the color and size of the object (String)

;Contructors
(define pos1 (make-posn 450 50))
(define pos3 (make-posn 200 540))
(define shape1 (make-SHAPE "circle" pos1 "down" "red" 50))
(define shape2 (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170))
(define shape3 (make-SHAPE "circle" pos3 "up" "green" 100))
(define shape4 (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155))

(define shapelist1 (list shape1 shape2 shape3 shape4))

;Selectors
(posn-x pos1) ;400 (Number)
(posn-y (SHAPE-posn shape2)) ;200 (Number)
(SHAPE-type shape3) ;"circle" (Image)
(SHAPE-direction shape4) ;"left" (String)
(SHAPE-color shape1) ;"red" (String)

;Predicators
(SHAPE? shape1) ;true
(posn? shape2) ;false
(posn? pos1) ;true

;Purpose: Transferring all the shapes to the scene 
;alltheshapes shapelist1-->image
(define (alltheshapes f)
  (place-image (circle (SHAPE-size (first shapelist1)) "solid" (SHAPE-color (first shapelist1)))
               (posn-x (SHAPE-posn (first shapelist1))) (posn-y (SHAPE-posn (first shapelist1)))
               (place-image
                (square (SHAPE-size (first(rest shapelist1))) "solid" (SHAPE-color (first (rest shapelist1))))
                (posn-x (SHAPE-posn (first(rest shapelist1)))) (posn-y (SHAPE-posn (first(rest shapelist1))))
                (place-image
                 (circle (SHAPE-size (first (rest (rest shapelist1)))) "solid" (SHAPE-color (first (rest (rest shapelist1)))))
                 (posn-x (SHAPE-posn (first (rest (rest shapelist1))))) (posn-y (SHAPE-posn (first (rest (rest shapelist1)))))
                 (place-image
                  (square (SHAPE-size(first(rest(rest(rest shapelist1))))) "solid" (SHAPE-color(first(rest(rest(rest shapelist1))))))
                   (posn-x (SHAPE-posn (first(rest(rest(rest shapelist1)))))) (posn-y (SHAPE-posn (first(rest(rest(rest shapelist1))))))
                   MYSCN)))))


I couldn't figure out the recursive part here 

    ;Purpose: Change the position of the shape according to the current word
    ;Contract: moveshapes shapelist1-->creates a new shape
    
    (define (moveshapes shapelist1)
      (cond
        ((string=? (SHAPE-direction (first shapelist1)) "down") (append (list (make-SHAPE "circle" (make-posn 450 (+ 1 (posn-y pos1)))  "down" "red" 50)) (moveshape (rest shapelist1))))
        ((string=? (SHAPE-direction (first shapelist1)) "right") (append (list (make-SHAPE "square" (make-posn (+ 1 (posn-x (SHAPE-posn shape2))) 230) "right" "purple" 170)) (moveshape (rest shapelist1))))
        ((string=? (SHAPE-direction (first shapelist1)) "up")  (append (list(make-SHAPE "circle" (make-posn 200 (- (posn-y (SHAPE-posn shape3)) 1))  "up" "green" 100)) (moveshape (rest shapelist1))))
        ((string=? (SHAPE-direction (first shapelist1)) "left") (append (list(make-SHAPE "square" (make-posn (- (posn-x (SHAPE-posn shape4)) 1) 450) "left" "orange" 155))))
    

    (define (Project super)
          (big-bang super
            (to-draw alltheshapes) ;Takes the current word and produce an image
            (on-tick moveshapes))) ;It will take the shapes and create a new shape while moving them
        
        (Project shapelist1)

在进入动画部分之前,我想帮助您消除上面程序中的一些难点-

(需要2htdp/image)
; 设置我的场景
(定义我的场景(空场景600 750))
; 定义场景元素的结构
(定义结构元素(图像位置方向))
; 定义我的元素
(定义e1(使元件(圆圈50“实心”“红色”)(使posn 450 50“向下”))
(定义e2(使元素(正方形170“实心”“紫色”)(使posn 100 230“右侧”))
(定义e3(使元素(圆圈100“实心”“绿色”)(使posn 200 540“向上”))
(定义e4(使元素(正方形155“实心”“橙色”)(使posn 500 450“左侧”))
(定义我的元素(列表e1 e2 e3 e4))
; 将元素放置到场景中
(定义(放置元素场景元素)
(如果(空?元素)
场景
(放置图像(元素图像(汽车元素))
(posn-x(元素posn(汽车元素)))
(posn-y(元素posn(汽车元素)))
(放置元素场景(cdr元素(()())))
(放置元素我的场景我的元素)
这将渲染以下场景-


如果您在阅读本帖后仍停留在动画上,请让我知道,我会进一步帮助您。

非常感谢您,但我已经完成了动画部分,我删除了旧功能,因为它非常难看。创建一个新的,逐个创建形状添加功能并创建动画。感谢您的努力和时间,这是完成的版本。如果您对任何部分有任何问题,请随时问我
(define R 300)
(define L (* 2 R))
(define U (* 2.5 R))
(define MYSCN (empty-scene L U))


;Structure:
(define-struct SHAPE (type posn direction color size))
;type is a shape of the object and it can only be circle or square (Image)
;posn is the initial point of the shape that drawn (Number)
;direction can be "left right up down" and represents the direction of the shape that moves (String)
;color and size are the color and size of the object (String)

;Contructors
(define pos1 (make-posn 450 60))
(define pos3 (make-posn 160 620))
(define shape1 (make-SHAPE "circle" pos1 "down" "red" 50))
(define shape2 (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170))
(define shape3 (make-SHAPE "circle" pos3 "up" "green" 100))
(define shape4 (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155))

(define shapelist1 (list shape1 shape2 shape3 shape4))

;Selectors
(posn-x pos1) ;450 (Number)
(posn-y (SHAPE-posn shape2)) ;230 (Number)
(SHAPE-type shape3) ;"circle" (String)
(SHAPE-direction shape4) ;"left" (String)
(SHAPE-color shape1) ;"red" (String)

;Predicators
(SHAPE? shape1) ;true
(posn? shape2) ;false
(posn? pos1) ;true


;*****************************************************************************************************************************************************************************************************
;Purpose: add all the image inside of the given list to a scene
;Contract: to-draw (alltheshapes LOS -> Images)

;Example
(check-expect (alltheshapes (list shape1)) (place-image (circle 50 "solid" "red") 450 60 MYSCN))
(check-expect (alltheshapes (list shape3)) (place-image (circle 100 "solid" "green") 160 620 MYSCN))
(check-expect (alltheshapes (list shape4)) (place-image (square 155 "solid" "orange") 500 450 MYSCN))

;Function
(define (alltheshapes a-list)
  (cond
    ((empty? a-list) MYSCN)
    ((string=? (SHAPE-type (first a-list)) "circle")
     (place-image
      (circle (SHAPE-size (first a-list)) "solid" (SHAPE-color (first a-list)))
      (posn-x (SHAPE-posn (first a-list))) (posn-y (SHAPE-posn (first a-list)))
      (alltheshapes (rest a-list))))
    
    ((string=? (SHAPE-type (first a-list)) "square")
     (place-image
     (square (SHAPE-size (first a-list)) "solid" (SHAPE-color (first a-list)))
     (posn-x (SHAPE-posn (first a-list))) (posn-y (SHAPE-posn (first a-list)))
     (alltheshapes (rest a-list))))))

;Test
(alltheshapes (list shape2 shape3))
(alltheshapes (list shape1 shape3 shape4))

;*****************************************************************************************************************************************************************************************************
;Purpose: Moves the shapes every tick
;Contract: on-tick (moveshapes LOS->LOS)

;Example
(check-expect (moveshapes (list shape2)) (cons (make-SHAPE "square" (make-posn 101 230) "right" "purple" 170) '()))
(check-expect (moveshapes (list shape1)) (cons (make-SHAPE "circle" (make-posn 450 61)  "down" "red" 50) '()))
(check-expect (moveshapes (list shape4)) (cons (make-SHAPE "square" (make-posn 499 450) "left" "orange" 155) '()))

;Function
(define (moveshapes a-list)
  (cond
    ((empty? a-list) empty)
    ((string=? (SHAPE-direction (first a-list)) "down") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (posn-x (SHAPE-posn (first a-list))) (+ (posn-y (SHAPE-posn (first a-list))) 1))
                                                                          "down" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
    
    ((string=? (SHAPE-direction (first a-list)) "right")(cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (+ 1 (posn-x (SHAPE-posn (first a-list)))) (posn-y (SHAPE-posn (first a-list))))
                                                                          "right" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
    
    ((string=? (SHAPE-direction (first a-list)) "up") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (posn-x (SHAPE-posn (first a-list))) (- (posn-y (SHAPE-posn (first a-list))) 1))
                                                                        "up" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
    
    ((string=? (SHAPE-direction (first a-list)) "left") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (- (posn-x (SHAPE-posn (first a-list))) 1) (posn-y (SHAPE-posn (first a-list))))
                                                                          "left" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
    
    (else a-list)))

;Test
(moveshapes (list shape2 shape1))

;*****************************************************************************************************************************************************************************************************

;----------------------------------------------------------------------------
;Purpose: Help to on-mouse command with creating all the shapes again as a list
;Contract: LOS-> (creates the original forms of the shapes)

;Examples
(check-expect (helperonmouse (list shape2 shape3)) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
                                                         (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) '())))

(define (helperonmouse shapelist1)
  (cond
    ((empty? shapelist1) empty)
    (else (cons (make-SHAPE (SHAPE-type (first shapelist1)) (make-posn (posn-x (SHAPE-posn (first shapelist1))) (posn-y (SHAPE-posn (first shapelist1))))
                            (SHAPE-direction (first shapelist1)) (SHAPE-color (first shapelist1)) (SHAPE-size (first shapelist1))) (helperonmouse (rest shapelist1))))))

;Test
(helperonmouse (list shape1 shape4))
;----------------------------------------------------------------------------

;Purpose: Every time when you hit mouse1, the animation will restart
;Contract: on-mouse LOS -> LOS 

;Examples
(check-expect (restart (list shape2) 3 5 "button-down") (cons(make-SHAPE"circle"(make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
                                                        (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155)'())))))

(check-expect (restart (list shape4) 6 1 "button-down") (cons (make-SHAPE "circle" (make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
                                                        (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))))

(check-expect (restart (list shape1) 4 2 "button-down") (cons (make-SHAPE "circle" (make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
                                                        (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))))


;Function
(define (restart a-list x y clack)
  (cond
    ((empty? a-list) empty)
    ((mouse=? clack "button-down") (helperonmouse shapelist1))
    (else a-list)))

;Test
(restart (list shape1) 3 4 "button-down")

;*****************************************************************************************************************************************************************************************************
;Purpose: When you press the "d" button in your keyboard, one of the images will be deleted
;Contract: on-key LOS-> (- LOS 1)

;Examples
(check-expect (delete (list shape1 shape2) "d") (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170) '()))
(check-expect (delete (list shape2 shape3 shape4) "d") (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))
(check-expect (delete (list shape1) "d") '())

;Function
(define (delete a-list a-key)
  (cond
    ((empty? a-list) empty)
    ((key=? a-key "d") (rest a-list))
    (else a-list)))

;Test
(delete (list shape2 shape1) "d")

;*****************************************************************************************************************************************************************************************************
(define (Project super)
  (big-bang super
    (to-draw alltheshapes) ;Takes the current word and produce an image
    (on-tick moveshapes) ;Takes the current word and everytick changes the coordinates of the shapes 
    (on-mouse restart) ;Takes the current word and create the original current word again
    (on-key delete))) ;Takes the current word and remove one element inside of it


(Project shapelist1)