Advertisement
Guest User

Untitled

a guest
May 8th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.75 KB | None | 0 0
  1. #lang racket
  2. ;--------------------------------- IMPORT FUNCTIONS
  3. (require 2htdp/universe)
  4. (require 2htdp/image)
  5. (require lang/posn)
  6.  
  7. ;---------------------------------- DATA DEFINITIONS
  8.  
  9. (define-struct cell (x y direction))
  10. (define-struct tail_cell (x y cell))
  11. (define-struct fruit (x y))
  12. (define-struct tail (list tail_cell))
  13. (define-struct snake (cell))
  14. (define-struct data (colides alive WIDTH HEIGHT SCORE snake speed fruit screen))
  15.  
  16.  
  17. ;----------------------------------DEFINE GETTERS AND SETTERS
  18.  
  19. ;----------------------------------GETTERS
  20. (define (get_x world)
  21.   (cell-x (snake-cell (data-snake world))))
  22. (define (get_y world)
  23.   (cell-y (snake-cell (data-snake world))))
  24. (define (get_fruit_x world)
  25.   (fruit-x (data-fruit world)))
  26. (define (get_fruit_y world)
  27.   (fruit-y (data-fruit world)))
  28. (define (get_colides world)
  29.   (data-colides world))
  30. (define (get_alive world)
  31.   (data-alive world))
  32. (define (screen_width world)
  33.   (data-WIDTH world))
  34. (define (screen_height world)
  35.   (data-HEIGHT world))
  36. (define (get_score world)
  37.   (data-SCORE world))
  38. (define (get_speed world)
  39.   (data-speed world))
  40. (define (get_fruit world)
  41.   (data-fruit world))
  42. (define (get_head world)
  43.   (snake-cell (data-snake world)))
  44. (define (get_direction world)
  45.   (cell-direction (snake-cell (data-snake world))))
  46. (define (get_snake world)
  47.   (data-snake world))
  48.  
  49. ;--------------------------------SETTERS
  50.  
  51. ; x = x pos / y =  y pos / c = cell
  52. (define (set_cell x y d c)
  53.   (make-cell (+ (cell-x c) x) (+ (cell-y c) y) d))
  54.  
  55. ;world = data / i = number to change
  56. (define (make_snake world x y c)
  57.   (make-snake (set_cell x y c (get_head world))))
  58.  
  59. (define (set_x world i)
  60.   (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
  61.              (make_snake world i 0 (get_direction world))
  62.              (get_speed world) (get_fruit world) (data-screen world)))
  63.  
  64. (define (set_y world i)
  65.   (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
  66.              (make_snake world 0 i (get_direction world))
  67.              (get_speed world) (get_fruit world) (data-screen world)))
  68.  
  69. (define (get_data world)
  70.   (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
  71.              (get_snake world)
  72.              (get_speed world) (get_fruit world) (data-screen world)))
  73.  
  74. (define (set_colides world i)
  75.   (make-data i (get_alive world) (screen_width world) (screen_height world) (get_score world)
  76.              (get_snake world)
  77.              (get_speed world) (get_fruit world) (data-screen world)))
  78.  
  79. (define (fruit_random world)
  80.   (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
  81.              (get_snake world)
  82.              (get_speed world) (make-fruit (random (screen_width world)) (random (screen_height world))) (data-screen world)))
  83.  
  84. (define (set_direction world i)
  85.   (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
  86.              (make_snake world 0 0 i)
  87.              (get_speed world) (get_fruit world) (data-screen world)))
  88. (define (make_new_data)
  89.   (make-data #t #t 800 600 0 (make-snake (make-cell 400 300 6)) 6 (make-fruit (random 800) (random 600))
  90.                 (rectangle 800 600 "solid" "black")))
  91.  
  92.  
  93. ;----------------------------------MATH
  94. ; i value to add to position to check if is inside boundaries
  95. (define (x_inside world i)
  96.   (and (> (- (get_x world) i) 0) (< (+ (get_x world) i) (screen_width world))))
  97. (define (y_inside world i)
  98.   (and (> (- (get_y world) i) 0) (< (+ (get_y world) i) (screen_height world))))
  99.  
  100. ;-*************************************************************************** THIS IS THE CODE THAT DOESNT WORK
  101. ; every condition of the and works when use in isolation, but when uncomented more than one, it doesnt work
  102. (define (inside_fruit world)
  103.   (and
  104.    ;(> (- (get_fruit_x world) 32) (get_x world))
  105.    ;(< (+ (get_fruit_x world) 32) (get_x world))
  106.    ;(> (- (get_fruit_y world) 32) (get_y world))
  107.    ;(< (+ (get_fruit_y world) 32) (get_y world))
  108.    ))
  109.  
  110. ; ----------------------------------- START OF CODE
  111.  
  112.  
  113. (define (draw world)
  114.   (
  115.    place-images
  116.    (list (square 32 "solid" "white")
  117.          (square 32 "solid" "blue"))
  118.    (list (make-posn (get_x world) (get_y world))
  119.          (make-posn (get_fruit_x world) (get_fruit_y world)))
  120.    (data-screen world)
  121.   ))
  122.  
  123.  
  124. (define (tick_snake world)
  125.   (
  126.   cond
  127.    [(= 6 (get_direction world)) (if (x_inside world (get_speed world)) (set_x world (get_speed world)) (get_data world))]
  128.    [(= 4 (get_direction world)) (if (x_inside world (get_speed world)) (set_x world (- (get_speed world))) (get_data world))]
  129.    [(= 8 (get_direction world)) (if (y_inside world (get_speed world)) (set_y world (get_speed world)) (get_data world))]
  130.    [(= 2 (get_direction world)) (if (y_inside world (get_speed world)) (set_y world (- (get_speed world)))(get_data world))]
  131.    [else (get_data world)]
  132.    ))
  133.  
  134. (define (collides world) (if (inside_fruit world) (set_colides world #t) (set_colides world #f)))          
  135.  
  136. (define (colision world) (if (get_colides world) (fruit_random world) (get_data world)))
  137.  
  138. (define (tick world) (tick_snake (colision (collides world))))
  139.  
  140. (define (input world key)
  141.   (
  142.   cond
  143.    [(equal? key "left") (set_direction world 4)]
  144.    [(equal? key "right") (set_direction world 6)]
  145.    [(equal? key "up") (set_direction world 2)]
  146.    [(equal? key "down") (set_direction world 8)]
  147.    [(equal? key "q") (make_new_data)]
  148.    [else (set_direction world (get_direction world))]
  149.    ))
  150.  
  151. (big-bang (make_new_data)
  152.           (on-tick tick)
  153.           (on-key input)
  154.           (on-draw draw)
  155.           (name "Snake")
  156.           )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement