1 /// **************************************************************************
2 /// Copyright (c) 2002 Free Software Foundation, Inc.                        *
3 ///                                                                          *
4 /// Permission is hereby granted, free of charge, to any person obtaining a  *
5 /// copy of this software and associated documentation files (the            *
6 /// "Software"), to deal in the Software without restriction, including      *
7 /// without limitation the rights to use, copy, modify, merge, publish,      *
8 /// distribute, distribute with modifications, sublicense, and/or sell       *
9 /// copies of the Software, and to permit persons to whom the Software is    *
10 /// furnished to do so, subject to the following conditions:                 *
11 ///                                                                          *
12 /// The above copyright notice and this permission notice shall be included  *
13 /// in all copies or substantial portions of the Software.                   *
14 ///                                                                          *
15 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 /// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 /// IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 /// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 /// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 /// THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 ///                                                                          *
23 /// Except as contained in this notice, the name(s) of the above copyright   *
24 /// holders shall not be used in advertising or otherwise to promote the     *
25 /// sale, use or other dealings in this Software without prior written       *
26 /// authorization.                                                           *
27 /// *************************************************************************
28 
29 extern crate pancurses;
30 extern crate rand;
31 
32 use pancurses::*;
33 use rand::Rng;
34 
35 fn next_j<T: Rng>(mut j: usize, rng: &mut T, window: &Window) -> usize {
36     if j == 0 {
37         j = 4;
38     } else {
39         j -= 1;
40     }
41 
42     if has_colors() {
43         let z = rng.gen::<chtype>() % 3;
44         let mut color = COLOR_PAIR(z);
45 
46         if z != 0 {
47             color |= A_BOLD;
48         }
49 
50         window.attrset(color);
51     }
52 
53     j
54 }
55 
56 fn main() {
57     let window = initscr();
58 
59     let mut rng = rand::thread_rng();
60 
61     if has_colors() {
62         let mut bg = COLOR_BLACK;
63 
64         start_color();
65         if use_default_colors() == OK {
66             bg = -1;
67         }
68 
69         init_pair(1, COLOR_BLUE, bg);
70         init_pair(2, COLOR_CYAN, bg);
71     }
72 
73     nl();
74     noecho();
75     curs_set(0);
76     window.timeout(0);
77     window.keypad(true);
78 
79     let mut r = window.get_max_y() - 4;
80     let mut c = window.get_max_x() - 4;
81 
82     let mut xpos = [0; 5];
83     let mut ypos = [0; 5];
84 
85     for j in (0..5).rev() {
86         xpos[j] = rng.gen::<i32>() % c + 2;
87         ypos[j] = rng.gen::<i32>() % r + 2;
88     }
89 
90     let mut j = 0;
91 
92     loop {
93         let x = rng.gen::<i32>() % c + 2;
94         let y = rng.gen::<i32>() % r + 2;
95 
96         window.mvaddch(y, x, '.');
97 
98         window.mvaddch(ypos[j], xpos[j], 'o');
99 
100         j = next_j(j, &mut rng, &window);
101         window.mvaddch(ypos[j], xpos[j], 'O');
102 
103         j = next_j(j, &mut rng, &window);
104         window.mvaddch(ypos[j] - 1, xpos[j], '-');
105         window.mvaddstr(ypos[j], xpos[j] - 1, "|.|");
106         window.mvaddch(ypos[j] + 1, xpos[j], '-');
107 
108         j = next_j(j, &mut rng, &window);
109         window.mvaddch(ypos[j] - 2, xpos[j], '-');
110         window.mvaddstr(ypos[j] - 1, xpos[j] - 1, "/ \\");
111         window.mvaddstr(ypos[j], xpos[j] - 2, "| O |");
112         window.mvaddstr(ypos[j] + 1, xpos[j] - 1, "\\ /");
113         window.mvaddch(ypos[j] + 2, xpos[j], '-');
114 
115         j = next_j(j, &mut rng, &window);
116         window.mvaddch(ypos[j] - 2, xpos[j], ' ');
117         window.mvaddstr(ypos[j] - 1, xpos[j] - 1, "   ");
118         window.mvaddstr(ypos[j], xpos[j] - 2, "     ");
119         window.mvaddstr(ypos[j] + 1, xpos[j] - 1, "   ");
120         window.mvaddch(ypos[j] + 2, xpos[j], ' ');
121 
122         xpos[j] = x;
123         ypos[j] = y;
124 
125         match window.getch() {
126             Some(Input::Character(q)) if q == 'q' || q == 'Q' => {
127                 curs_set(1);
128                 endwin();
129                 return;
130             }
131             Some(Input::Character('s')) => {
132                 window.nodelay(false);
133             }
134             Some(Input::Character(' ')) => {
135                 window.nodelay(true);
136             }
137             Some(Input::KeyResize) => {
138                 resize_term(0, 0);
139                 window.erase();
140                 r = window.get_max_y() - 4;
141                 c = window.get_max_x() - 4;
142             }
143             _ => {}
144         }
145         napms(50);
146     }
147 }
148