0votos

Snake en Clojure

por AverageUser hace 5 meses

Añadir [quil "2.6.0"] a las dependencias de un proyecto Leiningen, luego usar lein run, o lein repl y lanzar (-main)

Fernando de Genbetadev dice que si haces un Snake en un lenguaje, entonces, ese lenguaje ya casi no tiene secretos para tí.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(ns my-snake.core 
  (:require [quil.core :as q])) 
 
 
;; Definitions and Variables =================================================== 
(def width      688) 
(def height     688) 
(def point-size 16) 
 
(defn create-apple [] 
  (let [margin-x (/ (- width point-size) point-size) 
        margin-y (/ (- height point-size) point-size)] 
    [(rand-int margin-x) (rand-int margin-y)])) 
 
(def apple     (atom (create-apple))) 
(def length    (atom 4)) 
(def direction (atom [1 0])) 
(def snake     (atom [[3 2] [2 2] [1 2] [0 2]])) 
 
;; purefunctions =============================================================== 
(defn key-code->move [n dir] 
  "37 = LEFT; 38 = UP; 39 = RIGHT; 40 = DOWN" 
  (cond (and (not (= dir [1  0])) (= n 37)) [-1 0] 
        (and (not (= dir [0  1])) (= n 38)) [0 -1] 
        (and (not (= dir [-1 0])) (= n 39)) [1  0] 
        (and (not (= dir [0 -1])) (= n 40)) [0  1] 
        :else    nil)) 
 
(defn v+ [v1 v2] 
  "v+ [1 2] [4 3] = [5 5]" 
  [(+ (first v1) (first v2)) 
   (+ (second v1) (second v2))]) 
 
(defn snake-grow? [apple snake] 
  "If the coordinates of the snake head are equals to 
  the coordinates of the apple, returns true." 
  (= apple (first snake))) 
 
 
(defn game-over? [snake] 
  "Checks if the snake eats him self, or the borders." 
  (let [head (first snake) body (rest snake) 
        h-x (first head) 
        h-y (second head)] 
    (or (contains? (set body) head) 
        (< h-x 0) 
        (< h-y 0) 
        (>= h-x (/ width point-size)) 
        (>= h-y (/ height point-size))))) 
 
;; Mutability ================================================================== 
(defn update-direction [] 
  (let [key (q/key-code) 
        dir (key-code->move key @direction)] 
    (when dir 
      (reset! direction dir)))) 
 
(defn update-snake [] 
  (update-direction) 
  (swap! snake #(into [(v+ @direction (first %))] (butlast %))) 
  (if (snake-grow? @apple @snake) 
    (letfn [(repeat-l [vx] (conj vx (last vx)))] 
      (swap! length inc) 
      (swap! snake repeat-l) 
      (reset! apple (create-apple))))) 
 
;; Graphic function ============================================================ 
(defn draw-board [] 
  (let [margin-x (inc (int (/ width point-size))) 
        margin-y (inc (int (/ height point-size)))] 
    (q/fill 0) 
    (q/stroke 30) 
    (doall (map #(doall (map (fn [x] (q/rect (* x point-size) (* % point-size) 
                                             point-size point-size)) 
                             (range margin-x))) 
                (range margin-y))))) 
 
(defn draw-snake [snake] 
  (let [to-draw (map #(vector (* point-size (first %)) 
                              (* point-size (second %)) 
                              point-size 
                              point-size) 
                     snake)] 
    (q/fill 0 255 0) 
    (q/stroke 0) 
    (doall (map #(apply q/rect %) to-draw)))) 
 
(defn draw-apple [apple] 
  (q/fill 255 0 0) 
  (q/stroke 0) 
  (q/rect (* point-size (first apple)) 
          (* point-size (second apple)) 
          point-size point-size)) 
 
(defn draw-length [length] 
  (q/fill 0 0 255) 
  (q/text (str "Length: " length) 5 20)) 
 
(defn setup [] 
  (q/text-font (q/create-font "DejaVu Sans" 20 true)) 
  (q/frame-rate 15)) 
 
 
(defn draw [] 
  (draw-board) 
  (update-snake) 
  (draw-length @length) 
  (draw-snake @snake) 
  (draw-apple @apple) 
  (if (game-over? @snake) 
    (do 
      (Thread/sleep 1000) 
      (q/exit)))) 
 
(defn -main [] 
 (q/defsketch my-snake 
  :title "Snake" 
  :size  [width height] 
  :setup setup 
  :draw  draw)) 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.