[Keith Vetter] 2003-05-04 : a simple little whizzlet simulating rain drops on a window. Much like the old unix game rain. [http://mini.net/sdarchive/rainstorm.jpg] ---- [Jacob Levy] 05/05/2003 I keep getting error messages (in "deluge" mode mostly) about ::R(somenumber) not being there when incr is applied. The problem is probably collisions due to the ID computation based on rand(), in proc RainDrop. If ::R($id,x) already exists then we should probably go back and compute another ID. [KPV] 05/06/2003 : thanks for the fix. I took the liberty of merging the code. [Jacob Levy] 05/06/2003 Urk... the version below is (again) missing my fix... [KPV] 05/06/2003 : true, but it contains a simpler fix [etdxc] Sep 08 2003 : I received an error when in deluge mode. Wish84 has caused an error in DIBENG.DLL Its probably something to do with my 'Windows Me' and not your code but I thought I'de let your know. Btw, can I use this code in a project I'm working on ? [KPV] of course, everything I put on Wiki is for all to use freely. [DKF]: Is it necessary to create and delete all those ovals? Couldn't you use [[$w coords]] to resize them instead? (You also leak tags; better to not set a tag and just store the id in a variable.) ---- ##+########################################################################## # # RainStorm.tcl - Simulates rain drops on a window # by Keith Vetter, May 2, 2003 # ##+########################################################################## ############################################################################# package require Tk set S(delay) 80 set S(rain) 3 set S(id) 0 array set SIZES {0 1 1 2 2 4 3 6 4 8 5 10 6 12 7 14 8 16} array set DROPS {0 {Mist 500} 1 {Sprinkles 200} 2 {Shower 100} 3 {Rain 50} 4 {Storm 25} 5 {Down\ Pour 10} 6 {Deluge 1}} wm title . "Rain Storm" canvas .c -relief raised -borderwidth 0 -height 500 -width 500 scale .rain -orient h -variable S(rain) -command Rain -showvalue 0 -from 0 -to 6 image create photo ::img::blank -width 1 -height 1 button .about -image ::img::blank -highlightthickness 0 -command \ [list tk_messageBox -message "Rain Storm\nby Keith Vetter, May 2003"] pack .c -side top -fill both -expand 1 place .rain -in .c -relx 1 -rely 1 -anchor se place .about -in .c -relx 1 -rely 1 -anchor se bind all {console show} proc Rain {args} { after cancel Rain .rain config -label [lindex $::DROPS($::S(rain)) 0] RainDrop after [lindex $::DROPS($::S(rain)) 1] Rain } proc RainDrop {{id {}}} { .c delete d$id if {$id == {}} { ;# New drop set id [incr ::S(id)] set ::R($id,x) [expr {round([winfo width .c] * rand())}] set ::R($id,y) [expr {round([winfo height .c] * rand())}] set ::R($id,step) -1 } set n [incr ::R($id,step)] if {! [info exists ::SIZES($n)]} { array unset ::R $id,* } else { .c create oval [box $::R($id,x) $::R($id,y) $::SIZES($n)] -tag d$id after $::S(delay) [list RainDrop $id] } } proc box {x y r} { return [list [expr {$x-$r}] [expr {$y-$r}] [expr {$x+$r}] [expr {$y+$r}]] } Rain ---- [Category Application] | [Category Graphics] | [Category Whizzlet] | [Category Animation]