-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.py
123 lines (105 loc) · 3.88 KB
/
keyboard.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import socket
import sys
import pygame as pg
import time
def client_program():
print('Started server')
pg.init()
#host = socket.gethostname() # as both code is running on same pc
host = '192.168.43.31'
port = 5031 # socket server port number
client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server
print('connected to the server')
done = False
pg.joystick.init()
if(pg.joystick.get_count()!=0):
joystick = pg.joystick.Joystick(0)
print(joystick)
joystick.init()
while not done:
time.sleep(.4)
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
axis1 = joystick.get_axis( 1 ) #for forward movement
axis0 = joystick.get_axis( 0 ) #for left and right movement
axis2 = joystick.get_axis( 2 )
A = joystick.get_button( 0 )
B = joystick.get_button( 1 )
Y = joystick.get_button( 3 )
X = joystick.get_button( 2 )
keys = pg.key.get_pressed()
if A == 1:
pressed.append(1)
if B == 1:
pressed.append(2)
if Y == 1:
pressed.append(3)
if X == 1:
pressed.append(0)
if pressed[-1] == 0:
speed = ",190"
if pressed[-1] == 1:
speed = ",100"
if pressed[-1] == 2:
speed = ",120"
if pressed[-1] == 3:
speed = ",150"
message = "0,0"+speed+speed
if keys[pg.K_a] or axis0 <= -0.85: #to move left
message = "3,3"+speed+speed
if keys[pg.K_d]or axis0 >= 0.85: #to move right
message = "4,4"+speed+speed
if keys[pg.K_w]or axis1 <= -0.85: #to move up
message = "1,1"+speed+speed
if keys[pg.K_s]or axis1 >= 0.85: #to move down
message = "2,2"+speed+speed
if axis2 <= -0.85:
message = "2,1"+speed+speed
if axis2 >= 0.85:
message = "1,2"+speed+speed
print(message)
#message = input(" -> ") # take input
#key = input()
client_socket.send(message.encode()) # send message
data = client_socket.recv(1024).decode() # receive response
print('Received from server: ' + data) # show in terminal
# message = input(" -> ") # again take input
#key = input()
client_socket.close() # close the connection
else:
while not done:
time.sleep(.4)
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
keys = pg.key.get_pressed()
message = "0,0,0,0"
if keys[pg.K_a]: #to move left
message = "3,3,255,255"
elif keys[pg.K_d]: #to move right
message = "4,4,255,255"
elif keys[pg.K_w]: #to move up
message = "1,1,255,255"
elif keys[pg.K_s]: #to move down
message = "2,2,255,255"
elif keys[pg.K_q]:
message = "2,1,255,255"
elif keys[pg.K_e]:
message = "1,2,255,255"
print(message)
#message = input(" -> ") # take input
#key = input()
client_socket.send(message.encode()) # send message
data = client_socket.recv(1024).decode() # receive response
print('Received from server: ' + data) # show in terminal
# message = input(" -> ") # again take input
#key = input()
client_socket.close() # close the connection
if __name__ == '__main__':
global pressed
pressed=[0]
client_program()
pg.quit()
sys.exit()