Hi there!
Can anyone tell me, how I can build a Countdown in my Console-Programm. The Background is, that the Player has a certain time to answer a Input()-Question, after the time is up, he's lose. And it is possible to make the Countdown visible in seconds while at the same moment the player can answer?
			
			
									
									[Console]How to realise a countdown?
[Console]How to realise a countdown?
PB 4 | WinXP_SP2
						- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
OK, probably somewhat buggy and unpolished as yet, but this covers most of the bases:
			
			
									
									Code: Select all
Global startloc 
Global threadactive 
Procedure TimeOut(t) 
  time=ElapsedMilliseconds() 
  timestr.s = Str(t - (ElapsedMilliseconds()-time) / 1000) 
  ConsoleLocate(40,5) 
  Print(timestr) 
  ConsoleLocate(startloc,5) 
  Repeat 
    If threadactive 
      newtimestr.s = Str(t - (ElapsedMilliseconds()-time) / 1000) 
      If newtimestr <> timestr 
        timestr = newtimestr 
        ConsoleLocate(40,5) 
        Print(newtimestr+"  ") 
        ConsoleLocate(startloc,5) 
      EndIf 
      If (ElapsedMilliseconds()-time) / 1000 >= t 
        keybd_event_(#VK_RETURN,0,0,0) 
        keybd_event_(#VK_RETURN,0,#KEYEVENTF_KEYUP,0) 
        threadactive = #False 
      EndIf 
      Delay(1) 
    EndIf 
  Until threadactive = #False 
EndProcedure 
OpenConsole() 
EnableGraphicalConsole(1) 
ConsoleLocate(5,5) 
Print("Enter your name: ") 
threadactive = #True 
CreateThread(@Timeout(),10) 
startloc = 22 
Repeat 
  Delay(1)
  inputstr.s = Inkey() 
  If inputstr <> "" 
    If inputstr = Chr(8) 
      startloc - 1 
      ConsoleLocate(startloc,5) 
      Print(" ") 
      outstr.s = Left(outstr,Len(outstr)-1) 
    Else 
      ConsoleLocate(startloc,5) 
      If inputstr<>Chr(13) 
        outstr.s + inputstr 
        Print(inputstr) 
      Else 
        ConsoleLocate(40,5) 
        Print("  ") 
        ConsoleLocate(18,9) 
        threadactive = #False 
      EndIf 
      startloc + 1 
    EndIf 
  EndIf 
Until inputstr = Chr(13) 
ConsoleLocate(5, 7) 
If outstr = "" 
  PrintN("Timeout!") 
Else 
  PrintN("You entered: "+outstr) 
EndIf 
PrintN("") 
Print("<any key to quit>") 
Input()BERESHEIT
						- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 

