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

查看:58
本文介绍了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 并没有给 pad 足够的时间来重新初始化 - 至少在我的电脑上.我发现如果你将调用限制在每秒,它会起作用.

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 秒或什么的),最好只运行此代码.这样你就不会在用户实际使用设备时退出

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天全站免登陆