使用Clojure实现贪吃蛇

最近学习Clojure,买了<<Clojure程序设计>>,对里面大部分章节进行了精读. 比如贪吃蛇程序,并且这个程序也比较精简,200行不到.

在读的过程中,对不能一目了然的地方,添加了注释,现发出来,希望能对有的人有用.

 

 

;;
;;Excerpted from "Programming Clojure, Second Edition",
;;published by The Pragmatic Bookshelf.
;;Copyrights apply to this code. It may not be used to create training material, 
;;courses, books, articles, and the like. Contact us if you are in doubt.
;;We make no guarantees that this code is fit for any purpose. 
;;Visit http://www.pragmaticprogrammer.com/titles/shcloj2 for more book information.
;;
; Inspired by the snakes that have gone before:
; Abhishek Reddy's snake: http://www.plt1.com/1070/even-smaller-snake/
; Mark Volkmann's snake: http://www.ociweb.com/mark/programming/ClojureSnake.html 

; The START:/END: pairs are production artifacts for the book and not 
; part of normal Clojure style

(ns examples.snake
  (:import (java.awt Color Dimension) 
   (javax.swing JPanel JFrame Timer JOptionPane)
           (java.awt.event ActionListener KeyListener))
  (:use examples.import-static))
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_UP VK_DOWN)

; ----------------------------------------------------------
; functional model
; ----------------------------------------------------------
(def width 75)
(def height 50)
(def point-size 10)
(def turn-millis 75)
(def win-length 5)
;通过Map定义了4个运动方向
(def dirs { VK_LEFT  [-1  0] 
            VK_RIGHT [ 1  0]
            VK_UP    [ 0 -1] 
    VK_DOWN  [ 0  1]})

;当snake吃到苹果后, snak的长度需要添加1	
(defn add-points [& pts] 
  (vec (apply map + pts)))

  
;pt是 snake的节点坐标(body)信息,或者apple的位置(location)坐标信息, 数据结构是一个2维vector [(rand-int width) (rand-int height)]   ;
;(pt 0):取vector#pt的第一个元素返回,X坐标
;(pt 1):取vector#pt的第二个元素返回,Y坐标
(defn point-to-screen-rect [pt] 
  (map #(* point-size %) 
       [(pt 0) (pt 1) 1 1]))

(defn create-apple [] 
  {:location [(rand-int width) (rand-int height)]
   :color (Color. 210 50 90)
   :type :apple}) 

(defn create-snake []
  {:body (list [1 1]) 
   :dir [1 0]
   :type :snake
   :color (Color. 15 160 70)})

;这种函数定义方式很少见,可参考 http://stuartsierra.com/2010/01/15/keyword-arguments-in-clojure
;参数1是一个Map, 使用了结构的办法直接传递给函数体,
;参数2不是必须的, 是变长参数(在此Demo程序中,传入了一个类型为key的实参,类似于传入了True )  
;业务逻辑就是在snak移动的过程中,判断是否需要增长一个节点
;:keys [body dir]} 从输入Map中,寻找:body所对应的Value并赋值给body
;
(defn move [{:keys [body dir] :as snake} & grow]
  (assoc snake :body (cons (add-points (first body) dir) 
   (if grow body (butlast body)))))

(defn turn [snake newdir] 
  (assoc snake :dir newdir))

(defn win? [{body :body}]
  (>= (count body) win-length))

;检测snake是否出现交叉,当snake长度扩张后容易出现  
(defn head-overlaps-body? [{[head & body] :body}]
  (contains? (set body) head))

(def lose? head-overlaps-body?)

;snake头和苹果重合时, snake吃掉apple
(defn eats? [{[snake-head] :body} {apple :location}]
   (= snake-head apple))

; ----------------------------------------------------------
; mutable model
; ----------------------------------------------------------
;苹果被吃和新苹果显示,需要在一个事务中;
;如果被吃,需要重新生成苹果,然后Move需要考虑蛇的增长
;如果没有被吃,则只需简单Move
(defn update-positions [snake apple]
  (dosync
   (if (eats? @snake @apple)
     (do (ref-set apple (create-apple))
 (alter snake move :grow))
     (alter snake move)))
  nil)

;Snake的:dir因为同时被move函数读取,所以读取:dir与update :dir需要通过事务来隔离.
(defn update-direction [snake newdir]
  (when newdir (dosync (alter snake turn newdir))))

(defn reset-game [snake apple]
  (dosync (ref-set apple (create-apple))
  (ref-set snake (create-snake)))
  nil)

; ----------------------------------------------------------
; gui
; ----------------------------------------------------------
(defn fill-point [g pt color] 
  (let [[x y width height] (point-to-screen-rect pt)]
    (.setColor g color) 
;x,y指定了横纵坐标, 	width,height指定了矩形的宽和高
    (.fillRect g x y width height)))
	
;声明一个多态的方法,具体的实现由Object中的:type决定, 在此Demo中:type有2类, :apple, :snake
(defmulti paint (fn [g object & _] (:type object)))

;多态的实现之一
(defmethod paint :apple [g {:keys [location color]}] ; <label id="code.paint.apple"/>
  (fill-point g location color))

;多态的实现之一
(defmethod paint :snake [g {:keys [body color]}] ; <label id="code.paint.snake"/>
;doseq 使用point 迭代 body里面的每一个元素,然后传递给(fill-point g point color)处理
;确保snake body里面的每一个元素都被打印
  (doseq [point body]
    (fill-point g point color)))

	
;返回的是一个Class,实现了2个接口,覆写父类的部分方法	
(defn game-panel [frame snake apple]
;JPanel是Class, ActionListener KeyListener 是Interface
  (proxy [JPanel ActionListener KeyListener] []
    (paintComponent [g] ; <label id="code.game-panel.paintComponent"/> 重写 JComponent.paintComponent方法
      (proxy-super paintComponent g)
      (paint g @snake)
      (paint g @apple))
   
   (actionPerformed [e] ; <label id="code.game-panel.actionPerformed"/>实现接口ActionListener总的方法
      (update-positions snake apple)
      (when (lose? @snake)
(reset-game snake apple)
(JOptionPane/showMessageDialog frame "You lose!"))
      (when (win? @snake)
(reset-game snake apple)
(JOptionPane/showMessageDialog frame "You win!"))
      (.repaint this))
  
    (keyPressed [e] ; <label id="code.game-panel.keyPressed"/> ;实现接口KeyListener中的方法
      (update-direction snake (dirs (.getKeyCode e)))) ;捕捉键盘方向键,然后转换成之前定义的Map中寻找匹配的Value
   
   (getPreferredSize []  ;重写 JComponent.getPreferredSize 方法
      (Dimension. (* (inc width) point-size) 
  (* (inc height) point-size)))
    
(keyReleased [e]) ;实现接口KeyListener中的方法
    
(keyTyped [e]))) ;实现接口KeyListener中的方法

(defn game [] 
  (let [snake (ref (create-snake)) ; <label id="code.game.let"/>
apple (ref (create-apple))
frame (JFrame. "Snake")
panel (game-panel frame snake apple)
timer (Timer. turn-millis panel)]
;设置Panel对应的监听
    (doto panel ; <label id="code.game.panel"/>
      (.setFocusable true)
      (.addKeyListener panel))
;将游戏Panel关联到frame上面	  
    (doto frame ; <label id="code.game.frame"/>
      (.add panel)
      (.pack)
      (.setVisible true))
    (.start timer) ; <label id="code.game.timer"/>
    [snake, apple, timer])) ; <label id="code.game.return"/>

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值