Skip to content

Instantly share code, notes, and snippets.

@Cxarli
Last active February 22, 2017 23:18
Show Gist options
  • Save Cxarli/9ffd508cf002fcfd9e8b418b35353785 to your computer and use it in GitHub Desktop.
Save Cxarli/9ffd508cf002fcfd9e8b418b35353785 to your computer and use it in GitHub Desktop.
;; Constants
;;; TICK: The time between renders
(setf TICK 0.5)
;; Symbols
(setq WALL '\#)
(setq PEMP '\ )
(setq PDOT '\.)
(setq PILL '*)
(setq PACM 'C)
;; The grid on which pacman moves
(setq grid '(
(A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
(B 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0)
(C 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0)
(D 0 3 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 3 0)
(E 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0)
(F 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0)
(G 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0)
(H 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0)
(I 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0)
(J 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 2 0 0 0 0 0 1 0 0 0 0 0 0)
(K 2 2 2 2 2 0 1 0 0 0 0 0 2 0 0 2 0 0 0 0 0 1 0 2 2 2 2 2)
(L 2 2 2 2 2 0 1 0 0 2 2 2 2 2 2 2 2 2 2 0 0 1 0 2 2 2 2 2)
(M 2 2 2 2 2 0 1 0 0 2 0 0 0 2 2 0 0 0 2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1 1 1 1 1 1 1 1 0)
(E 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
))
;; Pacman location
(setf X 14)
(setf Y 23)
;; Direction
(setf dX 1)
(setf dY 0)
;; Print a newline
(defun NL ()
(format t "~%")
)
;; Clear the terminal
(defun cls ()
(format t "~A[H~@*~A[J" #\escape)
)
;; Draw Pac-Man
(defun drawpacman ()
;; TODO Alternating C and O
PACM
)
;; Convert element number to element char
(defun numtochar (num)
(if (eq num 0) ;; WALL
WALL
(if (eq num 1) ;; Path (dot)
PDOT
(if (eq num 2) ;; Path (empty)
PEMP
(if (eq num 3) ;; Pill
PILL
(if (eq num 9) ;; Pacman
(drawpacman)
num
)
)
)
)
)
)
;; Print item
(defun printitem (item)
(format t "~a" (numtochar item))
)
;; Print line of grid
(defun printline (line)
(loop for item in line do
(printitem item)
)
(NL)
)
;; Print pacman location
(defun printlocation ()
(format t "X: ~a; Y: ~a ~%" X Y)
)
;; Print grid
(defun printgrid ()
(loop for line in grid do
(printline line)
)
(NL)
)
;; Check if position is valid to go to
(defun is-valid-position (newX newY newelem)
(not (eq newelem 0))
)
;; Move pacman
(defun move-pacman ()
;; Get new coordinates
(setf newX (+ X dX))
(setf newY (+ Y dY))
;; Get new element
(setf newelem (nth newX (nth newY grid)))
;; Check if move is valid
(if (is-valid-position newX newY newelem) ;; Wall
(progn
;; Set current position to PATH
(setf (nth X (nth Y grid)) 1)
;; Set next position to PACM
(setf (nth newX (nth newY grid)) 9)
;; Add change to location
(setf X newX)
(setf Y newY)
T
)
)
)
;; Update state
(defun update ()
;; DEBUG: Remove from production
(if (not (move-pacman))
(format t "Failed to move!~%")
)
)
;; Draw screen
(defun draw ()
;; Clear terminal
;; DEBUG: Enable on production to dismiss update errors
;; (cls)
;; Print grid
(printgrid)
;; DEBUG Print location
(printlocation)
)
;; Main loop
(loop do
;; Calculate new state
(update)
;; Draw screen
(draw)
;; Wait for tick
(sleep TICK)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment