$options mode=extended, +optimize, optimize=-convert_percent-const_replace
$define VCOUNT = $D40B
$define TRIG0 = $D010

'title page
#start
?["{clr}Test your Atari Age!"]
?"Press fire as quickly as you can"
?"when the screen turns white."
?"After a number of attempts you"
?"will learn your Atari years."
exec press

points = 0
t = 10
while t                                         : 'Multiple tests to get it leveled
    do
        if peek(@TRIG0)
            exit
        endif
    loop      : ' wait for release
    poke 710,0 : ' BLACK
    ?
    ? ["wait for light"]
    i = rand(1000)+$ff                          : 'Random delay
    while i                
        if not peek(@TRIG0)                     : 'False start fire
            exit
        endif
        i=i-1
    wend

    if i
        print ["{bell}false start, points zeroed!!!"]
        points = 0
    else
        poke 710,15 : 'WHITE

        'Test speed
        poke 20,0
        do
            if not peek(@TRIG0)
                exit
            endif
        loop      : ' wait for fire
        total_time = peek(20)*20                        : ' reaction time in ms
        do
            if peek(@TRIG0)
                exit
            endif
        loop          : ' wait for release
        
        points = points + 2000-total_time
        print "Reaction time: ";total_time;" ms, points:";points

    endif

    t=t-1
wend

' Convert points to Atari years
' 0 points ==> 120 years
' 20000 points ==> 7 years
years = -113*points/20000 + 120
?
?"Final results:"
?"Total points: ";points
?"You are ";int(years+0.5);" Atari years young!"
exec press
go# start

proc press
    poke 710,0 : ' BLACK
    ?
    ?"Fire to continue"
    do
        if not peek(@TRIG0)
            exit
        endif
    loop      : ' wait for fire
endproc