-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroboserver.py
102 lines (87 loc) · 2.45 KB
/
roboserver.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
#!/usr/bin/python
import pygame, socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("", 5660))
serversocket.listen(1)
conn, addr = serversocket.accept()
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
# Set the width and height of the screen [width,height]
size = [640, 480]
screen = pygame.display.set_mode(size)
global done
done = False
pygame.display.set_caption("Robo Kal")
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
# -------- Main Program Loop -----------
joynum = 0
joystick_count = pygame.joystick.get_count()
if joystick_count > 0:
joystick = pygame.joystick.Joystick(joynum)
joystick.init()
x_key = float(0)
y_key = float(0)
x_joy = float(0)
y_joy = float(0)
add = float(0)
while not done:
Ox_key = x_key
Oy_key = y_key
Ox_joy = x_joy
Oy_joy = y_joy
x_key = float(0)
y_key = float(0)
x_joy = float(0)
y_joy = float(0)
# EVENT PROCESSING STEP
for event in pygame.event.get():
if event.type == pygame.QUIT:
global done
done = True
# PPossible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN
# JOYBUTTONUP JOYHATMOTION
if event.type == pygame.JOYBUTTONDOWN:
if joystick.get_button(4):
add -= float(0.025)
print "Will add"+str(add)
if joystick.get_button(5):
add += float(0.025)
print "Will add"+str(add)
press=pygame.key.get_pressed()
if press[pygame.K_w]:
y_key += float(-0.75)
if press[pygame.K_a]:
x_key += float(-0.75)
if press[pygame.K_s]:
y_key += float(0.75)
if press[pygame.K_d]:
x_key += float(0.75)
if joystick_count > 0:
x_joy = joystick.get_axis(0)
y_joy = joystick.get_axis(1)
print (str(x_joy) + str(y_joy))
if x_joy < float(0.3) and x_joy > float(-0.3):
x_joy = float(0)
elif x_joy < float(-0.3):
x_joy -= add
elif x_joy > float(0.3):
x_joy += add
if y_joy < float(0.3) and y_joy > float(-0.3):
y_joy = float(0)
elif y_joy < float(-0.3):
y_joy -= add
elif y_joy > float(0.3):
y_joy += add
if x_joy > float(0.3) or x_joy < float(-0.3) or y_joy > float(0.3) or y_joy < float(-0.3) or Oy_joy != 0 or Ox_joy != 0:
conn.sendall(str(x_joy) + "," +str(y_joy)+",")
if x_key > float(0.3) or x_key < float(-0.3) or y_key > float(0.3) or y_key < float(-0.3) or Oy_key != 0 or Ox_key != 0:
conn.sendall(str(x_key) + "," +str(y_key)+",")
# Limit to 10 frames per second
clock.tick(20)
pygame.quit()