(require 2htdp/image) (require 2htdp/universe) ;;; SNAKE WORLD (define-struct world (snake food)) (define-struct snake (dir segs)) ;;; A World is a (make-world Snake Food). ;;; Food = Posn ;;; A Snake is a (make-snake Direction Segs) ;;; A snake's Segs may not be empty. ;;; The first element of the list is the head. ;;; Direction is one of: 'up 'down 'left 'right ;;; Segs is one of: ;;; -- empty ;;; -- (cons Posn Segs) ;;; Coordinates are in "grid" units, with X running left-to-right, ;;; and Y running bottom-to-top. ;;; Start with the world, when you make up a wish list, ;;; the others will fall out. Organise wish list into groups ;;; of functions. ;;; Rendering subystem ;;; world->scene : World -> Scene ;;; food+scene : Food Scene -> Scene ;;; snake+scene : Snake Scene -> Scene ;;; Other big-bang handlers: ;;; next-world : World -> World ;;; key-handler : World Key-event -> World ;;; World simulation: ;;; snake-slither : Snake -> Snake ;;; new-direction : Snake Direction -> Snake ;;; snake-eat : World -> World ;;; snake-grow : Snake -> Snake ;;; Collision detection of various kinds: ;;; snake-dead? : Snake -> Boolean ;;; snake-self-collide? : Snake -> Boolean ;;; snake-wall-collide? : Snake -> Boolean ;;; snake-eating? : World -> Boolean ;; Add purpose statements. Then pick a place and start. ;; --- CONSTANTS : DESCRIBE PROPERTIES THAT ARE ALWAYS THE SAME (define GRID-SIZE 10) ; width of a game-board square (define BOARD-HEIGHT 20) ; height in grid squares (define BOARD-WIDTH 30) ; width in grid squares (define BOARD-HEIGHT-PIXELS (* GRID-SIZE BOARD-HEIGHT)) (define BOARD-WIDTH-PIXELS (* GRID-SIZE BOARD-WIDTH)) (define BACKGROUND (empty-scene BOARD-WIDTH-PIXELS BOARD-HEIGHT-PIXELS)) (define SEGMENT-RADIUS (quotient GRID-SIZE 2)) (define SEGMENT-IMAGE (circle SEGMENT-RADIUS 'solid 'red)) (define FOOD-RADIUS (floor (* 0.9 SEGMENT-RADIUS))) (define FOOD-IMAGE (circle FOOD-RADIUS 'solid 'green)) (define Snake1 (make-snake 'right (cons (make-posn 5 3) empty))) (define Food1 (make-posn 8 12)) (define World1 (make-world Snake1 Food1)) (define World2 (make-world Snake1 (make-posn 5 3))) ; An eating scenario ;; --- FUNCTIONS ;;; Image-painting functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; place-image/grid Image Number Number Scene ;;; Just like PLACE-IMAGE, but use grid coordinates. (define (place-image-on-grid img x y scene) (place-image img (* GRID-SIZE x) ;; We have to round-off because of the 1/2: (round (- BOARD-HEIGHT-PIXELS (* GRID-SIZE (+ 1/2 y)))) scene)) (check-expect (place-image-on-grid FOOD-IMAGE 10 10 BACKGROUND) (place-image FOOD-IMAGE (* 10 GRID-SIZE) (- BOARD-HEIGHT-PIXELS (* GRID-SIZE 21/2)) BACKGROUND))