-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.lisp
417 lines (360 loc) · 11.5 KB
/
canvas.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
;; pelusica 1.0.0
(in-package #:calm)
;;
;; CALM version check
;; version check won't work on JSCL, since the lack of ASDF
;;
#-jscl
(let ((required-version "1.0.0")
(calm-version (slot-value (asdf:find-system 'calm) 'asdf:version)))
(when (uiop:version< calm-version required-version)
(format t
"Sorry, this is built on CALM ~A, older version (current: ~A) of CALM won't work.~%"
required-version calm-version)
(uiop:quit)))
;;
;; the swank server is for debugging, for usage please check
;; Emacs:
;; https://slime.common-lisp.dev/
;; Visual Studio Code
;; https://lispcookbook.github.io/cl-cookbook/vscode-alive.html
;;
;; uncomment the following line to enable SWANK Server
#-jscl
(unless (str:starts-with? "dist" (uiop:getenv "CALM_CMD")) (swank:create-server))
;;
;; by default, the screensaver is disabled,
;; if you want to enable screensaver,
;; please uncomment the following line
;;
;; (setf (uiop:getenv "SDL_VIDEO_ALLOW_SCREENSAVER") "1")
;;
;; setting window properties, for more of this, please check
;; https://github.com/VitoVan/calm/blob/main/src/config.lisp
;;
(setf *calm-window-width* 600)
(setf *calm-window-height* 600)
(setf *calm-window-title* "Pelúsica")
#+jscl
(setf *calm-fps* 0)
#-jscl
(setf *calm-delay* 4)
(defparameter *music-list* '(
g3 b3 d4
g3 b3 d4
g3 b3 d4
g3 b3 d4
f+3 b3 d4
f+3 b3 d4
f+3 b3 d4
f+3 b3 d4
g3 b3 d4
g3 b3 d4
g3 b3 d4
g3 b3 d4
f+3 a3 c4
f+3 a3 c4
f+3 a3 c4
f+3 a3 c4
g3 b3 d4 e4 d4 c4 b3 a3
f+3 a3 c4 d4 c4 b3 a3 b3
g3 b3 d4 e4 d4 c4 b3 a3
f+3 a3 c4 d4 c4 b3 a3 g3
g3 b3 d4 e4 d4 c4 b3 a3
f+3 a3 c4 d4 c4 b3 a3 b3
g3 b3 d4 e4 d4 c4 b3 c4
f+3 a3 c4 d4 c4 b3 a3 g3
g3 b3 d4 f4 e4 c4 b3 c4
f+3 a3 c4 e4 d4 a3 g3 b3
g3 b3 d4 f4 e4 d4 c4 b3
f+3 a3 c4 d4 c4 b3 a3 g3
))
(defparameter *started* nil)
(defparameter *paused* nil)
(defparameter *died* nil)
(defparameter *suffocated* nil)
(defparameter *music-played* 0)
(defparameter *music-index* 0)
(defparameter *level* 0)
(defparameter *ball-x* 100)
(defparameter *ball-y* 570)
(defparameter *enemy-list* (list (list 1 200) (list 2 100) (list 3 0) (list 4 -100) (list 5 -200)))
(defparameter *enemy-move-per-ms* 0.2)
(defparameter *enemy-move-tick* nil
"to detect should we move the enemies,
the value were set when running, leave this nil while def")
(defparameter *bg-color* '(1 1 1))
(defparameter *enemy-color* '(0 0 0))
(defparameter *ball-color* '(0 0.35 0.59))
(defparameter *health-color* '(0.89 0.12 0.17))
(defparameter *not-health-color* '(0.83 0.82 0.84))
(defparameter *bg-color-light* '(1 1 1))
(defparameter *enemy-color-light* '(0 0 0))
(defparameter *ball-color-light* '(0 0.35 0.59))
(defparameter *health-color-light* '(0.89 0.12 0.17))
(defparameter *bg-color-dark* '(0.1 0.1 0.1))
;; (defparameter *bg-color-dark* '(0 0 0))
(defparameter *enemy-color-dark* '(1 1 1))
;; (defparameter *ball-color-dark* '(0.027 1 0.89))
(defparameter *ball-color-dark* '(0.1 0.45 0.69))
(defparameter *health-color-dark* '(0.89 0.12 0.17))
(defun switch-color ()
(if (= (mod *music-played* 2) 0)
(setf *bg-color* *bg-color-light*
*enemy-color* *enemy-color-light*
*ball-color* *ball-color-light*
*health-color* *health-color-light*)
(setf *bg-color* *bg-color-dark*
*enemy-color* *enemy-color-dark*
*ball-color* *ball-color-dark*
*health-color* *health-color-dark*)))
(defun play-note ()
(if (< (c:playing) 8)
(progn
(c:play-wav (concatenate 'string "assets/" (string-downcase (nth *music-index* *music-list*)) ".wav"))
(if (>= *music-index* (1- (length *music-list*)))
(progn
(setf *music-index* 0)
(incf *music-played*)
(setf *ball-y* (- *ball-y* (* *music-played* 42))))
(incf *music-index*)))
(progn
(setf *died* t
*suffocated* t)
(c:play-music "assets/block.wav"))))
(defun reset-game ()
(setf
*enemy-list* (list (list 1 200) (list 2 100) (list 3 0) (list 4 -100) (list 5 -200))
*enemy-move-per-ms* 0.2
*enemy-move-tick* nil
*ball-x* 100
*ball-y* 570
*music-played* 0
*music-index* 0
*level* 0
*died* nil
*suffocated* nil
*calm-redraw* t
))
(defun on-keydown (key)
(when (c:keq key
:SCANCODE-RIGHT
:SCANCODE-K
:SCANCODE-L
:SCANCODE-LEFT
:SCANCODE-J
:SCANCODE-H
:SCANCODE-RETURN)
(c:open-audio-if-not-yet))
(cond
((c:keq key :SCANCODE-P)
(setf
*paused* (not *paused*)
*enemy-move-tick* nil)))
(when (and (not *died*) (not *paused*) *started*)
(cond
((c:keq key :SCANCODE-RIGHT :SCANCODE-K :SCANCODE-L)
(when (and (< *ball-x* 450) (play-note))
(incf *ball-x* 100)))
((c:keq key :SCANCODE-LEFT :SCANCODE-J :SCANCODE-H)
(when (and (> *ball-x* 100) (play-note))
(decf *ball-x* 100)))
(t (format t "~%KEY PRESSED: ~A~%" key))))
(unless *started*
(cond
((c:keq key :SCANCODE-RETURN :SCANCODE-LEFT :SCANCODE-RIGHT)
(setf *started* t))))
(when *died*
(cond
((c:keq key :SCANCODE-RETURN)
(reset-game)))))
(defun on-fingerup (&key x y dx dy pressure finger-id)
(declare (ignore y dx dy pressure finger-id))
(c:open-audio-if-not-yet)
(when (and (not *died*) (not *paused*) *started*)
(cond
((> x 0.5)
(when (and (< *ball-x* 450) (play-note))
(incf *ball-x* 100)))
((< x 0.5)
(when (and (> *ball-x* 100) (play-note))
(decf *ball-x* 100)))
((= x 0.5) ;; I wonder who can actually do this?
(setf
*paused* (not *paused*)
*enemy-move-tick* nil))
(t (format t "~%Finger pressed, x: ~A~%" x))))
(unless *started* (setf *started* t))
(when *died*
(reset-game)))
(defun draw-welcome-screen ()
(c:set-source-rgb 1 1 1)
(c:paint)
(c:select-font-family "Open Sans" :normal :normal)
(c:set-font-size 24)
(apply #'c:set-source-rgb *ball-color*)
(c:move-to 120 240)
(c:show-text "press ")
(apply #'c:set-source-rgb *health-color*)
#-jscl
(cond
((uiop:os-macosx-p) (c:show-text "return"))
(t (c:show-text "Enter")))
#+jscl
(if (= (#j:window:navigator:userAgent:indexOf "Mac OS") -1)
(c:show-text "Enter")
(c:show-text "return"))
(apply #'c:set-source-rgb *ball-color*)
(c:show-text " to start or restart")
(c:move-to 120 290)
(c:show-text "press ")
(apply #'c:set-source-rgb *health-color*)
(c:show-text "left")
(apply #'c:set-source-rgb *ball-color*)
(c:show-text " or ")
(apply #'c:set-source-rgb *health-color*)
(c:show-text "right")
(apply #'c:set-source-rgb *ball-color*)
(c:show-text " to move")
#+jscl
(when (or (<= #j:screen:width 800) (<= #j:screen:height 600))
(c:move-to 120 370)
(c:show-text "If you don't have keyboard now,")
(c:move-to 120 420)
(c:show-text "touch the left or right half of screen")))
(defun draw-died-screen ()
(if *suffocated*
(apply #'c:set-source-rgb *not-health-color*)
(apply #'c:set-source-rgb *health-color*))
(c:arc 300 300 50 0 (* 2 pi))
(c:fill-path)
(if *suffocated*
(apply #'c:set-source-rgb *health-color*)
(c:set-source-rgb 1 1 1))
(c:move-to 280 280)
(c:line-to 320 320)
(c:stroke)
(c:move-to 320 280)
(c:line-to 280 320)
(c:stroke)
)
(defun draw-health-bar ()
(c:save)
(c:set-line-width 10)
(let ((bar-points 8)
(bar-width 60))
(c:move-to 580 20)
(if (or *died* (= (c:playing) 8))
(apply #'c:set-source-rgb *not-health-color*)
(apply #'c:set-source-rgb *health-color*))
(c:line-to (- 580 (* (/ bar-width bar-points)
(if *died* 0 (- 8 (c:playing))))) 20)
(c:stroke))
(c:restore))
(defun recal-level ()
(setf
*level* (* 40 (/ *music-index* (length *music-list*)))
*enemy-move-per-ms* (max 0.22 (/
(log (1+ *level*)) 4))))
(defun draw-level-border ()
(apply #'c:set-source-rgb *ball-color*)
(c:set-line-width 10)
(c:set-line-cap :round)
(cond
((> *level* 30)
(c:move-to 0 0)
(c:line-to 0 600)
(c:line-to 600 600)
(c:line-to 600 0)
(c:move-to 600 0)
(c:line-to (- 600 (* 600 (/ (- *level* 30) 10))) 0)
(c:stroke))
((> *level* 20)
(c:move-to 0 0)
(c:line-to 0 600)
(c:line-to 600 600)
(c:move-to 600 600)
(c:line-to 600 (- 600 (* 600 (/ (- *level* 20) 10))))
(c:stroke))
((> *level* 10)
(c:move-to 0 0)
(c:line-to 0 600)
(c:move-to 0 600)
(c:line-to (* 600 (/ (- *level* 10) 10)) 600)
(c:stroke))
((> *level* 0)
(c:move-to 0 0)
(c:line-to 0 (* 600 (/ (- *level* 0) 10)))
(c:stroke))
))
(defun draw-enemy (x y)
(c:save)
(apply #'c:set-source-rgb *enemy-color*)
(c:move-to x y)
(c:line-to x (- y 1))
(c:stroke)
(c:restore))
(defun draw-enemies ()
(when (< *music-played* 2)
(let ((should-move
(and
(not *died*)
(not *paused*))))
(loop
for enemy in *enemy-list*
for x = (* 100 (car enemy))
for y = (cadr enemy)
for i = 0 then (incf i)
if (< y 630)
do
(draw-enemy x y)
;; test if hit the ball, mercy 10
(when (and (not *died*) (= *ball-x* x) (< (abs (- *ball-y* y)) 10))
(setf *died* t)
(c:play-music "assets/chord.wav"))
(when should-move
;; move downward
(incf
(cadr (nth i *enemy-list*))
(if *enemy-move-tick*
(* (- (c:get-ticks) *enemy-move-tick*) *enemy-move-per-ms*)
1)))
else
do
;; randomly reset the Y position
(setf (cadr (nth i *enemy-list*)) (* (1+ (random 400)) -1)))
(when should-move (setf *enemy-move-tick* (c:get-ticks))))))
(defun draw-ball ()
(c:save)
(c:set-line-width 30)
(apply #'c:set-source-rgb *ball-color*)
(c:move-to *ball-x* *ball-y*)
(c:line-to *ball-x* (- *ball-y* 1))
(c:stroke)
(c:restore))
(defun draw-forever ()
(unless *started*
(draw-welcome-screen)
(setf *calm-redraw* nil)
(return-from draw-forever))
(apply #'c:set-source-rgb *bg-color*)
(c:paint)
;; lines
(c:set-source-rgb 0.83 0.82 0.84)
(loop for x from 1 to 5
do
(c:move-to (* 100 x) 0)
(c:line-to (* 100 x) 600)
(c:stroke))
(c:set-line-cap :round)
(c:set-line-width 30)
(switch-color)
(draw-health-bar)
(draw-enemies)
(recal-level)
(draw-level-border)
(draw-ball)
(when *died*
(draw-died-screen))
(when (or *paused* *died* *suffocated*)
(setf *calm-redraw* nil)))