50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
import pygame
|
|
import pygameControls as PC
|
|
from pygameControls import globals
|
|
|
|
if __name__ == "__main__":
|
|
pygame.init()
|
|
|
|
done = False
|
|
|
|
joystick_count = pygame.joystick.get_count()
|
|
|
|
joysticks = {}
|
|
if joystick_count == 0:
|
|
print("No controllers found...Exiting!")
|
|
done = True
|
|
|
|
while not done:
|
|
# Event processing step.
|
|
# Possible joystick events: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
|
|
# JOYBUTTONUP, JOYHATMOTION, JOYDEVICEADDED, JOYDEVICEREMOVED
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
done = True # Flag that we are done so we exit this loop.
|
|
|
|
if event.type == pygame.JOYBUTTONDOWN:
|
|
print("Joystick button pressed.")
|
|
if event.button == 0:
|
|
joystick = joysticks[event.instance_id]
|
|
try:
|
|
joystick.rumble(0.7, 1.0, 1000)
|
|
except AttributeError:
|
|
print("Rumble is not supported on this joystick.")
|
|
except Exception as e:
|
|
print(f"Rumble call failed: {e}")
|
|
|
|
if event.type == pygame.JOYBUTTONUP:
|
|
print("Joystick button released.")
|
|
|
|
# Handle hotplugging
|
|
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()] = PC.controller.Controllers(joy)
|
|
|
|
if event.type == pygame.JOYDEVICEREMOVED:
|
|
del joysticks[event.instance_id]
|
|
print(f"Joystick {event.instance_id} disconnected")
|
|
|
|
|