-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
179 lines (158 loc) · 5.47 KB
/
test.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/python3
import pygame
import numpy as np
from pygame.locals import *
from gtts import gTTS
import subprocess
import os
from itertools import product
from math import atan2, pi, cos, sin
white = (255,255,255)
black = (0,0,0)
lines = ['GREEN LINE B','GREEN LINE C','GREEN LINE D','GREEN LINE E','RED LINE']
directions = ['OUTBOUND','INBOUND']
tutorial = 'Swipe up or down to change direction. Swipe left or right to change destination.'
stop = 'You have arrived!'
nLines = len(lines)
nDirections = len(directions)
soundProcess = None
def speak_word(word):
global soundProcess
if soundProcess is not None:
soundProcess.kill()
soundProcess = subprocess.Popen(["mpg123",os.path.join("sounds",word)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
def init_sounds():
for line, dir in product(lines, directions):
string = line+' '+dir
path = os.path.join("sounds",string)
if not os.path.isfile(path):
tts = gTTS(text=string,lang='en')
tts.save(path)
path = os.path.join('sounds','tutorial')
if not os.path.isfile(path):
tts = gTTS(text = tutorial, lang='en')
tts.save(path)
path = os.path.join('sounds','stop')
if not os.path.isfile(path):
tts = gTTS(text = stop, lang='en')
tts.save(path)
def main():
init_sounds()
speak_word('tutorial')
W, H = (640,480)
pygame.init()
#set to true to close the window
done = False
#pygame init
screen = pygame.display.set_mode((W,H))
beep = pygame.mixer.Sound(os.path.join('sounds','beep.ogg'))
font = pygame.font.SysFont('arial', 20)
pygame.event.set_grab(True)
pygame.mouse.set_visible(False)
#destination selection
line = 0
direction = 0
#player status
goal = np.random.random(size = 2)*1000
loc = np.array([500.0,500.0])
angle = 0
speed = 0.05
angleSpeed = 0.0002
#limits and cutoffs
minBeepTime = 600
angleRange = pi/24
distRange = 100
arrived = False
minVolume = 0.05
#timing and swiping
startTimeout = 500
swipeTimeout = 100
swipe = np.array([0,0])
minSwipeLen = 500
lastMove = pygame.time.get_ticks()
startTime = lastMove
lastBeep = lastMove
#main loop
while not done:
#handel input
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEMOTION:
if (pygame.time.get_ticks()-startTime) > startTimeout:
lastMove = pygame.time.get_ticks()
swipe += event.rel
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
elif event.type == pygame.MOUSEBUTTONUP:
speak_word(lines[line]+' '+directions[direction])
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]:
angle += angleSpeed
if keys[pygame.K_RIGHT] and not keys[pygame.K_LEFT]:
angle -= angleSpeed
if keys[pygame.K_UP] and not keys[pygame.K_DOWN]:
loc += np.array([cos(angle),sin(angle)])*speed
if keys[pygame.K_DOWN] and not keys[pygame.K_UP]:
loc -= np.array([cos(angle),sin(angle)])*speed
#detect swipes
time = pygame.time.get_ticks()
if time-lastMove > swipeTimeout:
sx, sy = swipe
len = np.linalg.norm(swipe)
if len > minSwipeLen:
if abs(sx) > abs(sy):
line += np.sign(sx)
line %= nLines
else:
direction += np.sign(sy)
direction %= nDirections
goal = np.random.random(size = 2)*1000
speak_word(lines[line]+' '+directions[direction])
swipe = np.array([0,0])
#do radar
goalHeading = goal - loc
dist = np.linalg.norm(goalHeading)
#are we there yet?
if dist<distRange:
if not arrived:
speak_word('stop')
arrived = True
#do beeps
elif (pygame.time.get_ticks()-lastBeep) > dist+minBeepTime:
arrived = False
lastBeep = pygame.time.get_ticks()
goalAngle = (atan2(goalHeading[1],goalHeading[0])-angle) % (2*pi)
if goalAngle > pi:
goalAngle -= 2*pi
#print(angle, goalAngle, dist)
if abs(goalAngle)<.1:
channel = beep.play()
channel.set_volume(1,1)
elif goalAngle > 0:
channel = beep.play()
volume = max((pi-goalAngle)/pi, minVolume)
channel.set_volume(volume,0)
else:
channel = beep.play()
volume = max((pi+goalAngle)/pi, minVolume)
channel.set_volume(0,volume)
#render screen
screen.fill(black)
lineStr = lines[line]
directionStr = directions[direction]
text = font.render(lineStr, True, white)
textRec = text.get_rect()
h = textRec.height
textRec.center = (W//2, H//2)
screen.blit(text,textRec)
text = font.render(directionStr, True, white)
textRec = text.get_rect()
textRec.center = (W//2, H//2+h)
screen.blit(text,textRec)
pygame.display.update()
if __name__ == '__main__':
main()