Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. (require picturing-programs)
  2.  
  3.  
  4. ;;Cat and Mouse
  5. (define CAT-SIZE 20)
  6. (define CAT (circle CAT-SIZE "solid" "black"))
  7. (define MOUSE-SIZE 10)
  8. (define MOUSE (circle MOUSE-SIZE "solid" "gray"))
  9. (define HEIGHT 400)
  10. (define WIDTH 400)
  11.  
  12.  
  13. ;; A cat is
  14. ;; (make-cat posn string)
  15. (define-struct cat (loc dir))
  16.  
  17. ;;draw-cat: cat scene -> image
  18. ;;consumes a cat and a scene and produces a picture of it
  19. (define (draw-cat a-CAT a-scene)
  20. (place-image CAT (posn-x (cat-loc a-CAT))(posn-y (cat-loc a-CAT)) a-scene))
  21.  
  22. ;; A mouse is:
  23. ;; (make-mouse posn number number)
  24. (define-struct mouse (loc dx dy))
  25.  
  26. ;;draw-mouse: mouse scene -> image
  27. ;;consumes a mouse and a scene and produces an image of it
  28. (define (draw-mouse a-MOUSE a-scene)
  29. (place-image MOUSE (posn-x (mouse-loc a-MOUSE))(posn-y (mouse-loc a-MOUSE)) a-scene))
  30.  
  31. ;; A world is:
  32. ;; (make-world cat mouse)
  33. (define-struct world (cat mouse))
  34.  
  35. ;;draw-world: cat mouse -> image
  36. ;;consumes world and produces an image of it
  37. (define (draw-world a-WORLD)
  38. (draw-mouse (world-mouse a-WORLD)
  39. (draw-cat (world-cat a-WORLD)
  40. (empty-scene WIDTH HEIGHT))))
  41.  
  42. ;;move-cat: cat -> cat
  43. ;;consusmes a cat an moves it two pixels in the right direction
  44. (define (move-cat a-CAT)
  45. (cond [(string=? "w" (cat-dir a-CAT))
  46. (make-cat (make-posn (posn-x (cat-loc a-CAT)) (- (posn-y (cat-loc a-CAT))6)) "w")]
  47. [(string=? "s" (cat-dir a-CAT))
  48. (make-cat (make-posn (posn-x (cat-loc a-CAT)) (+ (posn-y (cat-loc a-CAT))6)) "s")]
  49. [(string=? "a" (cat-dir a-CAT))
  50. (make-cat (make-posn (- (posn-x (cat-loc a-CAT))6) (+ (posn-y (cat-loc a-CAT))0)) "a")]
  51. [(string=? "d" (cat-dir a-CAT))
  52. (make-cat (make-posn (+ (posn-x (cat-loc a-CAT))6) (+ (posn-y (cat-loc a-CAT))0)) "d")]
  53. [else a-CAT]))
  54.  
  55.  
  56.  
  57. ;;move-mouse: mouse->mouse
  58. ;;consumes a mouse and moves it in the right direction
  59. (define (move-mouse a-MOUSE)
  60. (make-mouse (make-posn (+ (posn-x (mouse-loc a-MOUSE)) (mouse-dx a-MOUSE))
  61. (+ (posn-y (mouse-loc a-MOUSE)) (mouse-dy a-MOUSE)))
  62. (+(- (random 3) 1) (mouse-dx a-MOUSE))
  63. (+(- (random 3) 1) (mouse-dy a-MOUSE))))
  64.  
  65. ;;keep-cat-on-screen: cat -> cat
  66. ;;consumes a cat and keeps it on the screen
  67. (define (keep-cat-on-screen a-CAT)
  68. (make-cat (make-posn (cond [(< (posn-x (cat-loc a-CAT)) CAT-SIZE)
  69. CAT-SIZE]
  70. [(> (posn-x (cat-loc a-CAT)) (- WIDTH CAT-SIZE))
  71. (- WIDTH CAT-SIZE)]
  72. [else (posn-x (cat-loc a-CAT))])
  73. (cond [(< (posn-y (cat-loc a-CAT)) CAT-SIZE)
  74. CAT-SIZE]
  75. [(> (posn-y (cat-loc a-CAT)) (- HEIGHT CAT-SIZE))
  76. (- HEIGHT CAT-SIZE)]
  77. [else (posn-y (cat-loc a-CAT))]))
  78. (cat-dir a-CAT)))
  79.  
  80.  
  81. ;;wrap-screen-cat: cat -> cat
  82. ;;moves a cat to the other side of the screen if it goes over
  83. (define (wrap-screen-cat a-CAT)
  84. (make-cat (make-posn (cond [(< (posn-x (cat-loc a-CAT)) 0)
  85. WIDTH]
  86. [(> (posn-x (cat-loc a-CAT)) WIDTH)
  87. 0]
  88. [else (posn-x (cat-loc a-CAT))])
  89. (cond [(< (posn-y (cat-loc a-CAT)) 0)
  90. HEIGHT]
  91. [(> (posn-y (cat-loc a-CAT)) HEIGHT)
  92. 0]
  93. [else (posn-y (cat-loc a-CAT))]))
  94. (cat-dir a-CAT)))
  95.  
  96. ;;wrap-screen-mouse: mouse-> mouse
  97. ;;moves a mouse to the other side of the screen if it goes over
  98. (define (wrap-screen-mouse a-MOUSE)
  99. (make-mouse (make-posn (cond [(< (posn-x (mouse-loc a-MOUSE)) 0)
  100. WIDTH]
  101. [(> (posn-x (mouse-loc a-MOUSE)) WIDTH)
  102. 0]
  103. [else (posn-x (mouse-loc a-MOUSE))])
  104.  
  105. (cond [(< (posn-y(mouse-loc a-MOUSE)) 0)
  106. HEIGHT]
  107. [(> (posn-y (mouse-loc a-MOUSE)) HEIGHT)
  108. 0]
  109. [else (posn-y (mouse-loc a-MOUSE))]))
  110. (mouse-dx a-MOUSE)
  111. (mouse-dy a-MOUSE)))
  112.  
  113.  
  114.  
  115. ;;new-mouse-dx: mouse -> number
  116. ;;helper function for keep mouse on screen, produces a new dx for a given mouse
  117. (define (new-mouse-dx a-MOUSE)
  118. (cond [(or(< (posn-x (mouse-loc a-MOUSE)) MOUSE-SIZE)
  119. (> (posn-x (mouse-loc a-MOUSE)) (- WIDTH MOUSE-SIZE)))
  120. (* -1 (mouse-dx a-MOUSE))]
  121. [else (mouse-dx a-MOUSE)]))
  122.  
  123. ;;new-mouse-dy: mouse -> number
  124. ;;mimics new-mouse-dx for y-axis
  125. (define (new-mouse-dy a-MOUSE)
  126. (cond [(or(< (posn-y (mouse-loc a-MOUSE)) MOUSE-SIZE)
  127. (> (posn-y (mouse-loc a-MOUSE)) (- HEIGHT MOUSE-SIZE)))
  128. (* -1 (mouse-dy a-MOUSE))]
  129. [else (mouse-dy a-MOUSE)]))
  130.  
  131. ;;new-mouse-loc: mouse -> posn
  132. ;;consumes a mouse and, given the mouse moved off the screen, moves it to a new location
  133. (define (new-mouse-loc a-MOUSE)
  134. (make-posn (cond [(< (posn-x (mouse-loc a-MOUSE)) MOUSE-SIZE)
  135. (- (* 2 MOUSE-SIZE) (posn-x (mouse-loc a-MOUSE)))]
  136. [(> (posn-x (mouse-loc a-MOUSE)) (- WIDTH MOUSE-SIZE))
  137. (- (* 2 (- WIDTH MOUSE-SIZE)) (posn-x (mouse-loc a-MOUSE)))]
  138. [else (posn-x (mouse-loc a-MOUSE))])
  139. (cond [(< (posn-y (mouse-loc a-MOUSE)) MOUSE-SIZE)
  140. (- (* 2 MOUSE-SIZE) (posn-y (mouse-loc a-MOUSE)))]
  141. [(> (posn-y (mouse-loc a-MOUSE)) (- HEIGHT MOUSE-SIZE))
  142. (- (* 2 (- HEIGHT MOUSE-SIZE)) (posn-y (mouse-loc a-MOUSE)))]
  143. [else (posn-y (mouse-loc a-MOUSE))])))
  144.  
  145. ;;keep-mouse-on-screen: mouse -> mouse
  146. ;;consumes a mouse and make sure it stays on the screen
  147. (define (keep-mouse-on-screen a-MOUSE)
  148. (make-mouse (new-mouse-loc a-MOUSE)
  149. (new-mouse-dx a-MOUSE)
  150. (new-mouse-dy a-MOUSE)))
  151.  
  152.  
  153.  
  154. ;;move-world-mouseAI: world -> world
  155. ;;takes a world and moves the mouse actively away from the cat
  156. (define (move-world-mouseAI a-WORLD)
  157. (make-world (wrap-screen-cat (move-cat (world-cat a-WORLD)))
  158. (wrap-screen-mouse (make-mouse
  159. (make-posn (cond [(string=? (cat-dir (world-cat a-WORLD)) "a")
  160. (-(posn-x(mouse-loc(world-mouse a-WORLD)))(random 10))]
  161. [(string=? (cat-dir (world-cat a-WORLD)) "d")
  162. (+ (random 10)(posn-x(mouse-loc(world-mouse a-WORLD))))]
  163. [else (+ (mouse-dx (world-mouse a-WORLD))(posn-x(mouse-loc(world-mouse a-WORLD))))])
  164.  
  165. (cond [(string=? (cat-dir (world-cat a-WORLD)) "w")
  166. (- (posn-y(mouse-loc(world-mouse a-WORLD)))(random 10))]
  167. [(string=? (cat-dir (world-cat a-WORLD)) "s")
  168. (+ (random 10) (posn-y (mouse-loc (world-mouse a-WORLD))))]
  169. [else (+ (mouse-dy (world-mouse a-WORLD))(posn-y(mouse-loc(world-mouse a-WORLD))))]))
  170. (mouse-dx (world-mouse a-WORLD))
  171. (mouse-dx (world-mouse a-WORLD))))))
  172.  
  173.  
  174.  
  175.  
  176.  
  177. ;;move-world: world -> world
  178. ;;consumes a world and moves both the cat and mouse in it
  179. (define (move-world a-WORLD)
  180. (make-world (keep-cat-on-screen (move-cat (world-cat a-WORLD)))
  181. (keep-mouse-on-screen (move-mouse (world-mouse a-WORLD)))))
  182.  
  183.  
  184.  
  185. ;;move-world-wrap: world -> world
  186. ;;consumes a world and moves both the cat and mouse in it
  187. (define (move-world-wrap a-WORLD)
  188. (make-world (wrap-screen-cat (move-cat (world-cat a-WORLD)))
  189. (wrap-screen-mouse (move-mouse (world-mouse a-WORLD)))))
  190.  
  191.  
  192.  
  193. ;;change-cat-direction: world key-handler -> world
  194. ;;changes the cat in the world to a set direction
  195. (define (change-cat-direction a-WORLD n-dir)
  196. (make-world (make-cat (make-posn (posn-x (cat-loc (world-cat a-WORLD)))
  197. (posn-y (cat-loc (world-cat a-WORLD))))
  198. n-dir)
  199. (world-mouse a-WORLD)))
  200.  
  201. ;;cat-caught-mouse?: world -> boolean
  202. ;;tells if cat is close enough to eat mouse
  203. (define (cat-caught-mouse? a-world)
  204. (and (< (abs(- (posn-x (cat-loc (world-cat a-world)))
  205. (posn-x (mouse-loc (world-mouse a-world))))) 28)
  206. (< (abs (- (posn-y (cat-loc (world-cat a-world)))
  207. (posn-y (mouse-loc (world-mouse a-world))))) 28)))
  208.  
  209.  
  210. (define (next-level score)
  211. (display (string-append "Your score is " (number->string score)". Would you like to continue?"))
  212. (define ans (read))
  213. (cond [(string=? ans "N")
  214. (display "Oh well, come back next time.")]
  215. [(string=? ans "Y")
  216. (display "Let's go!")
  217. (define lvl1 (big-bang (make-world (make-cat (make-posn 300 300) "w")
  218. (make-mouse (make-posn (random 75)(random 75)) 4 -3))
  219. (on-draw draw-world)
  220. (on-tick move-world-mouseAI 0.05)
  221. (on-key change-cat-direction)
  222. (stop-when cat-caught-mouse?)))
  223. (define nscore (+ score 1))
  224. (next-level nscore)]
  225. [else (display "Bye now.")]))
  226.  
  227. (define (keep-score)
  228. (display "Would you like to play 'Cat and Mouse'? Y/N")
  229. (define ans (read))
  230. (cond [(string=? ans "N")
  231. (display "Oh well, come back next time.")]
  232. [(string=? ans "Y")
  233. (display "Let's go!")
  234. (display "You use the WSAD to move your cat around to catch the mouse. Good luck!")
  235. (sleep 3)
  236. (define lvl1 (big-bang (make-world (make-cat (make-posn 100 100) "w")
  237. (make-mouse (make-posn 20 20) 4 -3))
  238. (on-draw draw-world)
  239. (on-tick move-world-mouseAI 0.05)
  240. (on-key change-cat-direction)
  241. (stop-when cat-caught-mouse?)))
  242. (define score 1)
  243. (next-level 1)]
  244. [else (keep-score)]))
Add Comment
Please, Sign In to add comment