Creating toggling keyboard pause button

Blitz3D Forums/Blitz3D Programming/Creating toggling keyboard pause button

Shifty Geezer(Posted 2005) [#1]
I want to implement a keyboard toggle, press Space to pause animation, press Space again to continue. Reading the keyboard with keydown(), it's often the case the Space is down for longer than one iteration of the game loop, causing a pause and unpause when pressed and released. I've added a dealy (wait 150 ms after pressing key) along with flushing the keyboard, but it's far from ideal.

Any ideas on a nicer way to do this?

EDIT : I could always try KeyHit() instead! Doh!


scribbla(Posted 2005) [#2]
is this of any use
;
While Not KeyDown ( 1 )    
    Cls
    k=GetKey()

     If k=32 ; spacebar 
 	   Text 150,100, "paused"
       Repeat : k=GetKey() :  Until k=32
     Else
       Text 150,100, "game on"
     EndIf	

 VWait

Wend
End



Nicstt(Posted 2005) [#3]
Use KeyHit(1)


lo-tekk(Posted 2005) [#4]
While Not KeyHit(1)
If KeyHit ( 57 )
	Repeat
		Cls
		Text 10,10, " PAUSE"
		Flip 1
	Until KeyHit ( 57 )
EndIf
Cls
Text 10,10, "NO PAUSE"
Flip 1
Wend


------------------------
www.moonworx.de


Matty(Posted 2005) [#5]
paused=0
repeat

if keyhit(57) then paused=1-paused

if paused=0 then
;do whatever you do when the game is not paused
else
;do whatever you do when the game is paused

endif 

until keydown(1)





Shifty Geezer(Posted 2005) [#6]
Yeah, KeyHit's the way to go. If the user holds it down it still returns 0, only returning a change in state.

Thanks all.


Nicstt(Posted 2005) [#7]
what i used in a program i wrote

	If KeyHit(25) ; pause P
		CopyRect 0, 0, screenWidth, screenHeight, 0, 0, FrontBuffer(), BackBuffer()
		SetFont fntTimes70B : Color 0, 0, 0
		Text (screenWidth - 4) / 2, 200, "P A U S E D", True, False
		Color 255, 255, 255 : Text (screenWidth) / 2, 196, "P A U S E D", True, False
		Flip
		While Not KeyHit(25)
		Wend
		FlushKeys
		SetFont fntTimes20B
	EndIf



Neochrome(Posted 2005) [#8]
If KeyHit(25) ; pause P
FLUSHKEYS; <---------- it wont store the keyhit if your still holding the space bar down
CopyRect 0, 0, screenWidth, screenHeight, 0, 0, FrontBuffer(), BackBuffer()
SetFont fntTimes70B : Color 0, 0, 0
Text (screenWidth - 4) / 2, 200, "P A U S E D", True, False
Color 255, 255, 255 : Text (screenWidth) / 2, 196, "P A U S E D", True, False
Flip
While Not KeyHit(25)
Wend
FlushKeys
SetFont fntTimes20B
EndIf