Timer working alongside a get statement
Author |
Message |
GenesisXCS
|
Posted: Tue Apr 27, 2010 11:45 am Post subject: Timer working alongside a get statement |
|
|
Greetings to everyone =)
The program I am making is a Math Game. It is in Text Form right now, and I am planning to migrate it (not sure if that's correct terminology, w/e) to graphics mode. (that is still under plan). What actually happens are simple multiplication, division, addition, subtraction are asked, and the user needs to input an answer within the timelimit. The faster, the more points; the higher the answer, the more points. However, I cannot get a timer to work alongside a get statement (the processing halts as the program waits for user input). Is there an alternate way? Even if this doesn't work, can I get a code that times how long it takes for a user to input the answer?
What is it you are trying to achieve?
I am trying to make a timer that runs in parallel (if you may) with a get statement. I am not sure if this is possible. I have another plan that I think may work, but I am not totally sure yet.
Is it possible for the program to check for userinput every loop; so fast that we don't know that it is even checking, rather than halting the whole program waiting for an input??
What is the problem you are having?
Program halts as soon as the program waits for user input.
Describe what you have tried to solve this problem
I've (kind have) played around with the TimeElapsed code ( I believe). I am not totally sure how the various Time Functions work. Some go by the frequency of the CPU (I think), while some go by the clock.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Don't have anything right now worth mentioning. (There is a get statement)
Please specify what version of Turing you are using
Turing 4.1
Thanks in advance, I really appreciate any help I can get. This is my first game
Cheers
GenesisXCS
EDIT: The plan was in graphics mode, when a graphic hits the bottom of the screen such as (if x_arrow=25 then points := 0) or something like that. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
USEC_OFFICER
|
Posted: Tue Apr 27, 2010 11:53 am Post subject: RE:Timer working alongside a get statement |
|
|
Time.SinceLast. Look it up. It should be what you are looking for. |
|
|
|
|
|
TheGuardian001
|
Posted: Tue Apr 27, 2010 12:21 pm Post subject: Re: RE:Timer working alongside a get statement |
|
|
USEC_OFFICER @ Tue Apr 27, 2010 11:53 am wrote: Time.SinceLast. Look it up. It should be what you are looking for.
I believe you mean time_delaysincelast.
However I'm pretty sure that won't do what they want. What that will do is make sure that inputting the answer takes at least 10 seconds (as an example). It won't, however, actually tell you how long they took to complete answering the question, nor stop them from going over the allotted time while answering.
What you could do would be to use a loop in combination with hasch and with Time.Elapsed.
The process for doing so is basically:
1)right before the question starts, create a variable with the value of Time.Elapsed. This will tell you what time they started answering the question at (in milliseconds).
2)Start a loop. This loop will get input using getchar(), and check how long they've had to answer using Time.Elapsed. It should look something like:
Turing: |
var startTime : int := Time.Elapsed
var ans : string := ""
loop
if hasch then
ans + = getchar()
end
var currentTime : int := Time.Elapsed
exit when currentTime - startTime > 10000
end loop
|
The hasch will see if they're entering anything, and currentTime - startTime will give you the time they've had to answer. If they go over the time limit (in this case 10000 mS, or 10 seconds), you exit the loop. You should also exit the loop if the last character of ans is KEY_ENTER |
|
|
|
|
|
USEC_OFFICER
|
Posted: Tue Apr 27, 2010 2:53 pm Post subject: RE:Timer working alongside a get statement |
|
|
That's what I meant (Though that's moot point now.) I never really used the Time. Functions though. Oh well. |
|
|
|
|
|
andrew.
|
Posted: Tue Apr 27, 2010 3:34 pm Post subject: RE:Timer working alongside a get statement |
|
|
An easy way to do it is to get the time before the get and then after. After you subtract the two, you have to time it took to run. This is similar to what TheGuardian said.
Turing: | var startTime : int := Time.Elapsed
var currentTime : int
get myString : *
currentTime := Time.Elapsed
put "The time it took for you to type is: " + intstr(currentTime - startTime ) |
|
|
|
|
|
|
copthesaint
|
Posted: Tue Apr 27, 2010 8:38 pm Post subject: Re: Timer working alongside a get statement |
|
|
Heres a different way to do this:
Turing: | %without msdos the fun flickering thing wont appear as a block :)
View.Set ("Graphics,OffscreenOnly, msdos")
const REPEATRATE : int := 100
var enteredString : string := ""
%creates a set type for characters
type letterSet : set of char
var avalibleInputs : letterSet :=
letterSet (' ')
%adds values in the set
for i : '!' .. '~'
avalibleInputs + = letterSet (i )
end for
%2 vars, one for time and one for flicker rate
var t, flicker : int := 0
loop
%if the current time is greater or equal to the repeat rate (in ms) then
if Time.ElapsedCPU - t >= REPEATRATE then
flicker + = 1
%if you type a key
if hasch then
%get the key
var ch : char := getchar
%if the key is in the set
if ch in avalibleInputs then
%add the key to the variable
enteredString + = ch
%if the key is backspace
elsif ch = chr (8) /*BackSpace*/ then
%if there is a value to delete
if not (length (enteredString ) = 0) then
%delete one char
enteredString := enteredString (1 .. * - 1)
end if
end if
%exits the loop when enter is pressed.
exit when (ch = chr (10)) = true
end if
%removes the stored value
Input.Flush
%puts the stored string
put enteredString ..
%checks flicker timer
if flicker = 10 then
%puts the flicker timer char
put chr (219) ..
%resets the flicker timer
flicker := 0
end if
%finish the put line
put ""
%update the screen
View.Update
%clears the screen
cls
%resets the repeat timer
t := Time.ElapsedCPU
end if
end loop
%puts the final string
put enteredString
%updates the screen one last time.
View.Update
|
Its all commented so it should be easy enough to follow through.
I thought it was a good break making this, until turing freaking had a meltdown and shut down 3 times in row :/
unexpected environment errors...
FTL |
|
|
|
|
|
GenesisXCS
|
Posted: Wed Apr 28, 2010 1:51 pm Post subject: RE:Timer working alongside a get statement |
|
|
Thank you all for your lengthy and EXTREMELY helpful responses. This is a good starting point for me, and I will definitely put this into practise; once I absorb everything you've written down. +D
Thanks again! |
|
|
|
|
|
|
|