-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygametx.py
67 lines (57 loc) · 1.15 KB
/
pygametx.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
import sys, os, pygame, time, struct
import serial
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((696, 396))
pygame.display.set_caption('Pygame Caption')
pygame.mouse.set_visible(0)
ser = serial.Serial('/dev/ttyUSB1',9600)
send='r'
up = False
down = False
left = False
right = False
def input(events):
global send
global down
global left
global right
global up
for event in events:
print event
if event.type == QUIT:
sys.exit(0)
if event.type == KEYDOWN:
if event.key == K_LEFT:
left = True
if event.key == K_RIGHT:
right = True
if event.key == K_UP:
up = True
if event.key == K_DOWN:
down = True
if event.type == KEYUP:
if event.key == K_LEFT:
left = False
if event.key == K_RIGHT:
right = False
if event.key == K_UP:
up = False
if event.key == K_DOWN:
down = False
while True:
send='r'
input( pygame.event.get() )
if up:
send = 'w'
if down:
send = 's'
if left:
send = 'a'
if right:
send = 'd'
print send
#ser.write(struct.pack("!h",send))
ser.write(send)
#ser.flush()
time.sleep(0.1) #best so far: 0.1