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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#!/usr/bin/env ruby
# $Id: rain.rb,v 1.6 2005-08-22 21:41:49 t-peters Exp $
# This program is a translation of the popular rain.c demo program from the
# ncurses library distribution.
#
# Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
#
# I do not impose any additional restrictions over the copyright of the
# ncurses library distribution. It has the following Copyright notice
#/****************************************************************************
# * Copyright (c) 1998 Free Software Foundation, Inc. *
# * *
# * Permission is hereby granted, free of charge, to any person obtaining a *
# * copy of this software and associated documentation files (the *
# * "Software"), to deal in the Software without restriction, including *
# * without limitation the rights to use, copy, modify, merge, publish, *
# * distribute, distribute with modifications, sublicense, and/or sell *
# * copies of the Software, and to permit persons to whom the Software is *
# * furnished to do so, subject to the following conditions: *
# * *
# * The above copyright notice and this permission notice shall be included *
# * in all copies or substantial portions of the Software. *
# * *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
# * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
# * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
# * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
# * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
# * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
# * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
# * *
# * Except as contained in this notice, the name(s) of the above copyright *
# * holders shall not be used in advertising or otherwise to promote the *
# * sale, use or other dealings in this Software without prior written *
# * authorization. *
# ****************************************************************************/
require "ncursesw"
# A class responsible for raindrop drawing
class Raindrop
def initialize (window, color_pair = nil)
@window = window
@color_pair = color_pair
lines = []
columns = []
window.getmaxyx(lines,columns)
lines = (lines[0] <= 4) ? 1 : (lines[0] - 4)
columns = (columns[0] <= 4) ? 1 : (columns[0] - 4)
@current_phase = 0
@x = rand(columns)+2
@y = rand(lines)+2
end
# draw_next_phase draws the next phase of a raindrop. If this was the last
# phase, returns 0, otherwise returns the raindrop.
def draw_next_phase
if (@color_pair)
if Ncurses.respond_to?(:color_set)
@window.color_set(@color_pair, nil)
else
@window.attrset(Ncurses.COLOR_PAIR(@color_pair))
end
end
if (DRAWING_PROCS[@current_phase].call(@window,@y,@x))
@current_phase += 1
self
end
end
DRAWING_PROCS = [
Proc.new{|window,y,x|
window.mvaddstr(y,x, ".")
},
Proc.new{|window,y,x|
window.mvaddstr(y, x, "o")
},
Proc.new{|window,y,x|
window.mvaddstr(y, x, "O")
},
Proc.new{|window,y,x|
window.mvaddstr(y-1, x, "-")
window.mvaddstr(y, x-1, "|.|")
window.mvaddstr(y+1, x, "-")
},
Proc.new{|window,y,x|
window.mvaddstr(y-2, x, "-")
window.mvaddstr(y-1, x-1, "/ \\")
window.mvaddstr(y, x-2, "| O |")
window.mvaddstr(y+1, x-1, "\\ /")
window.mvaddstr(y+2, x, "-")
},
Proc.new{|window,y,x|
window.mvaddstr(y-2, x, " ")
window.mvaddstr(y-1, x-1, " ")
window.mvaddstr(y, x-2, " ")
window.mvaddstr(y+1, x-1, " ")
window.mvaddstr(y+2, x, " ")
nil # signal the last raindrop phase
}
]
NUMBER_OF_PHASES = DRAWING_PROCS.size - 1
end
# This class creates raindrops and tells them to draw on the screen
class Rain
AVERAGE_RAINDROP_SPACE = 475.1 # 4 simultaneous raindrops in a 80x24 Window
def Rain.sigwinch_handler(sig = nil)
ObjectSpace.each_object(Rain){|rain|
rain.window_size_changed = true
}
end
attr_writer :window_size_changed
def initialize(window)
@window = window
@window_size_changed = true
@raindrops = []
@has_colors = Ncurses.has_colors?
if (@has_colors)
@current_color = 1
end
end
def fall_for_a_moment
adjust_to_new_window_size if (@window_size_changed)
current_number_of_new_raindrops.times{
if (@has_colors)
@raindrops.push(Raindrop.new(@window, @current_color))
@current_color = 3 - @current_color # alternate between 1 and 2
else
@raindrops.push(Raindrop.new(@window))
end
}
@raindrops = @raindrops.collect{|raindrop|
raindrop.draw_next_phase
}.compact # erase raindrops that have expired from the list
end
def adjust_to_new_window_size
@window_size_changed = false
window_size = @window.getmaxx * @window.getmaxy
average_number_of_raindrops = window_size / AVERAGE_RAINDROP_SPACE
@average_number_of_new_raindrops =
average_number_of_raindrops / Raindrop::NUMBER_OF_PHASES
end
def current_number_of_new_raindrops
num_floor = @average_number_of_new_raindrops.floor
num_ceil = @average_number_of_new_raindrops.ceil
chance = @average_number_of_new_raindrops - num_floor
if (rand > chance)
num_floor
else
num_ceil
end
end
def fall(pause = 0.1)
begin
fall_for_a_moment
@window.refresh
sleep(pause)
end while (true)
end
end
Ncurses.initscr
begin
if (Ncurses.has_colors?)
bg = Ncurses::COLOR_BLACK
Ncurses.start_color
if (Ncurses.respond_to?("use_default_colors"))
if (Ncurses.use_default_colors == Ncurses::OK)
bg = -1
end
end
Ncurses.init_pair(1, Ncurses::COLOR_BLUE, bg);
Ncurses.init_pair(2, Ncurses::COLOR_CYAN, bg);
end
Ncurses.nl()
Ncurses.noecho()
Ncurses.curs_set(0)
Ncurses.stdscr.nodelay(true)
rain = Rain.new(Ncurses.stdscr)
begin
case(Ncurses.getch())
when 'q'.ord, 'Q'.ord
Ncurses.curs_set(1)
Ncurses.endwin()
exit
when 's'.ord
Ncurses.stdscr.nodelay(false)
when ' '.ord
Ncurses.stdscr.nodelay(true)
when Ncurses::KEY_RESIZE
Rain.sigwinch_handler
end
sleep(0.050)
rain.fall_for_a_moment
Ncurses.refresh
end while true
ensure
Ncurses.curs_set(1)
Ncurses.endwin()
end
|