Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help! How to Run a Timer and Other Commands at the Same Time?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MatthewZMD




PostPosted: Tue Jan 06, 2015 1:05 am   Post subject: Help! How to Run a Timer and Other Commands at the Same Time?

What is it you are trying to achieve?
I am making a game, I want to make the user click on an image for a number of times within a limited time.


What is the problem you are having?
I cannot make the timer and clicking image run at the same time.


Describe what you have tried to solve this problem
I tried to create a loop for the timer and then write the codes for clicking the button, but the program will run the timer first...
I also tried to make everything into 1 loop, but the timer and the clicking button countdown decrease at the same pace...

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


%First try
var rangeX, rangeY, button : int
var ending : boolean := false
var healthPoint, timeLeft : int
var notused1, notused2 : int
healthPoint := 50
timeLeft := 20
loop
    timeLeft := timeLeft - 1
    put healthPoint
    put timeLeft
    delay (1000)
    cls
end loop
loop
    buttonwait ("down", rangeX, rangeY, notused1, notused2)
    drawfillbox (250, 300, 600, 700, blue)
    View.Update
    if rangeX >= 250 and rangeX <= 600 and rangeY >= 300 and rangeY <= 700 then
        healthPoint := healthPoint - 1
    end if
    exit when healthPoint < 1
end loop


%Second try
var rangeX, rangeY, button : int
var ending : boolean := false
var healthPoint, timeLeft : int
var notused1, notused2 : int
healthPoint := 50
timeLeft := 20
loop
    buttonwait ("down", rangeX, rangeY, notused1, notused2)
    timeLeft := timeLeft - 1
    drawfillbox (250, 300, 600, 700, blue)
    put healthPoint
    put timeLeft
    View.Update
    if rangeX >= 250 and rangeX <= 600 and rangeY >= 300 and rangeY <= 700 then
        healthPoint := healthPoint - 1
    end if
    exit when healthPoint < 1
    delay (1000)
    cls
end loop



Please specify what version of Turing you are using
Open Turing newest version I suppose
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Jan 06, 2015 2:19 am   Post subject: RE:Help! How to Run a Timer and Other Commands at the Same Time?

I love that you posted both of your attempts -- this is a question well asked!

You are on the right path with your idea of putting both steps into a single loop. You want this loop to quickly check one of the steps, then another, then back to first. What you have getting in the way of the "quickly" part are "blocking calls" -- calling some functions that will block / wait.

E.g. of something that will block a loop:
code:

loop
   % main loop of a game
   get user_input
end loop

Because the loop stops and waits for a person to type in that input, asking for just one word from a 60 words-per-minute typist will make the game run no faster than 1 FPS. Unacceptable!

Lucky, you can make some changes in your code:

From buttonwait to Mouse.Where
From delay(1000) to delay(much_smaller_number)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MatthewZMD




PostPosted: Sat Jan 10, 2015 3:43 am   Post subject: RE:Help! How to Run a Timer and Other Commands at the Same Time?

Thank you! The problem is currently fixed now. Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: