首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame.joystick返回0

Pygame.joystick返回0
EN

Stack Overflow用户
提问于 2016-11-30 14:35:40
回答 1查看 825关注 0票数 2

我正在尝试建立我自己的迷你库,以简化操纵杆在pygame中的使用。但是,当我试图读取按钮和轴时,它们都返回0,但只返回assign函数(我使用的是罗技攻击3)。

代码语言:javascript
复制
import sys

import pygame


joysticks = []

pygame.joystick.init()
pygame.init()


# Variables for locations in list #
AXIS_PITCH = 1
AXIS_ROLL = 0
AXIS_THROTTLE = 2
BUTTON_TRIGGER = BUTTON_1 = 3
BUTTON_2 = 4
BUTTON_3 = 5
BUTTON_4 = 6
BUTTON_5 = 7




# Must be called first, initializes joysticks #
def init():
    for joy in range(pygame.joystick.get_count()):
        joysticks.append(pygame.joystick.Joystick(joy))
        joysticks[joy].init()


# Needs player count, surface to draw to, font to use, colour of text, dimesions of screen #
def assign(players, screen, font, colour, dimensions):
#     unassigned = joysticks.copy()
    toReturn = []
    for i in range(players):
        assigned = False
        while(not assigned):
            if(pygame.key.get_pressed()[pygame.K_ESCAPE]): pygame.quit(); sys.exit();
            screen.fill([0,0,0])
            outString = "Player "+str(i+1)+" pull the trigger"
            out = font.render(outString, 1, colour, None)
            screen.blit(out, (dimensions[0]/2-out.get_width()/2, dimensions[1]/2-out.get_height()/2))
            pygame.display.flip()
            for stick in joysticks:
                print(stick.get_axis(1))
                if(stick.get_button(0) == 1):
                    print("Triggered")
                    if(stick not in toReturn):
                        toReturn.append(stick)
                        assigned = True
    return(toReturn)


# Returns the values of three axis, and 5 buttons on the passed joystick
def check(joystick):
    toReturn = []
    toReturn.append(joystick.get_axis(0))
    toReturn.append(joystick.get_axis(1))
    toReturn.append(joystick.get_axis(2))
    toReturn.append(joystick.get_button(0))
    toReturn.append(joystick.get_button(1))
    toReturn.append(joystick.get_button(2))
    toReturn.append(joystick.get_button(3))
    toReturn.append(joystick.get_button(4))
    return(toReturn)

如果你需要更多的代码,就问一问。提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-01 14:00:52

如果不处理事件,像get_pressed()get_button()get_axis()这样的函数可能无法工作--当您不使用pygame.event.get()或其他调用pygame.event.pump()从系统获取信息的事件函数时。

定期使用pygame.event.get() (或其他事件函数)(即。在您的while循环中)从系统获取当前信息。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40891060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档