Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require picturing-programs)
- ;;Cat and Mouse
- (define CAT-SIZE 20)
- (define CAT (circle CAT-SIZE "solid" "black"))
- (define MOUSE-SIZE 10)
- (define MOUSE (circle MOUSE-SIZE "solid" "gray"))
- (define HEIGHT 400)
- (define WIDTH 400)
- ;; A cat is
- ;; (make-cat posn string)
- (define-struct cat (loc dir))
- ;;draw-cat: cat scene -> image
- ;;consumes a cat and a scene and produces a picture of it
- (define (draw-cat a-CAT a-scene)
- (place-image CAT (posn-x (cat-loc a-CAT))(posn-y (cat-loc a-CAT)) a-scene))
- ;; A mouse is:
- ;; (make-mouse posn number number)
- (define-struct mouse (loc dx dy))
- ;;draw-mouse: mouse scene -> image
- ;;consumes a mouse and a scene and produces an image of it
- (define (draw-mouse a-MOUSE a-scene)
- (place-image MOUSE (posn-x (mouse-loc a-MOUSE))(posn-y (mouse-loc a-MOUSE)) a-scene))
- ;; A world is:
- ;; (make-world cat mouse)
- (define-struct world (cat mouse))
- ;;draw-world: cat mouse -> image
- ;;consumes world and produces an image of it
- (define (draw-world a-WORLD)
- (draw-mouse (world-mouse a-WORLD)
- (draw-cat (world-cat a-WORLD)
- (empty-scene WIDTH HEIGHT))))
- ;;move-cat: cat -> cat
- ;;consusmes a cat an moves it two pixels in the right direction
- (define (move-cat a-CAT)
- (cond [(string=? "w" (cat-dir a-CAT))
- (make-cat (make-posn (posn-x (cat-loc a-CAT)) (- (posn-y (cat-loc a-CAT))6)) "w")]
- [(string=? "s" (cat-dir a-CAT))
- (make-cat (make-posn (posn-x (cat-loc a-CAT)) (+ (posn-y (cat-loc a-CAT))6)) "s")]
- [(string=? "a" (cat-dir a-CAT))
- (make-cat (make-posn (- (posn-x (cat-loc a-CAT))6) (+ (posn-y (cat-loc a-CAT))0)) "a")]
- [(string=? "d" (cat-dir a-CAT))
- (make-cat (make-posn (+ (posn-x (cat-loc a-CAT))6) (+ (posn-y (cat-loc a-CAT))0)) "d")]
- [else a-CAT]))
- ;;move-mouse: mouse->mouse
- ;;consumes a mouse and moves it in the right direction
- (define (move-mouse a-MOUSE)
- (make-mouse (make-posn (+ (posn-x (mouse-loc a-MOUSE)) (mouse-dx a-MOUSE))
- (+ (posn-y (mouse-loc a-MOUSE)) (mouse-dy a-MOUSE)))
- (+(- (random 3) 1) (mouse-dx a-MOUSE))
- (+(- (random 3) 1) (mouse-dy a-MOUSE))))
- ;;keep-cat-on-screen: cat -> cat
- ;;consumes a cat and keeps it on the screen
- (define (keep-cat-on-screen a-CAT)
- (make-cat (make-posn (cond [(< (posn-x (cat-loc a-CAT)) CAT-SIZE)
- CAT-SIZE]
- [(> (posn-x (cat-loc a-CAT)) (- WIDTH CAT-SIZE))
- (- WIDTH CAT-SIZE)]
- [else (posn-x (cat-loc a-CAT))])
- (cond [(< (posn-y (cat-loc a-CAT)) CAT-SIZE)
- CAT-SIZE]
- [(> (posn-y (cat-loc a-CAT)) (- HEIGHT CAT-SIZE))
- (- HEIGHT CAT-SIZE)]
- [else (posn-y (cat-loc a-CAT))]))
- (cat-dir a-CAT)))
- ;;wrap-screen-cat: cat -> cat
- ;;moves a cat to the other side of the screen if it goes over
- (define (wrap-screen-cat a-CAT)
- (make-cat (make-posn (cond [(< (posn-x (cat-loc a-CAT)) 0)
- WIDTH]
- [(> (posn-x (cat-loc a-CAT)) WIDTH)
- 0]
- [else (posn-x (cat-loc a-CAT))])
- (cond [(< (posn-y (cat-loc a-CAT)) 0)
- HEIGHT]
- [(> (posn-y (cat-loc a-CAT)) HEIGHT)
- 0]
- [else (posn-y (cat-loc a-CAT))]))
- (cat-dir a-CAT)))
- ;;wrap-screen-mouse: mouse-> mouse
- ;;moves a mouse to the other side of the screen if it goes over
- (define (wrap-screen-mouse a-MOUSE)
- (make-mouse (make-posn (cond [(< (posn-x (mouse-loc a-MOUSE)) 0)
- WIDTH]
- [(> (posn-x (mouse-loc a-MOUSE)) WIDTH)
- 0]
- [else (posn-x (mouse-loc a-MOUSE))])
- (cond [(< (posn-y(mouse-loc a-MOUSE)) 0)
- HEIGHT]
- [(> (posn-y (mouse-loc a-MOUSE)) HEIGHT)
- 0]
- [else (posn-y (mouse-loc a-MOUSE))]))
- (mouse-dx a-MOUSE)
- (mouse-dy a-MOUSE)))
- ;;new-mouse-dx: mouse -> number
- ;;helper function for keep mouse on screen, produces a new dx for a given mouse
- (define (new-mouse-dx a-MOUSE)
- (cond [(or(< (posn-x (mouse-loc a-MOUSE)) MOUSE-SIZE)
- (> (posn-x (mouse-loc a-MOUSE)) (- WIDTH MOUSE-SIZE)))
- (* -1 (mouse-dx a-MOUSE))]
- [else (mouse-dx a-MOUSE)]))
- ;;new-mouse-dy: mouse -> number
- ;;mimics new-mouse-dx for y-axis
- (define (new-mouse-dy a-MOUSE)
- (cond [(or(< (posn-y (mouse-loc a-MOUSE)) MOUSE-SIZE)
- (> (posn-y (mouse-loc a-MOUSE)) (- HEIGHT MOUSE-SIZE)))
- (* -1 (mouse-dy a-MOUSE))]
- [else (mouse-dy a-MOUSE)]))
- ;;new-mouse-loc: mouse -> posn
- ;;consumes a mouse and, given the mouse moved off the screen, moves it to a new location
- (define (new-mouse-loc a-MOUSE)
- (make-posn (cond [(< (posn-x (mouse-loc a-MOUSE)) MOUSE-SIZE)
- (- (* 2 MOUSE-SIZE) (posn-x (mouse-loc a-MOUSE)))]
- [(> (posn-x (mouse-loc a-MOUSE)) (- WIDTH MOUSE-SIZE))
- (- (* 2 (- WIDTH MOUSE-SIZE)) (posn-x (mouse-loc a-MOUSE)))]
- [else (posn-x (mouse-loc a-MOUSE))])
- (cond [(< (posn-y (mouse-loc a-MOUSE)) MOUSE-SIZE)
- (- (* 2 MOUSE-SIZE) (posn-y (mouse-loc a-MOUSE)))]
- [(> (posn-y (mouse-loc a-MOUSE)) (- HEIGHT MOUSE-SIZE))
- (- (* 2 (- HEIGHT MOUSE-SIZE)) (posn-y (mouse-loc a-MOUSE)))]
- [else (posn-y (mouse-loc a-MOUSE))])))
- ;;keep-mouse-on-screen: mouse -> mouse
- ;;consumes a mouse and make sure it stays on the screen
- (define (keep-mouse-on-screen a-MOUSE)
- (make-mouse (new-mouse-loc a-MOUSE)
- (new-mouse-dx a-MOUSE)
- (new-mouse-dy a-MOUSE)))
- ;;move-world-mouseAI: world -> world
- ;;takes a world and moves the mouse actively away from the cat
- (define (move-world-mouseAI a-WORLD)
- (make-world (wrap-screen-cat (move-cat (world-cat a-WORLD)))
- (wrap-screen-mouse (make-mouse
- (make-posn (cond [(string=? (cat-dir (world-cat a-WORLD)) "a")
- (-(posn-x(mouse-loc(world-mouse a-WORLD)))(random 10))]
- [(string=? (cat-dir (world-cat a-WORLD)) "d")
- (+ (random 10)(posn-x(mouse-loc(world-mouse a-WORLD))))]
- [else (+ (mouse-dx (world-mouse a-WORLD))(posn-x(mouse-loc(world-mouse a-WORLD))))])
- (cond [(string=? (cat-dir (world-cat a-WORLD)) "w")
- (- (posn-y(mouse-loc(world-mouse a-WORLD)))(random 10))]
- [(string=? (cat-dir (world-cat a-WORLD)) "s")
- (+ (random 10) (posn-y (mouse-loc (world-mouse a-WORLD))))]
- [else (+ (mouse-dy (world-mouse a-WORLD))(posn-y(mouse-loc(world-mouse a-WORLD))))]))
- (mouse-dx (world-mouse a-WORLD))
- (mouse-dx (world-mouse a-WORLD))))))
- ;;move-world: world -> world
- ;;consumes a world and moves both the cat and mouse in it
- (define (move-world a-WORLD)
- (make-world (keep-cat-on-screen (move-cat (world-cat a-WORLD)))
- (keep-mouse-on-screen (move-mouse (world-mouse a-WORLD)))))
- ;;move-world-wrap: world -> world
- ;;consumes a world and moves both the cat and mouse in it
- (define (move-world-wrap a-WORLD)
- (make-world (wrap-screen-cat (move-cat (world-cat a-WORLD)))
- (wrap-screen-mouse (move-mouse (world-mouse a-WORLD)))))
- ;;change-cat-direction: world key-handler -> world
- ;;changes the cat in the world to a set direction
- (define (change-cat-direction a-WORLD n-dir)
- (make-world (make-cat (make-posn (posn-x (cat-loc (world-cat a-WORLD)))
- (posn-y (cat-loc (world-cat a-WORLD))))
- n-dir)
- (world-mouse a-WORLD)))
- ;;cat-caught-mouse?: world -> boolean
- ;;tells if cat is close enough to eat mouse
- (define (cat-caught-mouse? a-world)
- (and (< (abs(- (posn-x (cat-loc (world-cat a-world)))
- (posn-x (mouse-loc (world-mouse a-world))))) 28)
- (< (abs (- (posn-y (cat-loc (world-cat a-world)))
- (posn-y (mouse-loc (world-mouse a-world))))) 28)))
- (define (next-level score)
- (display (string-append "Your score is " (number->string score)". Would you like to continue?"))
- (define ans (read))
- (cond [(string=? ans "N")
- (display "Oh well, come back next time.")]
- [(string=? ans "Y")
- (display "Let's go!")
- (define lvl1 (big-bang (make-world (make-cat (make-posn 300 300) "w")
- (make-mouse (make-posn (random 75)(random 75)) 4 -3))
- (on-draw draw-world)
- (on-tick move-world-mouseAI 0.05)
- (on-key change-cat-direction)
- (stop-when cat-caught-mouse?)))
- (define nscore (+ score 1))
- (next-level nscore)]
- [else (display "Bye now.")]))
- (define (keep-score)
- (display "Would you like to play 'Cat and Mouse'? Y/N")
- (define ans (read))
- (cond [(string=? ans "N")
- (display "Oh well, come back next time.")]
- [(string=? ans "Y")
- (display "Let's go!")
- (display "You use the WSAD to move your cat around to catch the mouse. Good luck!")
- (sleep 3)
- (define lvl1 (big-bang (make-world (make-cat (make-posn 100 100) "w")
- (make-mouse (make-posn 20 20) 4 -3))
- (on-draw draw-world)
- (on-tick move-world-mouseAI 0.05)
- (on-key change-cat-direction)
- (stop-when cat-caught-mouse?)))
- (define score 1)
- (next-level 1)]
- [else (keep-score)]))
Add Comment
Please, Sign In to add comment