predator-prey.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. (import
  2. (scheme base)
  3. (scheme read)
  4. (scheme write)
  5. (scheme load)
  6. (srfi 27)
  7. (yasos)
  8. ;; (macduffie alsos)
  9. (turtle turtle3)
  10. (turtle vector))
  11. (include "./animal.scm")
  12. (define (make-prey starting-pos)
  13. (define t (make-hungry-turtle))
  14. (hide! t)
  15. (pen-up! t)
  16. (set-pos! t starting-pos)
  17. (pen-down! t)
  18. (show! t)
  19. (set-food! t (lambda (turt) #(-20 -20)))
  20. ; (set-strategy! t (make-strategy-random 5 10 -10 5))
  21. ; (set-strategy! t (make-strategy-smell-3 2 6 20 10))
  22. ; (set-strategy! t (make-strategy-circle 10 20))
  23. (set-strategy! t (make-strategy-direct 10))
  24. t)
  25. (define (make-predator starting-pos the-prey)
  26. (define t (make-hungry-turtle))
  27. (hide! t)
  28. (pen-up! t)
  29. (set-pos! t starting-pos)
  30. (pen-down! t)
  31. (show! t)
  32. (set-food! t (lambda (turt) (get-pos the-prey)))
  33. (set-strategy! t (make-strategy-smell-3 2 6 20 10))
  34. t)
  35. (define max-delta 200)
  36. (define (execute-together pred-proc prey-proc)
  37. (let loop ((i 0))
  38. (when (< i max-delta)
  39. (pred-proc)
  40. (prey-proc)
  41. (loop (+ i 1)))))
  42. (define (main)
  43. (define t1 (make-prey #(100 100)))
  44. (define t2 (make-predator #(-50 -50) t1))
  45. (clear-screen)
  46. (execute-together
  47. (lambda () (step! t1 1 1))
  48. (lambda () (step! t2 3 1))))