-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.lisp
175 lines (156 loc) · 6.3 KB
/
snake.lisp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(load "/home/merov/quicklisp/setup.lisp")
(ql:quickload "bt-semaphore")
(ql:quickload "trivial-raw-io")
(defclass point ()
((x :initarg :x)
(y :initarg :y)))
(defclass game ()
((snake :initarg :snake
:initform (list (make-instance 'point :x 2 :y 4)
(make-instance 'point :x 2 :y 3)
(make-instance 'point :x 2 :y 2)))
(direction :initarg :direction
:initform 0)
(is_pause :initarg :is_pause
:initform Nil)
(food :initarg :food
:initform (list (make-instance 'point :x 2 :y 7) ;(make-random-state t)
))))
(defmethod new-food ((game1 game))
(let ((x nil)
(y nil)
(is_found T))
(loop do
(setf x (random 20))
(setf y (random 10))
(loop for point in (slot-value game1 'snake) do
(if (and (= (slot-value point 'x) x) (= (slot-value point 'y) y))
(setf is_found nil)
)
)
(if is_found
(progn
(push (make-instance 'point :x x :y y) (slot-value game1 'food))
(return)
)
)
(setf is_found T)
)
)
)
(defmethod growup ((game1 game))
(let ((x (slot-value (first (slot-value game1 'snake)) 'x))
(y (slot-value (first (slot-value game1 'snake)) 'y)))
(cond
((= (slot-value game1 'direction) 0) (push (make-instance 'point :x x :y (+ y 1)) (slot-value game1 'snake)))
((= (slot-value game1 'direction) 1) (push (make-instance 'point :x (+ x 1) :y y) (slot-value game1 'snake)))
((= (slot-value game1 'direction) 2) (push (make-instance 'point :x x :y (- y 1)) (slot-value game1 'snake)))
((= (slot-value game1 'direction) 3) (push (make-instance 'point :x (- x 1) :y y) (slot-value game1 'snake))))))
(defun cls ()
(format t "~A[H~@*~A[J" #\escape)
(format t "~A[H~@*~A[J" #\escape))
(defun remove-food (food game1)
(let ((new-list))
(loop for point in (slot-value game1 'food) do
(if (or (/= (slot-value point 'x) (slot-value food 'x)) (/= (slot-value point 'y) (slot-value food 'y)))
(setf new-list (append (slot-value game1 'food)
(list (make-instance 'point :x 3 :y 3))))))
(setf (slot-value game1 'food) new-list)))
(defmethod eat ((game1 game))
(let ((head (first (slot-value game1 'snake))))
(loop for point in (slot-value *game* 'food) do
(if (and
(= (slot-value point 'x) (slot-value head 'x))
(= (slot-value point 'y) (slot-value head 'y)))
(progn (remove-food point game1) (return T))))))
(defmethod move ((game1 game))
(let ((x (- (length (slot-value game1 'snake)) 1)))
(loop repeat (- (length (slot-value game1 'snake)) 1)
do (setf (slot-value (nth x (slot-value game1 'snake)) 'x)
(slot-value (nth (- x 1) (slot-value game1 'snake)) 'x))
(setf (slot-value (nth x (slot-value game1 'snake)) 'y)
(slot-value (nth (- x 1) (slot-value game1 'snake)) 'y))
(decf x)))
(cond
((eql (slot-value game1 'direction) 0) (incf (slot-value (first (slot-value game1 'snake)) 'y)))
((eql (slot-value game1 'direction) 1) (incf (slot-value (first (slot-value game1 'snake)) 'x)))
((eql (slot-value game1 'direction) 2) (decf (slot-value (first (slot-value game1 'snake)) 'y)))
((eql (slot-value game1 'direction) 3) (decf (slot-value (first (slot-value game1 'snake)) 'x)))))
(defmethod run ((game1 game))
(loop do (if (eql (slot-value game1 'is_pause) nil)
(progn
(cls)
(if (eat game1)
(progn
(growup game1)
(new-food game1))
(move game1))
(draw-field game1)
(setf point-food nil)))
(sleep 0.5)))
(defmethod draw-field ((game1 game))
(defvar width 20)
(defvar height 10)
(defvar y 0)
(defvar x 0)
(setq y 0)
(format t "Score: 0")
(terpri)
(format t "~v@{~A~:*~}" (+ width 2) "-")
(loop repeat height
do (setq x 0)
(incf y)
(terpri)
(format t "~a" "|")
(loop repeat width
do (incf x)
(let ((is_snake nil)
(is_food nil))
(loop for point in (slot-value *game* 'snake) do
(if (and
(= (slot-value point 'x) x)
(= (slot-value point 'y) y))
(progn (setf is_snake T) (return))))
(loop for point in (slot-value *game* 'food) do
(if (and
(= (slot-value point 'x) x)
(= (slot-value point 'y) y))
(progn (setf is_food T) (return))))
(cond
(is_snake (format t "-"))
(is_food (format t "@"))
(T (format t "+")))))
(format t "~a" "|"))
(terpri)
(format t "~v@{~A~:*~}" (+ width 2) "-")
(terpri)
(format t "~v@{~A~:*~}" (+ width 2) "-"))
(defparameter *game*
(make-instance 'game))
(defun key-down ()
(bt:make-thread
(lambda ()
(let ((x nil))
(loop
(setf x (trivial-raw-io:read-char))
(cond
((and (eql x #\A) (/= (slot-value *game* 'direction) 0))
(setf (slot-value *game* 'direction) 2))
((and (eql x #\B) (/= (slot-value *game* 'direction) 2))
(setf (slot-value *game* 'direction) 0))
((and (eql x #\C) (/= (slot-value *game* 'direction) 3))
(setf (slot-value *game* 'direction) 1))
((and (eql x #\D) (/= (slot-value *game* 'direction) 1))
(setf (slot-value *game* 'direction) 3))
((eql x #\Space)
(setf (slot-value *game* 'is_pause) (not (slot-value *game* 'is_pause)))))))))
T)
(key-down)
(run *game*)
; (let ((f '((make-instance 'point :x 2 :y 4)
; (make-instance 'point :x 2 :y 3)
; (make-instance 'point :x 2 :y 2)
; )))
; (remove-nth 0 '(2 3))
; (print f)
; )