Advertisement
juaniisuar

p2-2 ej1

Apr 21st, 2016
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.16 KB | None | 0 0
  1. (define ANCHO 600)
  2. (define ALTO 600)
  3. (define DELTA 12)
  4.  
  5. (define Inicio (make-posn (/ ANCHO 2) (/ ALTO 2)))
  6. (define radiomosca 2)
  7. (define GAMMA radiomosca)
  8. (define mosca (circle radiomosca "solid" "black"))
  9. (define ESTADO-FINAL (make-posn -100 -100))
  10.  
  11. (define (pantalla p) (if (end? p) (text "MOSCA ATRAPADA" 56 "Green") (place-image mosca (posn-x p) (posn-y p) (empty-scene ANCHO ALTO)) ) )
  12. (define (dist-origen M) (sqrt (+ (* (posn-x M) (posn-x M)) (* (posn-y M) (posn-y M)))))
  13. (define (elegir-random a b) (if (= (random 2) 0) a b))
  14.  
  15. (define (checkY y d) (if (and (>= (- y d) 0) (<= (+ y d) ALTO) ) #t #f))
  16. (define (checkX x d) (if (and (>= (- x d) 0) (<= (+ x d) ANCHO) ) #t #f))
  17. (define (tecla I k) (cond ((key=? k "left") (make-posn (- (posn-x I) DELTA) (posn-y I)))
  18.                           ((key=? k "right") (make-posn (+ (posn-x I) DELTA) (posn-y I)))
  19.                           ((key=? k "up") (make-posn (posn-x I) (- (posn-y I) DELTA)))
  20.                           ((key=? k "down") (make-posn (posn-x I) (+ (posn-y I) DELTA)))
  21.                           (else I)
  22.                           ))
  23.  
  24.  
  25. (define (mousehandler I x y event) (cond ((string=? event "button-down") (if (or (<= (abs (- (posn-x I) x)) GAMMA) (<= (abs (- (posn-y I) y)) GAMMA)) ESTADO-FINAL I ))
  26.                                          (else I)
  27.                                          ))
  28.  
  29. (define (randmov I) (if (elegir-random #t #f) I ;#t -> stay, #f -> move
  30.                           (if (elegir-random #t #f) ;#t -> vert, #f -> horiz
  31.                                  (if (elegir-random #t #f) ;#t -> up, #f -> down
  32.                                         (make-posn (posn-x I) (- (posn-y I) DELTA )) (make-posn (posn-x I) (+ (posn-y I) DELTA )) )
  33.                                      (if (elegir-random #t #f) ;#t -> left, #f -> r8
  34.                                         (make-posn (- (posn-x I) DELTA) (posn-y I)) (make-posn (+ (posn-x I) DELTA) (posn-y I) )))))
  35.  
  36. (define (end? I) (if (= (posn-x I) -100) #t #f))
  37.  
  38. (big-bang Inicio
  39.           (to-draw pantalla)
  40.           (on-tick randmov)
  41.           (on-mouse mousehandler)
  42.           (on-key tecla)
  43.           (stop-when end?)
  44.           )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement