如何将 event.value 用于主循环之外的另一个函数?
How to use event.value for another function outside of the main loop?
下面是我的实际代码的工作框架。我想要做的是获取 JOYAXISMOTION
、event.value
、y
以便我可以在循环外的不同函数中使用它。我已经定义了变量 y
.
import time
import sys
import pygame
import subprocess
import random
from pygame.locals import *
pygame.init()
y = 0
def get_percent_change(current, previous):
if current == previous:
return 0
try:
return ((float(current) - float(previous)) /abs(previous)) * 100.0
except ZeroDivisionError:
return float('inf')
def sendCommands():
time.sleep(5)
print('Hello World')
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
game_state = False
run = True
while run:
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
print(event)
if game_state == False and event.button == 8:
game_state = True
if event.button == 4:
game_state = False
if event.type == JOYHATMOTION:
if event.value[0] == 1 or event.value[0] == -1:
game_state = False
if event.type == JOYAXISMOTION:
if event.axis == 4:
current = event.value
y = get_percent_change(current, -1.0001)
print(y)
if event.type == JOYDEVICEADDED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
if event.type == JOYDEVICEREMOVED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
if game_state == True:
sendCommands()
pygame.quit()
sys.exit()
我 运行 遇到的主要问题是 sendCommands
需要的 time.sleep(5)
。它在睡觉时阻止脚本。我尝试过 asyncio 但没有成功。线程在这里也让我很困惑。我还尝试了提供给一个密切相关的问题 here 的答案中的延迟逻辑,但这也阻止了来自 运行ning.
的代码
请允许我就如何进行思考:
- 我可以运行脚本而不被阻止
- 以及如何在循环外访问和使用
event.value
、y
要在循环外使用 y
,您可以将其声明为全局的,但更好的替代方法是创建一个函数来获取值。您可以实现一个计时器来每 5 秒执行一次子进程,而不是阻塞整个线程。以下代码每 5 秒为我打印一次 None,因为我没有连接任何控制器,但我认为它对你有用。
import time
import sys
import pygame
from pygame.locals import *
pygame.init()
def sendCommands(waitTime, y):
global sendCommandsStart
#count how many seconds have passed since sendCommandsStart
#variable was last updated
if time.time() - sendCommandsStart > waitTime:
sendCommandsStart = time.time() #reset the timer
print(y)#call your subpreocess here
def getJoyStickY(events):
for event in events:
if event.type == JOYAXISMOTION:
if event.axis == 4:
return get_percent_change(current, -1.0001)
return None
pygame.joystick.init()
run = True
sendCommandsStart = time.time() #get the current time - its just a number (time since last epoh)
while run:
allEvents = pygame.event.get()
sendCommands(5, getJoyStickY(allEvents))
pygame.quit()
sys.exit()
下面是我的实际代码的工作框架。我想要做的是获取 JOYAXISMOTION
、event.value
、y
以便我可以在循环外的不同函数中使用它。我已经定义了变量 y
.
import time
import sys
import pygame
import subprocess
import random
from pygame.locals import *
pygame.init()
y = 0
def get_percent_change(current, previous):
if current == previous:
return 0
try:
return ((float(current) - float(previous)) /abs(previous)) * 100.0
except ZeroDivisionError:
return float('inf')
def sendCommands():
time.sleep(5)
print('Hello World')
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
game_state = False
run = True
while run:
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
print(event)
if game_state == False and event.button == 8:
game_state = True
if event.button == 4:
game_state = False
if event.type == JOYHATMOTION:
if event.value[0] == 1 or event.value[0] == -1:
game_state = False
if event.type == JOYAXISMOTION:
if event.axis == 4:
current = event.value
y = get_percent_change(current, -1.0001)
print(y)
if event.type == JOYDEVICEADDED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
print(joystick.get_name())
if event.type == JOYDEVICEREMOVED:
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
if game_state == True:
sendCommands()
pygame.quit()
sys.exit()
我 运行 遇到的主要问题是 sendCommands
需要的 time.sleep(5)
。它在睡觉时阻止脚本。我尝试过 asyncio 但没有成功。线程在这里也让我很困惑。我还尝试了提供给一个密切相关的问题 here 的答案中的延迟逻辑,但这也阻止了来自 运行ning.
请允许我就如何进行思考:
- 我可以运行脚本而不被阻止
- 以及如何在循环外访问和使用
event.value
、y
要在循环外使用 y
,您可以将其声明为全局的,但更好的替代方法是创建一个函数来获取值。您可以实现一个计时器来每 5 秒执行一次子进程,而不是阻塞整个线程。以下代码每 5 秒为我打印一次 None,因为我没有连接任何控制器,但我认为它对你有用。
import time
import sys
import pygame
from pygame.locals import *
pygame.init()
def sendCommands(waitTime, y):
global sendCommandsStart
#count how many seconds have passed since sendCommandsStart
#variable was last updated
if time.time() - sendCommandsStart > waitTime:
sendCommandsStart = time.time() #reset the timer
print(y)#call your subpreocess here
def getJoyStickY(events):
for event in events:
if event.type == JOYAXISMOTION:
if event.axis == 4:
return get_percent_change(current, -1.0001)
return None
pygame.joystick.init()
run = True
sendCommandsStart = time.time() #get the current time - its just a number (time since last epoh)
while run:
allEvents = pygame.event.get()
sendCommands(5, getJoyStickY(allEvents))
pygame.quit()
sys.exit()