Creating AHK game. Lag in the GuiControl, Move

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Creating AHK game. Lag in the GuiControl, Move

24 Feb 2018, 12:13

Hi,
I'm creating simple PingPong game with AHK. Everything is OK exepts that when I try to move my Stick, it reacts to the first Left or Right keybord click, then it sleeps for a second, and after that second, it starts to run multiple times like a smooth movement. I want to cut that "sleep" after the first click. How can I do that?

a part of my code:

Code: Select all

Hotkey, IfWinActive, PingPong
Hotkey, Right, PingPong_Right 
Hotkey, Left, PingPong_Left
return

PingPong_Right:
GuiControlGet, User_Ping, ping:Pos
if (User_PingX <= 400)
	GuiControl, ping: Move, User_Ping, % "x" User_PingX+15
return
Last edited by euras on 24 Feb 2018, 16:21, edited 1 time in total.
gregster
Posts: 9067
Joined: 30 Sep 2013, 06:48

Re: Lag in the GuiControl, Move

24 Feb 2018, 13:23

I don't feel like that snippet will help us identify the problem. Better would be a piece of code that would let us reproduce the problem.
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Lag in the GuiControl, Move

24 Feb 2018, 13:26

Pasting the whole code

Code: Select all

#SingleInstance, force

gui, ping: new
gui, ping: Default
gui, color, Black
gui, font, cWhite s12 bold
Gui, Add, Button, ReadOnly x5 y5 h10 w100 0x10 vPC_Ping
Gui, Add, Button, ReadOnly x5 y485 h10 w100 0x10 vUser_Ping
Gui, Add, Button, ReadOnly x240 y240 h10 w10 0x10 vPong
GuiControl, ping: Hide, Pong

gui, font, cWhite s100 bold
gui, add, text, x200 y170 vT1, 3
gui, add, text, x200 y170 vT2, 2
gui, add, text, x200 y170 vT3, 1
gui, add, text, x130 y170 vT4, GO!

Loop, 4
	GuiControl, ping: Hide, T%A_Index%

gui, font, cBlack s8, Verdana
Gui, Add, StatusBar,
gui, show, w500 h520, PingPong

Hotkey, IfWinActive, PingPong
Hotkey, Right, PingPong_Right 
Hotkey, Left, PingPong_Left

Start_Game()
return

PingPong_Right:
GuiControlGet, User_Ping, ping:Pos
if (User_PingX <= 400)
	GuiControl, ping: Move, User_Ping, % "x" User_PingX+15
;~ MsgBox The X coordinate is %User_PingX%. The Y coordinate is %User_PingY%. The width is %User_PingW%. The height is %User_PingH%.
return

PingPong_Left:
GuiControlGet, User_Ping, ping:Pos
if (User_PingX >= 0)
	GuiControl, ping: Move, User_Ping, % "x" User_PingX-15
;~ MsgBox The X coordinate is %User_PingX%. The Y coordinate is %User_PingY%. The width is %User_PingW%. The height is %User_PingH%.
return

