(require 2htdp/image) (require 2htdp/universe) ;;; Snake game ;;; Constants (define BOARD-WIDTH 30) ; in cells (define BOARD-HEIGHT 30) ; in cells (define PIXELS/CELL 10) (define SNAKE-COLOR "red") (define FOOD-COLOR "green") (define SEGMENT-IMG (circle (/ PIXELS/CELL 2) "solid" SNAKE-COLOR)) (define FOOD-IMG (circle (/ PIXELS/CELL 2) "solid" FOOD-COLOR)) (define EMPTY-BOARD (empty-scene (* PIXELS/CELL BOARD-WIDTH) (* PIXELS/CELL BOARD-HEIGHT))) ;; Grid coordinates have (0,0) at lower-left, and y axis points up ;;; Seg = (make-posn Number Number) ;;; Location of a snake segment in grid coordinates #; (define (seg-tmpl a-seg) ... (posn-x a-seg) ... (posn-y a-seg) ...) ;;; A LOS is one of: ;;; - empty ;;; - (cons Seg LOS) ;;; The first segment of the list is the head of the snake; ;;; and the last segment of the list is the tail of the snake #; (define (LOS-tmpl a-los) (cond [(empty? a-los) ...] [else ... (first a-los) ... (LOS-tmpl (rest a-los)) ...])) (define segs1 (list (make-posn 2 3))) (define segs2 (list (make-posn 2 3) (make-posn 3 3))) ;;; A Dir is one of 'up, 'down, 'left, 'right #; (define (dir-tmpl a-dir) (cond [(symbol=? a-dir 'up) ...] [(symbol=? a-dir 'down) ...] [(symbol=? a-dir 'left) ...] [(symbol=? a-dir 'right) ...])) (define-struct snake (segs dir)) ;;; A Snake is a (make-snake LOS DIR) (define snake1 (make-snake segs1 'right)) (define snake2 (make-snake segs2 'up)) #; (define (snake-tmpl a-snake) ... (snake-segs a-snake) ... (snake-dir a-snake) ...) ;;; Food = (make-posn Number Number) #; (define (food-tmpl a-food) ... (posn-x a-food) ... (posn-y a-food) ...) (define-struct world (snake food)) ;;; World = (make-world Snake Food) #; (define (world-templ a-world) ... (world-snake a-world) ... (world-food a-world) ...) (define initial-world (make-world snake1 (make-posn 15 15))) (deifne eating-world (make-world snake2 (make-posn 2 3))) #| Wishful thinking: slither-snake: Snake -> Snake grow-snake : Snake -> Snake snake-dies? : Snake -> Boolean snake-self-collides? : Snake -> Boolean snake-wall-collides? : Snake -> Boolean snake-eating? : World -> Boolean next-world : World -> World world->image : World -> Image snake+image : Snake Image -> Image food+image : Food Image -> Image key-handler : World Key -> World place-image/grid : Image Number Number Image -> Image |#