MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

javascript_games/digitalstream.js:12–59  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10}
11
12async function main() {
13 // Set up the constants:
14 const MIN_STREAM_LENGTH = 6; // (!) Try changing this to 1 or 50.
15 const MAX_STREAM_LENGTH = 14; // (!) Try changing this to 100.
16 const PAUSE = 0.1; // (!) Try changing this to 0.0 or 2.0.
17 const STREAM_CHARS = ['0', '1']; // (!) Try changing this to other characters.
18
19 // Density can range from 0.0 to 1.0:
20 const DENSITY = 0.02; // (!) Try changing this to 0.10 or 030.
21
22 // Get the size of the terminal window:
23 // (We can't print to the last column on Windows without it adding a
24 // newline automatically, so reduce the width by one.)
25 const WIDTH = process.stdout.columns - 1;
26
27 console.log('Digital Rain Screensaver, by Al Sweigart al@inventwithpython.com');
28 console.log('Press Ctrl-C to quit.');
29 await sleep(2);
30
31 // When the counter is 0, no bead of "digital rain" is shown.
32 // Otherwise, it acts as a counter for how many times a 1 or 0
33 // should be displayed in that column.
34 let columns = [];
35 for (let i = 0; i < WIDTH; i++) {
36 columns.push(0);
37 }
38 while (true) {
39 // Set up the counter for each column:
40 for (let i = 0; i < WIDTH; i++) {
41 if (columns[i] == 0) {
42 if (Math.random() < DENSITY) {
43 // Restart a stream on this column.
44 columns[i] = Math.floor(Math.random() * (MAX_STREAM_LENGTH - MIN_STREAM_LENGTH)) + MIN_STREAM_LENGTH;
45 }
46 }
47
48 // Display an empty space or a 1/0 character.
49 if (columns[i] > 0) {
50 process.stdout.write(STREAM_CHARS[Math.floor(Math.random() * STREAM_CHARS.length)]);
51 columns[i] -= 1;
52 } else {
53 process.stdout.write(' ');
54 }
55 }
56 console.log(); // Print a newline at the end of the row of columns.
57 await sleep(PAUSE);
58 }
59}
60
61main();

Callers 1

digitalstream.jsFile · 0.70

Calls 1

sleepFunction · 0.70

Tested by

no test coverage detected