如何从Python中的Xbox One控制器获取输入?

2024-10-02 02:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想给我用Python制作的游戏添加控制器支持,但是我找不到任何用于Python的xboxone控制器输入模块。我需要特别的Xbox One控制器支持。谢谢!在


Tags: 模块游戏控制器onexboxxboxone
1条回答
网友
1楼 · 发布于 2024-10-02 02:25:27

使用pygame,因为它可以读取操纵杆,对于pygame使用哪种类型的控制器并不重要。例如,当我们按下'A'按钮时,我们希望python打印“A Has Been Pressed”。在

import pygame
pygame.init()
joysticks = []
clock = pygame.time.Clock()
keepPlaying = True

# for al the connected joysticks
for i in range(0, pygame.joystick.get_count()):
    # create an Joystick object in our list
    joysticks.append(pygame.joystick.Joystick(i))
    # initialize them all (-1 means loop forever)
    joysticks[-1].init()
    # print a statement telling what the name of the controller is
    print ("Detected joystick "),joysticks[-1].get_name(),"'"
while keepPlaying:
    clock.tick(60)
    for event in pygame.event.get():
        #The zero button is the 'a' button, 1 the 'b' button, 3 the 'y' 
        #button, 2 the 'x' button
        if event.button == 0:
            print ("A Has Been Pressed")

相关问题 更多 >

    热门问题