pygame:检测游戏杆断开连接,然后等待它重新连接 [英] pygame: detect Joystick disconnect, and wait for it to be reconnected

查看:281
本文介绍了pygame:检测游戏杆断开连接,然后等待它重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pygame.joystick.Joystick对象,希望能够打印一条消息,要求用户拔下USB游戏杆后重新连接.

I'm using a pygame.joystick.Joystick object and want to be able to print a message asking the user to reconnect a usb joystick once it's been unplugged.

现在我大概有:

js = pygame.joystick.Joystick(0)
#... some game code and stuff
pygame.joystick.quit()
pygame.joystick.init()
while pygame.joystick.get_count() == 0:
    print 'please reconnect joystick'
    pygame.joystick.quit()
    pygame.joystick.init()

js = pygame.joystick.Joystick(0)
js.init()

但是它不能正确地重新连接,请确认它到底在做什么,但这绝对是错误的.任何指示都将有所帮助

but it doesn't reconnect properly, idk what exactly it's doing, but it's definitely wrong. Any direction on this would be helpful

推荐答案

我设法按照Noelkd的建议进行工作,但是我遇到了Ryan Haining描述的类似问题

I managed to get mine working with Noelkd's suggestion, but I had a similar issue described by Ryan Haining

起初我有类似的东西,但是它不起作用,因为它在所有退出和初始化过程中都无法跟踪游戏手柄的动作.最初,该方法可用于检查是否已完全插入控制器,但无法在运行时有效地进行检查

I had something like this at first, but it doesn't work because the it loses track of the gamepads actions with all the quiting and initing. This works initially to check if a controller is plugged in at all, but not to effectively check while running

我也遇到了这个问题.我认为您是对的,经常调用quit并不能使键盘有足够的时间重新初始化-至少在我的计算机上.我发现,如果您将通话限制为每秒一次,那么它将起作用.

I had this issue too. I think you are correct, calling quit too often doesn't give the pad enough time to re-initialise - at least on my computer. I found that if you limit the calls to every second, it works.

这可能会导致播放器输入暂时断开,因此joystick上的任何呼叫都将无法正常工作.

It can cause the player input to temporarily disconnect though, so any calls on a joystick won't work.

最好仅在检测到一段时间没有输入(例如5秒钟左右)的情况下运行此代码.这样,您就不会在用户实际使用设备时quit

It's better to only run this code if you detect that there has been no input for a while (say 5 seconds or something). This way you won't quit while a user is actually using the device

import pygame
import time

INACTIVITY_RECONNECT_TIME = 5
RECONNECT_TIMEOUT = 1

class ControllerInput():
  def __init__(self):
    pygame.joystick.init()
    self.lastTime = 0
    self.lastActive = 0

  def getButtons(self, joystickId):
    joystick = pygame.joystick.Joystick(joystickId)
    joystick.init()

    buttons = {}

    for i in range(joystick.get_numbuttons()):
      buttons[i] = joystick.get_button(i)
      if buttons[i]:
        self.lastActive = time.time()

    return buttons

  def hasController(self):
    now = time.time()
    if now - self.lastActive > INACTIVITY_RECONNECT_TIME and now - self.lastTime > RECONNECT_TIMEOUT:
      self.lastTime = now
      pygame.joystick.quit()
      pygame.joystick.init()

    return pygame.joystick.get_count() > 0

用法

# ... some constructor
controller = ControllerInput()

# ... game loop
if not controller.hasController():
  # handle disconnect
  print('reconnect')
  return

buttons = controller.getButtons(0)

if buttons[0]:
  # buttons[0] was pressed!

这篇关于pygame:检测游戏杆断开连接,然后等待它重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