#!/bin/sh # the next line restarts using wish \ exec wish "$0" "$@" # Written by Markus Triska (triska@gmx.at), 2005 # Public domain code set circlesize 100 set circlehalf [expr $circlesize/2] set canvassize 400 set centerx [expr $canvassize/2] set centery $centerx set middist 40 proc rand_c {} { set r [expr rand()] if {$r < 0.25} { return c1 } if {$r < 0.5} { return c2 } if {$r < 0.75} { return c3 } return c4 } set sequence {} set replay_index 0 array set normalcolors {c1 red c2 green c3 blue c4 yellow} array set blinkcolors {c1 #ffffff c2 #ffffff c3 #ffffff c4 #ffffff} set maincanvas [canvas .c -width $canvassize -height $canvassize] set seqlength [label .l -text 0 -font "*-Arial-bold-r-normal--20"] pack $maincanvas $seqlength $maincanvas create oval [expr $centerx - $circlehalf] \ [expr $centery - $middist - $circlesize] [expr $centerx + $circlehalf] \ [expr $centery - $middist] -fill $normalcolors(c1) -tags c1 $maincanvas create oval [expr $centerx + $middist] \ [expr $centery - $circlehalf] [expr $centerx + $middist + $circlesize] \ [expr $centery + $circlehalf] -fill $normalcolors(c2) -tags c2 $maincanvas create oval [expr $centerx - $circlehalf] \ [expr $centery + $middist] [expr $centerx + $circlehalf] \ [expr $centery + $middist + $circlesize] \ -fill $normalcolors(c3) -tags c3 $maincanvas create oval [expr $centerx - $middist - $circlesize] \ [expr $centery - $circlehalf] [expr $centerx - $middist] \ [expr $centery + $circlehalf] -fill $normalcolors(c4) -tags c4 foreach circle {c1 c2 c3 c4} { $maincanvas bind $circle "clicked $circle" } update proc clicked {circle} { global blinking replayed sequence replay_index if {$blinking} { return } # puts "clicked $circle" blink $circle 200 if {[lindex $sequence $replay_index] == $circle} { # correct incr replay_index } else { # wrong circle set sequence {} set replay_index 0 after 500 } if {$replay_index == [llength $sequence]} { set replay_index 0 set replayed 1 } } proc blink {circle {time 500}} { global maincanvas blinkcolors normalcolors # puts "blinking $circle with $blinkcolors($circle)" $maincanvas itemconfigure $circle -fill $blinkcolors($circle) update after $time $maincanvas itemconfigure $circle -fill $normalcolors($circle) update after $time } wm title . "Blinkory" wm protocol . WM_DELETE_WINDOW { exit } proc play_sequence {} { global blinking sequence replayed seqlength l set blinking 1 lappend sequence [rand_c] $seqlength configure -text [llength $sequence] foreach circle $sequence { blink $circle } set blinking 0 set replayed 0 tkwait variable replayed after 500 play_sequence } after 700 play_sequence