Skip to content

Instantly share code, notes, and snippets.

@ohsqueezy
Last active December 15, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohsqueezy/5231134 to your computer and use it in GitHub Desktop.
Save ohsqueezy/5231134 to your computer and use it in GitHub Desktop.
print message when gamepad axis is pressed and repeat after delay
# Print message when gamepad axis is pressed and repeat message after delay
#
# Run with the following command:
# python pygame-axis-motion-delay.py
import pygame
from pygame.locals import *
def main():
pygame.init()
pygame.display.set_mode((480, 360))
gamepad = pygame.joystick.Joystick(0)
gamepad.init()
delay = 1000
neutral = True
pressed = 0
last_update = pygame.time.get_ticks()
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
move = False
if gamepad.get_axis(1) == 0:
neutral = True
pressed = 0
else:
if neutral:
move = True
neutral = False
else:
pressed += pygame.time.get_ticks() - last_update
if pressed > delay:
move = True
pressed -= delay
if move:
print "move"
last_update = pygame.time.get_ticks()
if __name__ == "__main__":
main()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment