Updated 2016-04-27 19:06:09 by gold

Arjen Markus (10 july 2003) A very quick script to make procedures work independently, to simulate the concept of sprites. Well, I was lazy and there is only one procedure that moves coloured balls around, but it is invoked for each ball separately in a way that is outside the flow of the script itself.

You will enjoy turning this into independent agents in a game :)

If you want Brownian movement: see the comments
 # A quick attempt at independently called procs
 # - let us call them sprites
 #
 proc mkSprite { colour speed } {
    global speed_data
    set id [.c create oval 0 0 10 10 -fill $colour]
    set speed_data($id) $speed
    moveSprite $id
 }
 proc moveSprite { id } {
    global speed_data
    set speed $speed_data($id)
    set coords [.c coords $id]
    .c move $id [expr {rand()*$speed}] [expr {rand()*$speed}]

    # 
    # Use this one for Brownian movement 
    #
    # .c move $id [expr {(rand()-0.5)*$speed}] [expr {(rand()-0.5)*$speed}]
    #

    if { [lindex $coords 0] <   0 } {
       .c move $id  200 0
    }
    if { [lindex $coords 1] <   0 } {
       .c move $id 0  200
    }
    if { [lindex $coords 2] > 200 } {
       .c move $id -200 0
    }
    if { [lindex $coords 3] > 200 } {
       .c move $id 0 -200
    }
    after 10 [list moveSprite $id]
 }

 canvas .c -width 200 -height 200 -background white
 pack   .c -fill both
 foreach {colour speed} {red 10  green 20  blue 3} {
    mkSprite $colour $speed
 }

Screenshots Section

figure 1.

figure 2.

gold added pix