Pong_Moves:
if (First_Game = true)
{
	Loop
	{
		GuiControlGet, Pong, ping:Pos
		GuiControlGet, User_Ping, ping:Pos
		GuiControlGet, PC_Ping, ping:Pos
		
		User_Full_Ping_Start := User_PingX ; user ping width coordinates
		User_Full_Ping_End := User_PingX + 100
		User_Ping_Middle := User_PingX + 50
		
		if (PongY = 475 and (PongX >= User_Full_Ping_Start and PongX <= User_Full_Ping_End)){
			Side_to_Side_User()
			gosub, Pong_Moves_UP
			return
		}
		else if (PongY < 490){
			GuiControl, ping: Move, Pong, % "y" PongY+5
			sleep 25
		}
		if (PongY > 485){
			Your_Score += 0
			PC_Score += 10
			if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			SB_SetText("You have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
			sleep 2000
			Start_Game()
			return
		}
		
	}
}

if (First_Game = false)
{
	Loop
	{
		GuiControlGet, Pong, ping:Pos
		GuiControlGet, User_Ping, ping:Pos
		GuiControlGet, PC_Ping, ping:Pos
		
		User_Full_Ping_Start := User_PingX ; user ping width coordinates
		User_Full_Ping_End := User_PingX + 100
		User_Ping_Middle := User_PingX + 50
		
		if (PongY = 475 and (PongX >= User_Full_Ping_Start and PongX <= User_Full_Ping_End)){
			Side_to_Side_User()
			gosub, Pong_Moves_UP
			return
		}
		else if (PongY < 490){
			If (rotation = "Left" and PongX > 0){
				GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Left" and PongX <= 0){
				rotation := "Right"
				GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Right" and PongX < 490){
				GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Right" and PongX >= 490){
				rotation := "Left"
				GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY+5
				sleep 20
			}

		}
		if (PongY > 485){
			Your_Score += 0
			PC_Score += 10
			if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			SB_SetText("You have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
			sleep 2000
			Start_Game()
			return
		}
	}
}

;~ MsgBox The X coordinate is %PongX%. The Y coordinate is %PongY%. The width is %PongW%. The height is %PongH%.
;~ MsgBox The X coordinate is %User_PingX%. The Y coordinate is %User_PingY%. The width is %User_PingW%. The height is %User_PingH%.
;~ MsgBox The X coordinate is %PC_PingX%. The Y coordinate is %PC_PingY%. The width is %PC_PingW%. The height is %PC_PingH%.
return

Pong_Moves_UP:
First_Game := false
Loop
{
	GuiControlGet, Pong, ping:Pos
	GuiControlGet, User_Ping, ping:Pos
	GuiControlGet, PC_Ping, ping:Pos
	
	PC_Ping_Start := PC_PingX ; user ping width coordinates
	PC_Ping_Middle := PC_PingX + 50
	PC_Ping_End := PC_PingX + 100
	Pong_End := PongX + 10
	
	if (PongY = 15 and (PongX >= PC_Ping_Start and PongX <= PC_Ping_End)){
		Side_to_Side_PC()
		gosub, Pong_Moves
		return
	}
	
	if (PC_Ping_Middle < PongX){
		GuiControl, ping: Move, PC_Ping, % "x" PC_PingX+4
	}
	if (PC_Ping_Middle > Pong_End){
		GuiControl, ping: Move, PC_Ping, % "x" PC_PingX-4
	}
	
	if (PongY <= 485){
		If (rotation = "Left" and PongX > 0){
			GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Left" and PongX <= 0){
			rotation := "Right"
			GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Right" and PongX < 490){
			GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Right" and PongX >= 490){
			rotation := "Left"
			GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY-5
			sleep 20
		}
	}

	if (PongY < 10){
		Your_Score += 10
		PC_Score += 0
		if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
		if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
		SB_SetText("PC have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
		sleep 2000
		Start_Game()
		return
	}
}
return

Start_Game()
{
	global
	SB_SetText("")
	Loop, 4
	{
		sleep 100
		GuiControl, ping: Show, T%A_Index%
		sleep 900
		GuiControl, ping: Hide, T%A_Index%
	}
	GuiControl, ping: Move, Pong, % "x" 240 "y" 240
	GuiControl, ping: Show, Pong
	sleep 400
	First_Game := true
	gosub, Pong_Moves
}

Side_to_Side_User()
{
	global
	if (User_Ping_Middle > PongX)
		rotation := "Left"
	if (User_Ping_Middle < PongX)
		rotation := "Right"
	return, rotation
}

Side_to_Side_PC()
{
	global
	if (PC_Ping_Middle > PongX)
		rotation := "Left"
	if (PC_Ping_Middle < PongX)
		rotation := "Right"
	return, rotation
}

Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Lag in the GuiControl, Move

24 Feb 2018, 13:39

One possible solution would be to make more hotkeys. Example: Right arrow starts moving, and Right Up stops moving.
gregster
Posts: 9067
Joined: 30 Sep 2013, 06:48

Re: Lag in the GuiControl, Move

24 Feb 2018, 13:53

Oh, nice game! I think I see what you mean. Could it be that the loops are the problem? Perhaps using SetTimer for the ball movement would make the hotkeys more responsive. But I will have to dig a bit deeper into the code...
I think we have a few forum members who already programmed little games with AHK - I hope they can give some general (or specific) advice.
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Lag in the GuiControl, Move

24 Feb 2018, 16:13

I found one solution how to make the stick move smooth, but the problem now that the rest of the game freezes until I hold up the left or right key...
Solution:

Code: Select all

PingPong_Right:
Loop
{
	GuiControlGet, User_Ping, ping:Pos
	GetKeyState, Right_Status, Right, P
	if Right_Status = U
		break
	if (User_PingX <= 400){
		GuiControl, ping: Move, User_Ping, % "x" User_PingX+10
		sleep 25
	}
}
return
the game:

Code: Select all

#SingleInstance, force

gui, ping: new
gui, ping: Default
gui, color, Black
gui, font, cWhite s12 bold
Gui, Add, Button, ReadOnly x5 y5 h10 w100 0x10 vPC_Ping
Gui, Add, Button, ReadOnly x5 y485 h10 w100 0x10 vUser_Ping
Gui, Add, Button, ReadOnly x240 y240 h10 w10 0x10 vPong
GuiControl, ping: Hide, Pong

gui, font, cWhite s100 bold
gui, add, text, x200 y170 vT1, 3
gui, add, text, x200 y170 vT2, 2
gui, add, text, x200 y170 vT3, 1
gui, add, text, x130 y170 vT4, GO!

Loop, 4
	GuiControl, ping: Hide, T%A_Index%

gui, font, cBlack s8, Verdana
Gui, Add, StatusBar,
gui, show, w500 h520, PingPong

Hotkey, IfWinActive, PingPong
Hotkey, Right, PingPong_Right 
Hotkey, Left, PingPong_Left

Start_Game()
return

PingPong_Right:
Loop
{
	GuiControlGet, User_Ping, ping:Pos
	GetKeyState, Right_Status, Right, P
	if Right_Status = U
		break
	if (User_PingX <= 400){
		GuiControl, ping: Move, User_Ping, % "x" User_PingX+10
		sleep 25
	}
}
return

PingPong_Left:
Loop
{
	GuiControlGet, User_Ping, ping:Pos
	GetKeyState, Left_Status, Left, P
	if Left_Status = U
		break
	if (User_PingX >= 0){
		GuiControl, ping: Move, User_Ping, % "x" User_PingX-10
		sleep 25
	}
}
return

Pong_Moves:
if (First_Game = true)
{
	Loop
	{
		GuiControlGet, Pong, ping:Pos
		GuiControlGet, User_Ping, ping:Pos
		GuiControlGet, PC_Ping, ping:Pos
		
		User_Full_Ping_Start := User_PingX ; user ping width coordinates
		User_Full_Ping_End := User_PingX + 100
		User_Ping_Middle := User_PingX + 50
		
		if (PongY = 475 and (PongX >= User_Full_Ping_Start and PongX <= User_Full_Ping_End)){
			Side_to_Side_User()
			gosub, Pong_Moves_UP
			return
		}
		else if (PongY < 490){
			GuiControl, ping: Move, Pong, % "y" PongY+5
			sleep 25
		}
		if (PongY > 485){
			Your_Score += 0
			PC_Score += 10
			if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			SB_SetText("You have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
			sleep 2000
			Start_Game()
			return
		}
		
	}
}

if (First_Game = false)
{
	Loop
	{
		GuiControlGet, Pong, ping:Pos
		GuiControlGet, User_Ping, ping:Pos
		GuiControlGet, PC_Ping, ping:Pos
		
		User_Full_Ping_Start := User_PingX ; user ping width coordinates
		User_Full_Ping_End := User_PingX + 100
		User_Ping_Middle := User_PingX + 50
		
		if (PongY = 475 and (PongX >= User_Full_Ping_Start and PongX <= User_Full_Ping_End)){
			Side_to_Side_User()
			gosub, Pong_Moves_UP
			return
		}
		else if (PongY < 490){
			If (rotation = "Left" and PongX > 0){
				GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Left" and PongX <= 0){
				rotation := "Right"
				GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Right" and PongX < 490){
				GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY+5
				sleep 20
			}
			If (rotation = "Right" and PongX >= 490){
				rotation := "Left"
				GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY+5
				sleep 20
			}

		}
		if (PongY > 485){
			Your_Score += 0
			PC_Score += 10
			if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
			SB_SetText("You have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
			sleep 2000
			Start_Game()
			return
		}
	}
}
return

Pong_Moves_UP:
First_Game := false
Loop
{
	GuiControlGet, Pong, ping:Pos
	GuiControlGet, User_Ping, ping:Pos
	GuiControlGet, PC_Ping, ping:Pos
	
	PC_Ping_Start := PC_PingX ; user ping width coordinates
	PC_Ping_Middle := PC_PingX + 50
	PC_Ping_End := PC_PingX + 100
	Pong_End := PongX + 10
	
	if (PongY = 15 and (PongX >= PC_Ping_Start and PongX <= PC_Ping_End)){
		Side_to_Side_PC()
		gosub, Pong_Moves
		return
	}
	
	if (PC_Ping_Middle < PongX){
		GuiControl, ping: Move, PC_Ping, % "x" PC_PingX+4
	}
	if (PC_Ping_Middle > Pong_End){
		GuiControl, ping: Move, PC_Ping, % "x" PC_PingX-4
	}
	
	if (PongY <= 485){
		If (rotation = "Left" and PongX > 0){
			GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Left" and PongX <= 0){
			rotation := "Right"
			GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Right" and PongX < 490){
			GuiControl, ping: Move, Pong, % "x" PongX+Rand(4, 6) "y" PongY-5
			sleep 20
		}
		If (rotation = "Right" and PongX >= 490){
			rotation := "Left"
			GuiControl, ping: Move, Pong, % "x" PongX-Rand(4, 6) "y" PongY-5
			sleep 20
		}
	}

	if (PongY < 10){
		Your_Score += 10
		PC_Score += 0
		if (Your_Score = 100){
				MsgBox, You have won! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
		if (PC_Score = 100){
				MsgBox, You have lost! Your score is %Your_Score% vs PC score %PC_Score%
				return
			}
		SB_SetText("PC have lost! Your score " . Your_Score . " vs PC score " . PC_Score)
		sleep 2000
		Start_Game()
		return
	}
}
return

Start_Game()
{
	global
	SB_SetText("")
	Loop, 4
	{
		sleep 100
		GuiControl, ping: Show, T%A_Index%
		sleep 900
		GuiControl, ping: Hide, T%A_Index%
	}
	GuiControl, ping: Move, Pong, % "x" 240 "y" 240
	GuiControl, ping: Show, Pong
	sleep 400
	First_Game := true
	gosub, Pong_Moves
}

Side_to_Side_User()
{
	global
	if (User_Ping_Middle > PongX)
		rotation := "Left"
	if (User_Ping_Middle < PongX)
		rotation := "Right"
	return, rotation
}

Side_to_Side_PC()
{
	global
	if (PC_Ping_Middle > PongX)
		rotation := "Left"
	if (PC_Ping_Middle < PongX)
		rotation := "Right"
	return, rotation
}

Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Creating AHK game. Lag in the GuiControl, Move

25 Feb 2018, 12:47

why it freezes the rest of the code if I try to put it into loop? because other movements the game is doing at the same time is also in a loop? If yes, how to run 2 loops at the same time?
gregster
Posts: 9067
Joined: 30 Sep 2013, 06:48

Re: Creating AHK game. Lag in the GuiControl, Move

25 Feb 2018, 13:26

My guess is that SetTimer could help.
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Creating AHK game. Lag in the GuiControl, Move

26 Feb 2018, 11:08

I tried to get a keyboard delay via DLLCall.
Sadly, but it looks like the shortest period is 250 ms, which is still pretty high amount to have a smooth motion without a loop.

Sharing DLLCalls

Code: Select all

DllCall("SystemParametersInfo", UInt, 0xB, UInt, 31, UIntP, 0, UInt, 0) ; 0xB is SPI_SETKEYBOARDSPEED. 31 is the max speed, 0 is the min.
DllCall("SystemParametersInfo", UInt, 0x17, UInt, 0, UIntP, 0, UInt, 0) ; 0x17 is SPI_SETKEYBOARDDELAY. the first 0 is the shortest delay 250ms, 3 is the highest 1 sec
if someone have a though how I can reduce that 250 ms, I would be glad to hear it :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: lechat and 132 guests