尝试用Common Lisp写游戏

以前刚学C++的时候,学会了循环和条件语句后,就用控制台写了个黑简单的,文字显示的RPG打怪升级的"游戏".

哈哈哈,相当简单,进去时候输名字选职业,然后就随机遇怪,打了得经验,可以升级.完了.

不过那时候还是黑兴奋诶,而且后来学校里面那些问怎么开始学习游戏,我都说的这个来练习.

 

现在试看用CL来写看,前面看的数据库的黑多部分可以拿来用.

写了哈游戏主函数和游戏循环.明天写其他的.

 

不晓得囊个的用Emacs来显示文本的时候,如果直接用中文字符串显示是乱码,但是如果输入的是中文显示是正常,可能Emacs配置不对.

先不管了英文不影响效果.

 

下面是代码

(defun game-main () (create-role) (game-run)) (defvar *player* ()) (defun make-role (name job) (list :name name :job job :hp 100 :atk 5 :def 3)) (defun save-role (role) (setf *player* role)) (defun show-role () (format t "~{~a:~2t~a~2t~}~%" *player*)) (defun read-info (info) (format *query-io* "Enter Your ~a: " info) (force-output *query-io*) (read-line *query-io*)) (defun create-role () (save-role (make-role (read-info "Name") (read-info "Job")))) (Defun game-run () (loop (game-loop) (if (want-over) (return)))) (defun game-loop () (show-role)) (defun want-over () (format t "Enter q or Q GameOver:") (equal 'q (read *query-io*)))

 

运行截图

现在游戏循环只是简单的显示玩家信息

 


2011-05-06

 

今天又写了哈,随机遇怪,战斗,有暴击,战斗结束得的到经验,恢复血.

大致熟悉了哈写的方式,因为还不晓得局部变量之类的东西有没,所以有些函数要多次用到同一个变量的时候,都是当参数传入的.

 

还有昨天那个中文显示问题,估计应该是slime的问题,用了Emacs和Slime的时候才这样.直接用ccl加载fasl文件中文都是正常显示的.

 

下面是今天的代码

(defun game-main () (create-role) (game-run) nil) (defvar *player* ()) (defun make-role (name job exp hp atk def) (list :name name :job job :exp exp :hp hp :atk atk :def def)) (defun save-role (role) (setf *player* role)) (defun show-role (role) (format t "Your ~{~a:~2t~a~2t~}~%" role)) (defun read-info (info) (format *query-io* "Enter Your ~a: " info) (force-output *query-io*) (read-line *query-io*)) (defun create-role () (save-role (make-role (read-info "Name") (read-info "Job") 0 100 15 3))) (Defun game-run () (loop (game-loop) (if (game-over?) (return)))) (defun game-loop () (if (meet-monster?) (fight)) (show-role *player*)) (defun game-over? () (cond ((= 0 (getf *player* :hp)) (format t "Game Over!~%") t) (t (format t "Enter q or Q GameOver: ") (equal 'q (read *query-io*))))) (defun meet-monster? () (success-percent? "Meet monster" 50)) (defun success-percent? (thing rate) (if (success-judge? rate) (or (format t "Judge ~a: Yes~%" thing) t) nil)) (defun 100-percent () (random 100)) (defun success-judge? (rate) (< (100-percent) rate)) (defvar *monster* ()) (defun fight () (if (not *monster*) (create-monster)) (loop (fight-loop) (if (fight-over?) (return)))) (defun save-monster (role) (setf *monster* role)) (defun create-monster () (save-monster (make-role "Luo Ming" 0 0 100 14 4))) (defun fight-loop () (calc-hurt) t) (defun recover-role () (if (success-judge? 50) (recover-hp (+ 10 (random 20))))) (defun recover-hp (hp) (setf (getf *player* :hp) (+ (getf *player* :hp) hp)) (format t "Recover HP: ~a!~%" hp)) (defun player-win () (recover-role) (get-exp (+ 5 (random 5)))) (defun get-exp (exp) (setf (getf *player* :exp) (+ (getf *player* :exp) exp)) (format t "Get EXP: ~a~%" exp)) (defun fight-over? () (cond ((= 0 (getf *monster* :hp)) (format t "Killed ~a You Win!~%" (getf *monster* :name)) (setf *monster* ()) (player-win) t) ((= 0 (getf *player* :hp)) (format t "You Dead killed by ~a!~%" (getf *monster* :name)) t) (t nil))) (defun calc-atk (role) (if (success-judge? 20) (* 2 (getf role :atk)) (getf role :atk))) (defun attack (hitter sufferer) (format t "~a HIT ~a: " (getf hitter :name) (getf sufferer :name)) (dec-hp sufferer (- (calc-atk hitter) (getf sufferer :def)))) (defun dec-hp (role value) (format t "~a Losed ~a HP!~%" (getf role :name) value) (setf (getf role :hp) (- (getf role :hp) value)) (clamp-hp role)) (defun clamp-hp (role) (if (< (getf role :hp) 0) (setf (getf role :hp) 0))) (defun calc-hurt () (attack *player* *monster*) (attack *monster* *player*))

 

下面是今天的截图

你可能感兴趣的:(Lisp)