Matrix digital rain: Difference between revisions

From Rosetta Code
Content added Content deleted
(Task description, reference implementation in Lisp.)
 
(Abbreviate Lisp to prevent line breaks.)
Line 10: Line 10:


<lang lisp>
<lang lisp>
(defun matrix-digital-rain ()
(defun matrix3 ()
(with-screen (scr :input-echoing nil :input-blocking nil :cursor-visibility nil)
(with-screen (scr :input-echoing nil :input-blocking nil :cursor-visibility nil)
(let* ((width (.width scr))
(let* ((width (.width scr))
(height (.height scr))
(height (.height scr))
;; start at a random height in each column.
;; start at a random height in each column.
(positions (loop repeat width collect (random height)))
(pos (loop repeat width collect (random height)))
;; run each column at a random speed.
;; run each column at a random speed.
(speeds (loop repeat width collect (random 4))))
(speeds (loop repeat width collect (random 4))))
Line 24: Line 24:
;; generate a random ascii char
;; generate a random ascii char
(flet ((randch () (+ 64 (random 58))))
(flet ((randch () (+ 64 (random 58))))
(loop for column from 0 to (1- width) do
(loop for col from 0 to (1- width) do
(loop repeat (nth column speeds) do
(loop repeat (nth col speeds) do
(setf (.attributes win) '(:bold))
(setf (.attributes win) '(:bold))
(setf (.color-pair win) '(:white :black))
(setf (.color-pair win) '(:white :black))
(add-char win (randch) :y (mod (nth column positions) height) :x column)
(add win (randch) :y (mod (nth col pos) height) :x col)
(setf (.color-pair win) '(:green :black))
(setf (.color-pair win) '(:green :black))
(add-char win (randch) :y (mod (- (nth column positions) 1) height) :x column)
(add win (randch) :y (mod (- (nth col pos) 1) height) :x col)
(add-char win (randch) :y (mod (- (nth column positions) 2) height) :x column)
(add win (randch) :y (mod (- (nth col pos) 2) height) :x col)
(setf (.attributes win) '())
(setf (.attributes win) '())
(add-char win (randch) :y (mod (- (nth column positions) 3) height) :x column)
(add win (randch) :y (mod (- (nth col pos) 3) height) :x col)
;; overwrite the last char half the height from the first char.
;; overwrite the last char half the height from the first char.
(add-char win #\space :y (mod (- (nth column positions) (floor height 2)) height) :x column)
(add win #\space :y (mod (- (nth col pos) (floor height 2)) height) :x col)
(refresh win)
(refresh win)
;; advance the column
;; advance the column
(setf (nth column positions) (mod (1+ (nth column positions)) height)))))))
(setf (nth col pos) (mod (1+ (nth col pos)) height)))))))
(setf (.frame-rate scr) 20)
(setf (.frame-rate scr) 20)
(run-event-loop scr))))
(run-event-loop scr))))

Revision as of 17:57, 18 December 2018

Task
Matrix digital rain
You are encouraged to solve this task according to the task description, using any language you may know.

Implement the Matrix Digital Rain visual effect from the movie "The Matrix" as described in Wikipedia.

Provided is a reference implementation in Common Lisp to be run in a terminal.

Common Lisp

Runs in the terminal (using the Ncurses C library and the croatoan Lisp wrapper).

<lang lisp> (defun matrix3 ()

 (with-screen (scr :input-echoing nil :input-blocking nil :cursor-visibility nil)
   (let* ((width (.width scr))
          (height (.height scr))
          ;; start at a random height in each column.
          (pos (loop repeat width collect (random height)))
          ;; run each column at a random speed.
          (speeds (loop repeat width collect (random 4))))
     ;; hit the q key to exit the main loop.
     (add-event-handler (scr #\q) 'exit-event-loop)
     (add-event-handler (scr nil)
       (lambda (win event)
         ;; generate a random ascii char
         (flet ((randch () (+ 64 (random 58))))
           (loop for col from 0 to (1- width) do
             (loop repeat (nth col speeds) do
               (setf (.attributes win) '(:bold))
               (setf (.color-pair win) '(:white :black))
               (add win (randch) :y (mod (nth col pos) height) :x col)
               (setf (.color-pair win) '(:green :black))
               (add win (randch) :y (mod (- (nth col pos) 1) height) :x col)
               (add win (randch) :y (mod (- (nth col pos) 2) height) :x col)
               (setf (.attributes win) '())
               (add win (randch) :y (mod (- (nth col pos) 3) height) :x col)
               ;; overwrite the last char half the height from the first char.
               (add win #\space  :y (mod (- (nth col pos) (floor height 2)) height) :x col)
               (refresh win)
               ;; advance the column
               (setf (nth col pos) (mod (1+ (nth col pos)) height)))))))
     (setf (.frame-rate scr) 20)
     (run-event-loop scr))))

</lang>