-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbox.py
107 lines (80 loc) · 2.66 KB
/
xbox.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import pygame
import sys
import time
import socket
import cPickle as pickle
UDP_IP = "192.168.43.207"
UDP_PORT = 8080
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
# MESSAGE = pickle.dumps(["1","2"])
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
x='2'
y='3'
class TextPrint(object):
def __init__(self):
self.reset()
self.x_pos = 10
self.y_pos = 10
self.font = pygame.font.Font(None, 20)
def print1(self, my_screen, text_string):
text_bitmap = self.font.render(text_string, True, BLACK)
my_screen.blit(text_bitmap, [self.x_pos, self.y_pos])
self.y_pos += self.line_height
def reset(self):
self.x_pos = 10
self.y_pos = 10
self.line_height = 15
def indent(self):
self.x_pos += 10
def unindent(self):
self.x_pos -= 10
pygame.init()
size = [500, 700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
pygame.joystick.init()
textPrint = TextPrint()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.JOYBUTTONDOWN:
print("Joystick button pressed.")
if event.type == pygame.JOYBUTTONUP:
print("Joystick button released.")
screen.fill(WHITE)
textPrint.reset()
joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
axes = joystick.get_numaxes()
textPrint.print1(screen, "Number of axes: {}".format(axes) )
textPrint.indent()
for i in range( axes):
if(i==0 or i==1):
axis = joystick.get_axis( i )
textPrint.print1(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
# if(axis>0.01 or axis<0):
# print axis
textPrint.unindent()
buttons = joystick.get_numbuttons()
for i in range(buttons):
button = joystick.get_button(i)
if(i==0 or i==1 or i==2 or i==3 or i == 11 or i==12 or i==13 or i==14):
if(button==1):
print i,button
MESSAGE = pickle.dumps([i,button])
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
textPrint.print1(screen, "Button {:>2} vsalue: {}".format(i, button))
textPrint.unindent()
pygame.display.flip()
clock.tick(10000)
pygame.quit()