Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- ;--------------------------------- IMPORT FUNCTIONS
- (require 2htdp/universe)
- (require 2htdp/image)
- (require lang/posn)
- ;---------------------------------- DATA DEFINITIONS
- (define-struct cell (x y direction))
- (define-struct tail_cell (x y cell))
- (define-struct fruit (x y))
- (define-struct tail (list tail_cell))
- (define-struct snake (cell))
- (define-struct data (colides alive WIDTH HEIGHT SCORE snake speed fruit screen))
- ;----------------------------------DEFINE GETTERS AND SETTERS
- ;----------------------------------GETTERS
- (define (get_x world)
- (cell-x (snake-cell (data-snake world))))
- (define (get_y world)
- (cell-y (snake-cell (data-snake world))))
- (define (get_fruit_x world)
- (fruit-x (data-fruit world)))
- (define (get_fruit_y world)
- (fruit-y (data-fruit world)))
- (define (get_colides world)
- (data-colides world))
- (define (get_alive world)
- (data-alive world))
- (define (screen_width world)
- (data-WIDTH world))
- (define (screen_height world)
- (data-HEIGHT world))
- (define (get_score world)
- (data-SCORE world))
- (define (get_speed world)
- (data-speed world))
- (define (get_fruit world)
- (data-fruit world))
- (define (get_head world)
- (snake-cell (data-snake world)))
- (define (get_direction world)
- (cell-direction (snake-cell (data-snake world))))
- (define (get_snake world)
- (data-snake world))
- ;--------------------------------SETTERS
- ; x = x pos / y = y pos / c = cell
- (define (set_cell x y d c)
- (make-cell (+ (cell-x c) x) (+ (cell-y c) y) d))
- ;world = data / i = number to change
- (define (make_snake world x y c)
- (make-snake (set_cell x y c (get_head world))))
- (define (set_x world i)
- (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (make_snake world i 0 (get_direction world))
- (get_speed world) (get_fruit world) (data-screen world)))
- (define (set_y world i)
- (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (make_snake world 0 i (get_direction world))
- (get_speed world) (get_fruit world) (data-screen world)))
- (define (get_data world)
- (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (get_snake world)
- (get_speed world) (get_fruit world) (data-screen world)))
- (define (set_colides world i)
- (make-data i (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (get_snake world)
- (get_speed world) (get_fruit world) (data-screen world)))
- (define (fruit_random world)
- (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (get_snake world)
- (get_speed world) (make-fruit (random (screen_width world)) (random (screen_height world))) (data-screen world)))
- (define (set_direction world i)
- (make-data (get_colides world) (get_alive world) (screen_width world) (screen_height world) (get_score world)
- (make_snake world 0 0 i)
- (get_speed world) (get_fruit world) (data-screen world)))
- (define (make_new_data)
- (make-data #t #t 800 600 0 (make-snake (make-cell 400 300 6)) 6 (make-fruit (random 800) (random 600))
- (rectangle 800 600 "solid" "black")))
- ;----------------------------------MATH
- ; i value to add to position to check if is inside boundaries
- (define (x_inside world i)
- (and (> (- (get_x world) i) 0) (< (+ (get_x world) i) (screen_width world))))
- (define (y_inside world i)
- (and (> (- (get_y world) i) 0) (< (+ (get_y world) i) (screen_height world))))
- ;-*************************************************************************** THIS IS THE CODE THAT DOESNT WORK
- ; every condition of the and works when use in isolation, but when uncomented more than one, it doesnt work
- (define (inside_fruit world)
- (and
- ;(> (- (get_fruit_x world) 32) (get_x world))
- ;(< (+ (get_fruit_x world) 32) (get_x world))
- ;(> (- (get_fruit_y world) 32) (get_y world))
- ;(< (+ (get_fruit_y world) 32) (get_y world))
- ))
- ; ----------------------------------- START OF CODE
- (define (draw world)
- (
- place-images
- (list (square 32 "solid" "white")
- (square 32 "solid" "blue"))
- (list (make-posn (get_x world) (get_y world))
- (make-posn (get_fruit_x world) (get_fruit_y world)))
- (data-screen world)
- ))
- (define (tick_snake world)
- (
- cond
- [(= 6 (get_direction world)) (if (x_inside world (get_speed world)) (set_x world (get_speed world)) (get_data world))]
- [(= 4 (get_direction world)) (if (x_inside world (get_speed world)) (set_x world (- (get_speed world))) (get_data world))]
- [(= 8 (get_direction world)) (if (y_inside world (get_speed world)) (set_y world (get_speed world)) (get_data world))]
- [(= 2 (get_direction world)) (if (y_inside world (get_speed world)) (set_y world (- (get_speed world)))(get_data world))]
- [else (get_data world)]
- ))
- (define (collides world) (if (inside_fruit world) (set_colides world #t) (set_colides world #f)))
- (define (colision world) (if (get_colides world) (fruit_random world) (get_data world)))
- (define (tick world) (tick_snake (colision (collides world))))
- (define (input world key)
- (
- cond
- [(equal? key "left") (set_direction world 4)]
- [(equal? key "right") (set_direction world 6)]
- [(equal? key "up") (set_direction world 2)]
- [(equal? key "down") (set_direction world 8)]
- [(equal? key "q") (make_new_data)]
- [else (set_direction world (get_direction world))]
- ))
- (big-bang (make_new_data)
- (on-tick tick)
- (on-key input)
- (on-draw draw)
- (name "Snake")
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement