在Pygame中启动控制器会导致脚本停止

发布于 2025-02-04 08:16:49 字数 399 浏览 2 评论 0原文

我尝试了一些与控制器一起使用的pygame的示例,它们似乎都遇到了同样的问题,我无法弄清楚我是否尚未正确安装Pygame或我是否在做其他错误。

import pygame

pygame.joystick.init()
for index in range(pygame.joystick.get_count()):
    joystick = pygame.joystick.Joystick(index)
    print(joystick) # <- does actually return a controller


while True:
    pass # <- runs a few times and then stops

I've tried a few examples of pygame being used with controllers and they all seem to encounter the same issue, I can't figure out if I haven't installed pygame correctly or if im doing something else wrong.

import pygame

pygame.joystick.init()
for index in range(pygame.joystick.get_count()):
    joystick = pygame.joystick.Joystick(index)
    print(joystick) # <- does actually return a controller


while True:
    pass # <- runs a few times and then stops

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2025-02-11 08:16:49

参见 pygame窗口不响应。 >。您必须处理应用程序循环中的事件。参见 分别分别 ()

对于游戏的每个帧,您需要对活动队列进行某种呼叫。这样可以确保您的程序可以内部与操作系统的其余部分进行交互。

例如:

while True:
    pygame.event.pump()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

See PyGame window not responding after a few seconds. You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

e.g.:

while True:
    pygame.event.pump()

or

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
給妳壹絲溫柔 2025-02-11 08:16:49

遇到同样的问题。
我的操纵杆是Havit Twin USB控制器。
每次我启动Joystick.joystick(DeviceID)时,循环立即关闭。
我记得我在PC中安装了一个Twin USB驱动程序,该驱动器配置了控制器,轴速度,振动。
卸载Twinusb驱动程序/程序可以解决我的问题。
现在,我可以和我的操纵杆一起测试Pygame。

Encounter the same problem.
My joystick is Havit Twin USB Controller.
Everytime I init the joystick.Joystick(deviceID), the loop closes immediately.
I remembered i installed a Twin USB driver in my PC which configs the controller, axis speed, vibration.
Uninstalling the TwinUSB Driver/Program solves my problem.
Now I can test pygame along with my joystick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文