Need to be able to run if statement during delay.
Author |
Message |
fuscia
|
Posted: 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.
Turing: |
import GUI
var window : int
window := Window.Open ("title:Western Duel;position:center;middle;graphics:700;500")
% variables %
%-------------------------%
var ClassicModeButton, StoryModeButton : int
var InstructionsButton : int
var MainMenuButton : int
var BestOfFiveButton, BestOfTenButton, tomenu : int
var drawtime, enemytime : int
var chars : array char of boolean
var usertime : int := 0
var earlydraw : boolean
var font1 := Font.New ("Times New Roman:25:Bold")
var font2 := Font.New ("Times New Roman:13:Bold")
var font3 := Font.New ("Times New Roman:25:Bold")
%-------------------------%
% procs %
%-------------------------%
proc BestOfFive
end BestOfFive
proc BestOfTen
end BestOfTen
proc StoryMode
end StoryMode
% randint (drawtime, 1000, 3000)
%
% proc drawcheck
%
%
% Input.KeyDown (chars)
% if chars ('e') then
% Font.Draw ("Bam!", 530, 130, font2, black)
% end if
%
% earlydraw := true
%
% Input.KeyDown (chars)
% if earlydraw = true and chars ('e') then
% put "you drew too early"
% return
% end if
%
%
% delay (drawtime)
% Font.Draw ("Draw!", 300, 350, font1, black)
%
% earlydraw := false
loop
delay (1)
usertime := usertime + 1
randint (enemytime, 350, 650)
if usertime > enemytime then
put "you lose!"
exit
end if
Input.KeyDown (chars )
if chars ('e') then
Font.Draw ("Bam!", 530, 130, font2, black)
exit
end if
end loop
loop
if usertime < enemytime then
put "you win!"
put "you drew in ", usertime - 1, " milliseconds"
exit
elsif usertime = enemytime then
put "tie!"
exit
end if
end loop
end drawcheck
proc classicmode
Music.PlayFileStop
locate (1, 1)
cls
Font.Draw ("On my mark!", 50, 150, font2, black)
drawfilloval (550, 100, 15, 15, black)
drawfilloval (75, 100, 15, 15, black)
delay (1500)
cls
drawfilloval (75, 100, 15, 15, black)
drawfilloval (550, 100, 15, 15, black)
drawcheck
end classicmode
%-------------------------%
% menu screen %
%-------------------------%
proc menu
loop
GUI.Hide (tomenu )
GUI.Dispose (tomenu )
cls
var title := Pic.FileNew ("Title.gif")
var background := Pic.FileNew ("menubackground.gif")
Music.PlayFileLoop ("theme.mp3")
Pic.Draw (background, 0, 0, picMerge)
Pic.Draw (title, 290, 270, picMerge)
ClassicModeButton := GUI.CreateButton (310, 250, 0, " Classic Mode", classicmode )
StoryModeButton := GUI.CreateButton (310, 220, 0, " Story Mode ", StoryMode )
BestOfFiveButton := GUI.CreateButton (310, 190, 0, " Best of Five ", BestOfFive )
BestOfTenButton := GUI.CreateButton (310, 160, 0, " Multiplayer ", BestOfTen )
loop
exit when GUI.ProcessEvent
end loop
end loop
end menu
%-------------------------%
proc Instructions
drawfillbox (0, 0, maxx, maxy, black)
locate (1, 1)
Font.Draw ("When the game ouputs 'Draw!' in the middle of the screen; press the E button as fast as possible", 0, maxy div 2, font2, 40)
tomenu := GUI.CreateButton (310, 160, 0, " To Menu ", menu )
loop
exit when GUI.ProcessEvent
end loop
GUI.Hide (tomenu )
GUI.Dispose (tomenu )
cls
end Instructions
Instructions
loop
exit when GUI.ProcessEvent
end loop
|
Please specify what version of Turing you are using
4.11 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: 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. |
|
|
|
|
|
fuscia
|
Posted: 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? |
|
|
|
|
|
Insectoid
|
Posted: 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? |
|
|
|
|
|
fuscia
|
Posted: Sun May 14, 2017 2:48 pm Post subject: RE:Need to be able to run if statement during delay. |
|
|
Thanks, got it working. |
|
|
|
|
|
|
|