2009. 5. 27.

LISP 으로 어드벤처 게임을 ... CH 10

여기서 만들게 될 함수들은 상점, 음식먹기 등에 해당한다.
먼저 지난번에 만들었던 Event Loop를 살짝 수정했다.

(defun event-loop ()
  (let ((k (get-rooms-element (get-room *room*) #\m))
        (game-over nil))
    (loop
       (status-report)
       (let ((input-key (keyboard-event)))
         (cond ((string= input-key "n") (go-direction #\n))
               ((string= input-key "s") (go-direction #\s))
               ((string= input-key "e") (go-direction #\e))
               ((string= input-key "w") (go-direction #\w))
               ((string= input-key "u") (go-direction #\u))
               ((string= input-key "d") (go-direction #\d))
               ((string= input-key "c") (consume-food))
               ((string= input-key "f") (fight))
               ((string= input-key "r") (runaway))
               ((string= input-key "q") (setf game-over t))
               ((string= input-key "m") (magic-amulet))
               ((string= input-key "p") (picking-up))
               ((string= input-key "i") (inventory))))
       (if (eq game-over t)
           (return-from event-loop)))))


마법 아뮬렛이 있을때 수행할 함수를 만든다.
;;; 마법 아뮬렛을 사용할 때 수행할 곳(6번과 11번은 제외 )
(defun magic-amulet ()
  (let ((ro (+ (random 19) 1)))
    (cond ((or (= ro 6) (= ro 11)) (magic-amulet))
          (t
           (setf *room* ro)))))


또한 음식을 먹는 함수를 만든다.

;;; 음식 먹기
(defun consume-food ()
  (cond ((>= food 1)
         (progn
           (format t "You have ~a units of food~%" *food*)
           (format t "How many do you want to eat? :")
           (let ((z (parse-integer (read-line))))
             (if (> z food)
                 (consume-food)
                 (progn
                   (decf *food* z)
                   (incf *strength* (* 5 z)))))))))


이제 방에 보물이 있는 경우 집는 함수를 만든다.
;;; 방의 물건 집기
(defun picking-up ()
  (let ((treasure (get-rooms-element
                   (get-room *room*)
                    #\m)))
    (if (< treasure 10)
        (format t "There is no treasure to pickup")
        (progn
          (incf *wealth* treasure)
          (setf (nth 6 (nth (- *room* 1) *castle*)) 0)))))

마지막으로 상점에서 물건을 구입하는 함수를 만든다.
;;; Show shop
(defun show-shop ()
  (format t "You can buy 1 - Flamming Torch ($15)~%")
  (format t "            2 - Axe ($10)~%")
  (format t "            3 - Sword ($20)~%")
  (format t "            4 - Food ($2 per unit)~%")
  (format t "            5 - Magic of Amulet ($30)~%")
  (format t "            6 - Suit of Armor ($50)~%")
  (format t "            0 - To continue Adventure ~%"))

;;; Shop에서 물건사기
(defun inventory ()
  (format t "Provision & Inventory~%")
  (cond ((> *wealth* 0)
         (progn
           (show-shop)
           (let ((z (parse-integer (prompt-read "Enter no of item:"))))
             (cond ((= z 1)
                    (progn
                      (setf *light* 1)
                      (decf *wealth* 15)))
                   ((= z 2)
                    (progn
                      (setf *axe* 1)
                      (decf *wealth* 10)))
                   ((= z 3)
                    (progn
                      (setf *sword* 1)
                      (decf *wealth* 20)))
                   ((= z 4)
                    (progn
                      (let ((q (prompt-read "how many food do you want")))
                        (cond ((> (* q 2) *wealth*)
                               (format t "You Can't Get enough money"))
                              (t
                               (progn
                                 (incf *food* q)
                                 (defc *wealth* (* q 2))))))))
                   ((= z 5)
                    (progn
                      (setf *amulet* 1)
                      (decf *wealth* 30)))
                   ((= z 6)
                    (progn
                      (setf *armor* 1)
                      (decf *wealth* 50)))))))
        (t
         (format t "You have no money~%"))))

일단 기본적인 툴이 모두 마련되었다.
다음장에서는 전투에 대한 부분을 살펴보자.

댓글 없음:

댓글 쓰기