Computer Science Canada Need to be able to run if statement during delay. |
Author: | fuscia [ Sat May 13, 2017 2:32 pm ] | ||
Post subject: | Need to be able to run if statement during delay. | ||
What is it you are trying to achieve? I am trying to make a duel/draw speed game where the user has to draw their gun after the program outputs "Draw!", as fast as they can. If they draw before this time the game should output "you drew too fast". What is the problem you are having? I have been using a randomized delay using the delay() function (I know, not good to use) and putting a random int between 1 and 3 seconds in it. My problem is that during this delay I cant execute anything, so I cannot run my if statement that says if the user presses E then put "you drew too fast". Describe what you have tried to solve this problem I have looked throughout the forums and haven't found anything helpful in my situation Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) I put the problematic part as comments.
Please specify what version of Turing you are using 4.11 |
Author: | Insectoid [ Sat May 13, 2017 2:48 pm ] |
Post subject: | RE:Need to be able to run if statement during delay. |
instead of using delay(), you can enter a loop that checks for key presses that exits when a key is pressed OR when enough time passes. Time.Elapsed() will return the number of milliseconds since the program started running. You can use this and some basic math to calculate how much time has passed since a given point in your program. |
Author: | fuscia [ Sat May 13, 2017 6:13 pm ] |
Post subject: | RE:Need to be able to run if statement during delay. |
can you give an example of the second part? |
Author: | Insectoid [ Sun May 14, 2017 4:50 am ] |
Post subject: | RE:Need to be able to run if statement during delay. |
Nope, that's the part you need to figure out on your own. Time.Elapsed returns an integer. That means you can save it to a variable, add or subtract other numbers to it, compare it to other numbers, etc. If I woke up at 5:30, and it's currently 5:50, how long have I been awake? |
Author: | fuscia [ Sun May 14, 2017 2:48 pm ] |
Post subject: | RE:Need to be able to run if statement during delay. |
Thanks, got it working. |