Sep-23-2018, 01:43 AM
Joystick event example.
import pygame pygame.init() def main(): pygame.display.set_caption('JoyStick Example') surface = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 20) linesize = font.get_linesize() joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())] for joy in joysticks: joy.init() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.JOYAXISMOTION: print(event) elif event.type == pygame.JOYHATMOTION: print(event) elif event.type == pygame.JOYBUTTONDOWN: print(event) surface.fill((0,0,0)) pygame.display.flip() clock.tick(20) pygame.quit() main()joystick states example.
import pygame pygame.init() def main(): pygame.display.set_caption('JoyStick Example') surface = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 20) linesize = font.get_linesize() joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())] print(len(joysticks)) for joy in joysticks: joy.init() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False surface.fill((0,0,0)) # joystick buttons for j in range(len(joysticks)): for i in range(joysticks[j].get_numbuttons()): if joysticks[j].get_button(i): print("Joystick {}, Button {} pressed".format(j, i)) pygame.display.flip() clock.tick(20) pygame.quit() main()
99 percent of computer problems exists between chair and keyboard.