Untitled
unknown
plain_text
2 years ago
3.5 kB
7
Indexable
import pygame
import time
from datetime import datetime
def main():
pygame.init()
# Initialize the gamepad
pygame.joystick.init()
# Check if any joystick is connected
if pygame.joystick.get_count() == 0:
print("No joystick/gamepad found.")
return
# Initialize the first joystick
gamepad = pygame.joystick.Joystick(0)
gamepad.init()
print(f"Joystick name: {gamepad.get_name()}")
print(f"Number of axes: {gamepad.get_numaxes()}")
print(f"Number of buttons: {gamepad.get_numbuttons()}")
joysticks = {}
try:
vibrate = [False, False]
vibrate_other = [False, False]
while True:
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
gamepad = joysticks[event.instance_id]
other_gamepad = joysticks[1 - event.instance_id]
now = datetime.now()
print(now, "----", event)
if event.button == 0:
if vibrate[gamepad.get_instance_id()]:
gamepad.rumble(0, 0.7, 100)
if vibrate_other[gamepad.get_instance_id()]:
other_gamepad.rumble(0, 0.7, 100)
print(f"A")
elif event.button == 1:
if vibrate[gamepad.get_instance_id()]:
gamepad.rumble(0, 1, 100)
if vibrate_other[gamepad.get_instance_id()]:
other_gamepad.rumble(0, 1, 100)
print("B")
elif event.button == 2:
if vibrate[gamepad.get_instance_id()]:
gamepad.rumble(1, 1, 10)
if vibrate_other[gamepad.get_instance_id()]:
other_gamepad.rumble(1, 1, 10)
print("X")
elif event.button == 3:
if vibrate[gamepad.get_instance_id()]:
gamepad.rumble(0, 0.1, 100)
if vibrate_other[gamepad.get_instance_id()]:
other_gamepad.rumble(0, 0.1, 100)
print("Y")
elif event.button == 10:
print(gamepad.get_instance_id())
vibrate[gamepad.get_instance_id()] = not vibrate[gamepad.get_instance_id()]
print("R1 ", vibrate[gamepad.get_instance_id()])
elif event.button == 9:
vibrate_other[gamepad.get_instance_id()] = not vibrate_other[gamepad.get_instance_id()]
print("L1 ", vibrate_other[gamepad.get_instance_id()])
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
if event.type == pygame.JOYDEVICEREMOVED:
print(f"Joystick {event.device_index} disconnected")
joy = pygame.joystick.Joystick(event.device_index)
except KeyboardInterrupt:
pygame.quit()
if __name__ == "__main__":
main()Editor is loading...
Leave a Comment