Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun spawn-snake (board-dim &key (pos '(0 0)) (direction :down) (body-length 3))
- "Create instance of snake on board.
- ARGS:
- `board-dim' - a list like '(16 9)
- `pos' - position of the head of the snake e.g.: '(3 5)
- `direction' - :up, :down, :left, :right
- `body-length' - length mony snake-parts the snake consists of"
- (let ((x-dim (first board-dim))
- (y-dim (second board-dim))
- (x-pos (first pos))
- (y-pos (second pos)))
- (unless (outside-board-p) ;; checks weather the snake can be created
- (make-instance 'snake
- :body (snake-make-body)
- :length body-length
- :head-direction direction)
- (defun snake-make-body ()
- "create list out of `body-length' snake-parts. Starting from the tail"
- (do* ((i 0 (1+ i))
- (previous-part nil)
- (curr-part nil)
- (result '() (cons curr-part result)))
- ((= i 3) result)
- (setf curr-part (make-instance 'snake-part
- :position `(,(calc-x-pos i) ,(calc-y-pos i))
- :previous previous-part))
- (setf previous-part curr-part))
- (defun calc-x-pos (index)
- (case direction
- (:up x-pos)
- (:down x-pos)
- (:left (+ x-pos (1- (- body-length index))))
- (:right (- x-pos (1- (- body-length index))))))
- (defun calc-y-pos (index)
- (case direction
- (:up (- y-pos (1- (- body-length index))))
- (:down (+ y-pos (1- (- body-length index))))
- (:left y-pos)
- (:right y-pos))))
- (defun outside-board-p ()
- "Checks weather the snake fits completely onto the board."
- (or (head-not-on-board-p)
- (tail-in-wall-p))
- (defun head-not-on-board-p ()
- (or (or (> 0 x-pos) (>= x-pos x-dim))
- (or (> 0 y-pos) (>= y-pos y-dim))))
- (defun tail-in-wall-p ()
- (case direction
- (:up (> 0 (- y-pos (1- body-length))))
- (:down (< y-dim (+ y-pos (1- body-length))))
- (:left (< x-dim (+ x-pos (1- body-length))))
- (:right (> 0 (- x-pos (1- body-length))))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement