yannbouteiller / vgamepad

Virtual XBox360 and DualShock4 gamepads in python
MIT License
162 stars 21 forks source link

I combined it with pygame to read the ps3 controller as input to drive the virtual gamepad but i got an error #8

Closed zarlicho closed 2 years ago

zarlicho commented 2 years ago

I use float data from ps3 controller with pygame as vgamepad input, but I have an error so I can't. does anyone want to try my code?

import pygame
import sys
from pygame.locals import *
from math import trunc
import vgamepad as vg
import time

gamepad = vg.VDS4Gamepad()
pygame.init()
joysticks = []
clock = pygame.time.Clock()
keepPlaying = True
motion = [0,0]
print("tes")
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(1) for i in range(pygame.joystick.get_count())]
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(joysticks[-1])
    # print a statement telling what the name of the controller is
    #print ("Detected joystick "),joysticks[0].get_name(),"'"
    #print(joysticks[-1].get_name())

while True:
    for event in pygame.event.get():
        if event.type == JOYAXISMOTION:
            print(event.axis)
            if event.axis < 2:

                motion[event.axis] = event.value
                xvalue =  motion[0]
                yvalue = motion[1]
                yc = float(yvalue)
                print("X",xvalue,"Y",yc)
                gamepad.left_joystick_float(x_value_float=xvalue, y_value_float=0.0) 
                gamepad.update()
zarlicho commented 2 years ago

https://pastebin.com/upNV3CCu

yannbouteiller commented 2 years ago

Hello, can you paste the traceback of the error you get, please?

(PS: if you are using left_joystick_float(), make sure you are passing values between -1.0 and +1.0)

zarlicho commented 2 years ago

I have resolved the error but a bug appears, the virtual gamepad is read by pygame so looping occurs

yannbouteiller commented 2 years ago

Haha I see. You need a way to find out which one is the virtual gamepad so as to ignore it in pygame I guess.

yannbouteiller commented 2 years ago

The get_guid() and get_name() methods in pygame might help you here. Or maybe simpler : try creating the Joystick object in pygame before creating the virtual gamepad.

zarlicho commented 2 years ago

ok i try