Version 15 of A minimal starfield

Updated 2005-05-18 16:52:03

if 0 {Richard Suchenwirth 2005-05-17 - In a few leisurely minutes at work, I hacked together these few LOC that give you kind of a starfield animation by scaling ovals on a canvas :) Featuritis demanded different colors, and user-controllable speed (<Up> and <Down> cursor keys), but it's still pretty small.

http://mini.net/files/stars.jpg }

 package require Tk
 proc stars'go {c factor} {
     set w [winfo width $c]
     set h [winfo height $c] 
     $c scale all [expr {$w/2}] [expr {$h/2}] $factor $factor 
     foreach item [$c find all] {
         if {[llength [$c bbox $item]] == 0} {$c delete item; continue} ;# (1)
         foreach {x0 y0 x1 y1} [$c bbox $item] break
         if {$x1<0 || $x0>$w || $y1<0 || $y0>$h} {$c delete $item}
     }
     time {
         set x [expr {rand()*$w}]
         set y [expr {rand()*$h}]
         set col [lpick {white yellow beige bisque cyan}]
         $c create oval $x $y [expr {$x+1}] [expr {$y+1}] -fill $col \
                 -outline $col
     } 10
     after $::ms [info level 0]
 }
 proc lpick list {lindex $list [expr {int(rand()*[llength $list])}]}

#-- Let's go!

 pack [canvas .c -bg black] -fill both -expand 1
 set ms 40
 bind . <Up> {incr ms -5}
 bind . <Down> {incr ms 5}
 stars'go .c 1.05

if 0 {


Neat, but I flew through a star and it crashed. Will have to add code to protect against the ovals from getting too large. Earl Johnson - RS: How did it crash? If a star starts exactly from the centre, it will grow very big - but be deleted when it exceeds one of the canvas boundaries.

After more testing I have not been able to reproduce the problem. I guess that what I saw was what MAK saw but it just happen to happen while I was flying through a sun. Earl Johnson

MAK

 can't read "x1": no such variable
 can't read "x1": no such variable
    while executing
 "if {$x1<0 || $x0>$w || $y1<0 || $y0>$h} {$c delete $item}"
    (procedure "stars'go" line 7)
    invoked from within
 "stars'go .c 1.05"
    ("after" script)

RS: Interesting - that means there was a canvas item with an empty bounding box, so the

         foreach {x0 y0 x1 y1} [$c bbox $item] break

command didn't set any of these four variables. This is documented in the canvas man page: "If no items match any of the tagOrId arguments or if the matching items have empty bounding boxes (i.e. they have nothing to display) then an empty string is returned." Thanks for the hint (though I still don't understand why an oval should have nothing to display...) Added a safety belt, see #(1) above.

MG thinks there's a mistake in your change - should that $c delete item be $c delete $item?


Category Animation - Arts and crafts of Tcl-Tk programming}