Dan Torop

Part 4: animation

Continued from Emacs Lisp programming pt. 3.

All of these examples define functions which draw on the screen. Once you define such a function in ielm mode, you’ll need to test it in a blank buffer. To create a new buffer in another window type C-x 5 b then a name for the new buffer (such as test) and then press Enter. You should now see two buffers, each in its own window: the old ielm one and the new test one. You can use the test buffer to try out all your defuns. To switch between these two windows with the keyboard you can type C-x 5 o. Or just use the mouse, 1990s style. Emacs, by the way, refers to “windows” as “frames”.

When you’re in the test buffer, type M-: to get a Lisp prompt at the bottom of the window (it should say Eval:). You can then run a defun you’ve entered in ielm. If, for example, you created a defun called wave, type (wave) at the Eval: prompt.

A line moving down the screen

(defun line-down ()
  (dotimes (line 10)
    (erase-buffer)
    (dotimes (y line) (newline))
    (insert "----------------")
    (sit-for 0.2)))

That line moving down the screen ten times

(defun line-down-repeat ()
  (dotimes (many-times 10)
    (dotimes (line 10)
      (erase-buffer)
      (dotimes (y line) (newline))
      (insert "----------------")
      (sit-for 0.2))))

If you get bored watching it repeat, type C-g to stop the program.

Two lines moving apart from each other

(defun two-apart ()
  (dotimes (line 10)
    (erase-buffer)
    (dotimes (y line) (newline))
    (insert "------------------------")
    (dotimes (y line) (newline))
    (insert "+=======================+")
    (sit-for 0.2)))

Top line moving, bottom line static

(defun top-move-bottom-static ()
  (dotimes (line 10)
    (erase-buffer)
    (newline (+ 1 line))
    (insert "------------------------")
    (newline (- 10 line))
    (insert "+=======================+")
    (sit-for 0.2)))

One line chasing another

(defun chase ()
  (dotimes (y 30)
    (erase-buffer)
    (newline (+ 1 y))
    (insert "------------------------")
    (newline (- 16 (/ y 2)))
    (insert "+=======================+")
    (sit-for 0.1)))

Chase with final collision

(defun chase2 ()
  (dotimes (y 30)
    (erase-buffer)
    (newline (+ 1 y))
    (insert "------------------------")
    (newline (- 16 (/ y 2)))
    (insert "+=======================+")
    (sit-for 0.1))
  (erase-buffer)
  (newline 32)
  (insert "************!!!!!***********"))

A line moving to the side

(defun sideways ()
  (dotimes (x 11)
    (erase-buffer)
    (dotimes (move-over x)
      (insert " "))
    (insert "---*--------")
    (sit-for 0.1)))

Two lines moving sideways in opposite directions

(defun two-ships ()
  (dotimes (y 16)
    (erase-buffer)
    (dotimes (move-over y)
      (insert " "))
    (insert "---*--------")
    (newline)
    (dotimes (move-over (- 16 y))
      (insert " "))
    (insert "---------*---")
    (sit-for 0.1)))

Using a sine wave to move a line up and down

(defun up-down ()
  (dotimes (y 50)
    (erase-buffer)
    (newline (round (+ 11 (* 10 (sin (/ y 7.0))))))
    (insert "----------------")
    (sit-for 0.2)))

Again, if you get bored watching this run, type C-g to stop it. If you increase/decrease the number 7.0, the line will move slower or faster, respectively.

Following the path of a sine wave (moving up/down and right)

(defun follow-sine ()
  (dotimes (y 70)
    (erase-buffer)
    (newline (round (+ 11 (* 10 (sin (/ y 10.0))))))
    (dotimes (over y)
      (insert " "))
    (insert "*")
    (sit-for 0.1)))

Continued in Emacs Lisp programming pt. 5.