Updated. /JL
This commit is contained in:
49
main.py
Normal file
49
main.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import pygame
|
||||
import pygameController as PC
|
||||
|
||||
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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user