-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patharrow-key_drawer.pl
130 lines (100 loc) · 2.86 KB
/
arrow-key_drawer.pl
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 25 June 2015
# Edit: 26 February 2023
# Website: https://github.com/trizen
# Draw right-angle abstract-art using the arrow-keys.
use utf8;
use 5.010;
use strict;
use warnings;
use Time::HiRes qw(sleep);
use Term::ANSIColor qw(colored);
use Term::ReadKey qw(ReadMode ReadLine);
binmode(STDOUT, ':utf8');
use constant {
VOID => 0,
HEAD => 1,
BODY => 2,
};
use constant {
LEFT => [+0, -1],
RIGHT => [+0, +1],
UP => [-1, +0],
DOWN => [+1, +0],
};
use constant {BG_COLOR => 'on_black'};
use constant {PEN_COLOR => ('bold green' . ' ' . BG_COLOR)};
use constant {
U_HEAD => colored('▲', PEN_COLOR),
D_HEAD => colored('▼', PEN_COLOR),
L_HEAD => colored('◀', PEN_COLOR),
R_HEAD => colored('▶', PEN_COLOR),
U_BODY => colored('■', PEN_COLOR),
D_BODY => colored('■', PEN_COLOR),
L_BODY => colored('■', PEN_COLOR),
R_BODY => colored('■', PEN_COLOR),
A_VOID => colored(' ', BG_COLOR),
};
my $sleep = 0.07; # sleep duration between displays
local $| = 1;
my $w = eval { `tput cols` } || 80;
my $h = eval { `tput lines` } || 24;
my $r = "\033[H";
my @grid = map {
[map { [VOID] } 1 .. $w]
} 1 .. $h;
my $dir = LEFT;
my @head_pos = ($h / 2, $w / 2);
my @tail_pos = ($head_pos[0], $head_pos[1] + 1);
$grid[$head_pos[0]][$head_pos[1]] = [HEAD, $dir]; # head
sub display {
print $r, join(
"\n",
map {
join(
"",
map {
my $t = $_->[0];
my $p = $_->[1] // '';
my $i =
$p eq UP ? 0
: $p eq DOWN ? 1
: $p eq LEFT ? 2
: 3;
$t == HEAD ? (U_HEAD, D_HEAD, L_HEAD, R_HEAD)[$i]
: $t == BODY ? (U_BODY, D_BODY, L_BODY, R_BODY)[$i]
: (A_VOID);
} @{$_}
)
} @grid
);
}
sub move {
# Move the pen head
my ($y, $x) = @head_pos;
my $new_y = ($y + $dir->[0]) % $h;
my $new_x = ($x + $dir->[1]) % $w;
my $cell = $grid[$new_y][$new_x];
my $t = $cell->[0];
# Create a new head
$grid[$new_y][$new_x] = [HEAD, $dir];
# Replace the current head with body
$grid[$y][$x] = [BODY, $dir];
# Save the position of the head
@head_pos = ($new_y, $new_x);
}
ReadMode(3);
while (1) {
my $key;
until (defined($key = ReadLine(-1))) {
move();
display();
sleep($sleep);
}
if ($key eq "\e[A") { $dir = UP }
elsif ($key eq "\e[B") { $dir = DOWN }
elsif ($key eq "\e[C") { $dir = RIGHT }
elsif ($key eq "\e[D") { $dir = LEFT }
}