summaryrefslogtreecommitdiffhomepage
path: root/contrib/luacurses/test/rain.lua
blob: 35e46910210f0c0e506a902ae8ee41f554338db8 (plain)
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

require("curses");

curses.initscr();
curses.nl();
curses.noecho();


if (curses.has_colors()) then
    curses.start_color();
    curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK);
    curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK);
end

curses.curs_set(0);
curses.timeout(0);

math.randomseed(os.time());

lines = curses.LINES();
cols = curses.COLS();

xpos = {};
ypos = {};
r = lines - 4;
c = cols - 4;
for i = 0, 4 do
  xpos[i] = c * math.random() + 2;
  ypos[i] = r * math.random() + 2;
end

function dec(i, max)
    if (curses.has_colors()) then
	local z = 3 * math.random();
	local c = curses.COLOR_PAIR(z);
	curses.attrset(c);
	if (math.floor(z) > 0) then
	    curses.attron(curses.A_BOLD);
	end
    end

    if (i > 0) then return i - 1;
    else return max;
    end
end

i = 0;
while(true) do
    x = c * math.random() + 2;
    y = r * math.random() + 2;

    curses.mvaddstr(y, x, ".");

    curses.mvaddstr(ypos[i], xpos[i],           "o");

    i = dec(i, 4);
    curses.mvaddstr(ypos[i], xpos[i],           "O");

    i = dec(i, 4);
    curses.mvaddstr(ypos[i] - 1, xpos[i],       "-");
    curses.mvaddstr(ypos[i],     xpos[i] - 1,  "|.|");
    curses.mvaddstr(ypos[i] + 1, xpos[i],       "-");

    i = dec(i, 4);
    curses.mvaddstr(ypos[i] - 2, xpos[i],       "-");
    curses.mvaddstr(ypos[i] - 1, xpos[i] - 1,  "/ \\");
    curses.mvaddstr(ypos[i],     xpos[i] - 2, "| O |");
    curses.mvaddstr(ypos[i] + 1, xpos[i] - 1, "\\ /");
    curses.mvaddstr(ypos[i] + 2, xpos[i],       "-");

    i = dec(i, 4);
    curses.mvaddstr(ypos[i] - 2, xpos[i],       " ");
    curses.mvaddstr(ypos[i] - 1, xpos[i] - 1,  "   ");
    curses.mvaddstr(ypos[i],     xpos[i] - 2, "     ");
    curses.mvaddstr(ypos[i] + 1, xpos[i] - 1,  "   ");
    curses.mvaddstr(ypos[i] + 2, xpos[i],       " ");


    xpos[i] = x;
    ypos[i] = y;
    
    local ch = curses.getch();
    if (ch == string.byte('q', 1)) or (ch == string.byte('Q', 1)) then break; end
    curses.refresh();
    curses.napms(50);
end

curses.endwin();