blob: f6564db0d6bfaad0977b7f0f733b92ffd6fc083e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import pygame
import os
from pygame import locals
import json
CFG_FILE = "config.json"
cfg = json.load(open(CFG_FILE))
pygame.init()
pygame.joystick.init()
try:
j = pygame.joystick.Joystick(0)
j.init()
print 'Enabled joystick: ' + j.get_name()
except pygame.error:
print 'no joystick found.'
while 1:
for e in pygame.event.get():
#print 'event : ' + str(e.type)
#print 'data : ' + str(e.dict)
if e.type == pygame.locals.JOYAXISMOTION:
x, y = j.get_axis(0), j.get_axis(1)
if (x > 0):
direction = "right"
elif(x < 0):
direction = "left"
if (y > 0):
direction = "up"
elif(y < 0):
direction = "down"
if (y == x == 0):
pass
else:
try:
os.system(cfg["direction"][direction])
except Exception as balls:
print "direction not defined?", balls
elif e.type == pygame.locals.JOYBUTTONDOWN:
try:
os.system(cfg["button"][str(e.button)])
except Exception as balls:
print "button not defined: ", balls
#elif e.type == pygame.locals.JOYBUTTONUP:
# print 'button up', e.joy, e.button
|