1

我的想法是通过我的老式六轴控制器玩堡垒之夜,之后我计划制作指定的 Arduino 控制器(带传感器)来控制游戏。我使用 pygame.joystick 从控制器获取数据,使用 pynput 来控制鼠标/键盘。光标在 Windows 中完美移动,但在游戏中我可以旋转但没有操纵杆(没有光标,我控制的)。一切正常,但代码处于半完成状态:

import pygame, pynput, time
#initialize joysticks, mouse and controller outputs
pygame.init()
pygame.joystick.init()
mouse = pynput.mouse.Controller()
keyboard = pynput.keyboard.Controller()

#joysticks = [pygame.joystick.Joystick(x) for x in 

range(pygame.joystick.get_count())]
#declare Kon (joystick object) and initialize it
Kon = pygame.joystick.Joystick(0)
Kon.init()

#map the controller
specs = {'tengelyek' : Kon.get_numaxes(), 'gombok' : Kon.get_numbuttons(), 'Iranypadok' : Kon.get_numhats() }

Gombok = {0 : 'X' , 1 : "A", 2 : 'B', 3 : 'Y', 4 : 'balA', 5 : 'jobbA', 6 : 'balB', 7 : 'jobbB', 8 : 'SELECT', 9 : 'START', 10 : 'Joy1', 11: 'Joy2'}
Gomboki = {'X' : 0 ,"A" : 1,'B' : 2,'Y' : 3,'balA' : 4,'jobbA' : 5,'balB' : 6,'jobbB' : 7,'SELECT' : 8,'START' : 9,'Joy1' : 10,'Joy2' : 11}
Axisok = {0 : 'Joy1 X', 1 : 'Joy1 Y', 2 : 'Joy2 X', 3 : 'Joy2 Y'}
Axisoki = {'Joy1 X' : 0 ,'Joy1 Y' : 1 ,'Joy2 X' : 2 ,'Joy2 Y' : 3}

Mode = False

Alkvalto = False
Epit = False

def Gombcheck(Gomb):
    return Kon.get_button(Gomboki[Gomb])

def Axischeck(Axis):
    temp = Kon.get_axis(Axisoki[Axis])
    if temp < -1 or temp > 1 or ( temp < 0.2 and temp > -0.2):
        return 0
    elif temp > 0:
        vissza = 1
        for seb in (0.9 , 0.8, 0.7, 0.6,0.5,0.4,0.3):
            if temp < seb:
                temp = seb
        return vissza
    else:
        vissza = -1
        for seb in (-0.9 , -0.8, -0.7, -0.6, -0.5, -0.4, -0.3):
            if temp > seb:
                temp = seb
        return vissza


#loop (planned to switch between modes) bind joy's buttons to actions
while Mode == False:
    pygame.event.get()

    if Gombcheck('balA') == 1:
        if Alkvalto == False:
            keyboard.press(pynput.keyboard.Key.alt)
            keyboard.press(pynput.keyboard.Key.tab)
            keyboard.release(pynput.keyboard.Key.tab)
            Alkvalto = True
            time.sleep(0.2)    
        else:
            keyboard.release(pynput.keyboard.Key.alt)
            Alkvalto = False
            time.sleep(0.2)
    tempor = Axischeck('Joy2 X')
    if tempor > 0:
        keyboard.press(pynput.keyboard.Key.right)
    elif tempor < 0:
        keyboard.press(pynput.keyboard.Key.left)
    elif tempor == 0:
        keyboard.release(pynput.keyboard.Key.right)
        keyboard.release(pynput.keyboard.Key.left)
    tempor = Axischeck('Joy2 Y')
    if tempor > 0:
        keyboard.press(pynput.keyboard.Key.down)
    elif tempor < 0:
        keyboard.press(pynput.keyboard.Key.up)
    elif tempor == 0:
        keyboard.release(pynput.keyboard.Key.down)
        keyboard.release(pynput.keyboard.Key.up)
    if Gombcheck('jobbA') == 1:
        mouse.press(pynput.mouse.Button.left)
    mouse.move(Axischeck('Joy1 X')*60,Axischeck('Joy1 Y')*30) #moves the mouse, works in windows but the game not
    if Gombcheck('A') == 1:
        keyboard.press('E')
    if Gombcheck('A') == 0:
        keyboard.release('E')
    if Gombcheck('B') == 1:
        keyboard.press(pynput.keyboard.Key.space)
        keyboard.release(pynput.keyboard.Key.space)
    if Gombcheck('Y') == 1:
        keyboard.press(pynput.keyboard.Key.ctrl_l)
        keyboard.release(pynput.keyboard.Key.ctrl_l)
    if Gombcheck('jobbB') == 1:
        keyboard.press(pynput.keyboard.Key.shift_l)
    if Gombcheck('jobbB') == 0:
        keyboard.release(pynput.keyboard.Key.shift_l)
    if Gombcheck('jobbA') == 0:
        mouse.release(pynput.mouse.Button.left)
    if Gombcheck('X') == 1:
        keyboard.press('Q')
        keyboard.release('Q')
        Epit = not Epit
    if Epit == False:
        if Kon.get_hat == (0,1):
            keyboard.press(2)
            keyboard.release(2)
        if Kon.get_hat == (1,0):
            keyboard.press(3)
            keyboard.release(3)
        if Kon.get_hat == (0,-1):
            keyboard.press(4)
            keyboard.release(4)
        if Kon.get_hat == (-1,0):
            keyboard.press(5)
            keyboard.release(5)
    if Epit == True:
        if Kon.get_hat == (0,1):
            keyboard.press(H)
            keyboard.release(H)
        if Kon.get_hat == (1,0):
            keyboard.press(J)
            keyboard.release(J)
        if Kon.get_hat == (0,-1):
            keyboard.press(K)
            keyboard.release(K)
        if Kon.get_hat == (-1,0):
            keyboard.press(L)
            keyboard.release(L)
    time.sleep(0.1)

我认为问题不在于代码!可以控制比赛吗?

4

0 回答 0