
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/scroll-art","feed_url":"https:\/\/mathspp.com\/blog\/tags\/scroll-art.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"ASCII rain scroll art","date_published":"2024-05-24T12:30:00+02:00","id":"https:\/\/mathspp.com\/blog\/ascii-rain-scroll-art","url":"https:\/\/mathspp.com\/blog\/ascii-rain-scroll-art","content_html":"<p>Using ASCII characters we can create a simple rain animation in the terminal.<\/p>\n\n<p>&ldquo;Scroll art&rdquo; is a term I've heard used to refer to images or animations made out of ASCII characters.\nIn this short article we'll go over the code necessary to create this raining animation:<\/p>\n<figure class=\"image-caption\"><img title=\"Rain scroll art animation.\" alt=\"An animation of random characters falling from the screen.\" src=\"\/user\/pages\/02.blog\/ascii-rain-scroll-art\/_ascii_scroll_art_rain.gif?decoding=auto&amp;fetchpriority=auto\"><figcaption class=\"\">Rain scroll art animation.<\/figcaption><\/figure><p>This animation runs on the terminal and only requires using the built-in <code>print<\/code>.<\/p>\n<h2 id=\"coding-the-animation\">Coding the animation<a href=\"#coding-the-animation\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>An animation is a series of images (frames) shown in quick succession.\nIn this case, each frame is composed of a series of printed lines in a loop, so it's the way in which we manage the lines that gives the impression of an animation.<\/p>\n<p>We will print some lines to fill the screen, we will pause for a fraction of a second, and then we will repeat this process.<\/p>\n<p>We will use a <code>deque<\/code> to hold all the lines that will fill the screen.\nTo create the animation of falling rain, for each frame we need to create a new line that gets printed at the top of the screen and we need to get rid of the line that was printed at the bottom of the screen in the previous frame.\nA <code>deque<\/code> is a good data structure for this because we can use <a href=\"\/blog\/python-deque-tutorial#how-to-create-a-deque\">the parameter <code>maxlen<\/code><\/a> to do this management automatically.<\/p>\n<p>So, our animation code starts like this:<\/p>\n<pre><code class=\"language-py\">from collections import deque\nimport shutil\n\nTERMINAL_WIDTH, TERMINAL_HEIGHT = shutil.get_terminal_size()\nRAIN_DENSITY = 0.05\nEMPTY_LINE = \" \" * TERMINAL_WIDTH\n\nlines = deque([EMPTY_LINE for _ in range(TERMINAL_HEIGHT)], maxlen=TERMINAL_HEIGHT)<\/code><\/pre>\n<p>At this point, if we print all of the lines inside <code>lines<\/code>, we clear the screen of the terminal.<\/p>\n<p>What we will do next is write a short function <code>build_next_line<\/code> that accepts a line of the animation and creates a new random line that we can print at the top of the animation.\nThis happens in two passes:<\/p>\n<ol><li>we put tails <code>|<\/code> on top of the drops <code>v<\/code> from the previous line; and<\/li>\n<li>we add drops <code>v<\/code> in some random positions.<\/li>\n<\/ol><p>Suppose the previous animation had this top line:<\/p>\n<pre><code>  |  vv  |v<\/code><\/pre>\n<p>In the first pass, we create this line:<\/p>\n<pre><code>     ||   |\n  |  vv  |v<\/code><\/pre>\n<p>In the second pass, we add random drops (which might replace a previous tail):<\/p>\n<pre><code>vv   ||   v\n  |  vv  |v<\/code><\/pre>\n<p>The function <code>build_next_line<\/code> looks like this:<\/p>\n<pre><code class=\"language-py\">import random\n\ndef build_next_line(source):\n    next_row = [\"|\" if char == \"v\" else \" \" for char in source]\n    for idx, _ in enumerate(next_row):\n        if random.random() &lt; RAIN_DENSITY:\n            next_row[idx] = \"v\"\n    return \"\".join(next_row)<\/code><\/pre>\n<p>To run the animation, we need an infinite loop!\nAt each step, we build a new line with the function <code>build_next_line<\/code> and append it to the left of the <code>deque<\/code> (and its <code>maxlen<\/code> parameter will make sure that the oldest line gets thrown away automatically).\nThen, we print all of the lines to the screen and pause the program for a bit:<\/p>\n<pre><code class=\"language-py\">from time import sleep\n\nwhile True:\n    new_row = build_next_line(lines[0])\n    lines.appendleft(new_row)\n    for row in lines:\n        print(row)\n    sleep(0.05)<\/code><\/pre>\n<h2 id=\"full-code\">Full code<\/h2>...","summary":"Using ASCII characters we can create a simple rain animation in the terminal.","date_modified":"2025-07-23T16:49:02+02:00","tags":["programming","python","scroll art","visualisation"],"image":"\/user\/pages\/02.blog\/ascii-rain-scroll-art\/thumbnail.webp"}]}
