Computer Science Canada

Time counting down simultaneously

Author:  MysticVegeta [ Tue May 24, 2005 4:29 pm ]
Post subject:  Time counting down simultaneously

Hi, There is a section in my program that asks for user input but at the same time, the seconds decrease. How can i make both of them work at the same time without using processes. Processes screw the user input location so bad... Crying or Very sad

Author:  Cervantes [ Tue May 24, 2005 4:51 pm ]
Post subject: 

That depends on how you're getting the input. If you're using get, well, let's not go there. If you're using getch(), that's a little better, because at least you could update your timer in the time between the user's keystrokes! However, the best way to do this is wrap your getch() statement inside an if hasch statement. Thus, the getch() code is only executed if there is a key waiting in the buffer.
code:

if hasch then
   getch (input)
end if

Expanding this to include adding the getch() to a variable and adding a timer, we get something like this:
Turing:

View.Set ("offscreenonly")
var input := ""
var _char : string (1)
var lastTime := -1000
var counter := 11
var fontID := Font.New ("Arial:16")

loop

    if hasch then
        getch (_char)
        exit when ord (_char) = 10  %enter
        if ord (_char) = 8 then %backspace
            if input ~= "" then
                input := input (1 .. * -1)  %chomp off the last char
            end if
        else
            input += _char
        end if
    end if
    if Time.Elapsed > lastTime + 1000 then              %updates every second (1000 milliseconds)
        lastTime := Time.Elapsed
        counter -= 1
    end if

    cls
    Font.Draw (input, 100, 200, fontID, black)
    Font.Draw (intstr (counter), 100, 100, fontID, black)
    View.Update
    delay (10)

    exit when counter <= 0
end loop

Author:  MysticVegeta [ Tue May 24, 2005 4:58 pm ]
Post subject: 

How about if i using get Confused

Author:  Cervantes [ Tue May 24, 2005 5:33 pm ]
Post subject: 

I don't think you can. You will, most definately, have to switch to the getch/hasch combo.

Author:  MysticVegeta [ Tue May 24, 2005 5:42 pm ]
Post subject: 

But its a whole word... Thinking
processes... Crying or Very sad

Author:  Cervantes [ Tue May 24, 2005 5:58 pm ]
Post subject: 

MysticVegeta wrote:
But its a whole word... Thinking

Yes, but a word is composed of letters. If you think of how exactly you type a word out on the keyboard, you press one key at a time in a specific order so you produce a word. To model that, we need to getch() one key at a time in the order that they come to produce a specific word.

Sure, get is meant for things like this, but that doesn't mean it's the way to go. Look at Hitler: he meant to take over Europe (and more!) but he failed. He failed, just like get will fail you with this.

Oh, the analogies. Hitler is a programming command? omgwtfbbq. Eh

Author:  MysticVegeta [ Wed May 25, 2005 3:20 pm ]
Post subject: 

hmm i think i know what you mean, i will try to do something like that.


: