18
18
voices = engine .getProperty ('voices' )
19
19
engine .setProperty ('voice' , voices [1 ].id )
20
20
21
+
21
22
def talk (text ):
22
23
engine .say (text )
23
24
engine .runAndWait ()
24
25
26
+
25
27
talk ('Hello buddy , What can i help you' )
26
- def take_command ():
28
+
29
+
30
+ def take_command ():
27
31
try :
28
32
with sr .Microphone () as source :
29
33
print ('listening...' )
30
34
voice = listener .listen (source )
31
35
command = listener .recognize_google (voice )
32
- command = command .lower ()
36
+ command = command .lower ()
33
37
if 'nova' in command :
34
38
command = command .replace ('nova' , '' )
35
39
print (command )
36
40
except :
37
41
pass
38
42
return command
39
43
44
+
40
45
def run_alexa ():
41
- command = take_command ()
46
+ command = take_command ()
42
47
print (command )
43
48
44
- #play song on youtube
49
+ #play song on youtube
45
50
if 'play' in command :
46
51
song = command .replace ('play' , '' )
47
52
talk ('playing' + song )
48
53
pywhatkit .playonyt (song )
49
54
50
- #Current time
55
+ #Current time
51
56
elif 'time' in command :
52
57
time = datetime .datetime .now ().strftime ('%H:%M' )
53
58
print (time )
54
- talk ('Current time is ' + time )
59
+ talk ('Current time is ' + time )
55
60
56
- #wikipedia answer
61
+ #wikipedia answer
57
62
elif 'what is' in command :
58
63
person = command .replace ('See' , '' )
59
64
info = wikipedia .summary (person , 2 )
@@ -70,176 +75,165 @@ def run_alexa():
70
75
print (info )
71
76
talk (info )
72
77
73
- #fun with nova
78
+ #fun with nova
74
79
elif 'i love you' in command :
75
80
talk ('i love you too' )
76
81
elif 'what are you doing' in command :
77
82
talk ('I am talking to you' )
78
83
elif 'are you single' in command :
79
84
talk ('I am relationship with wifi' )
80
-
85
+
81
86
elif 'joke' in command :
82
87
print (pyjokes .get_joke ())
83
88
talk (pyjokes .get_joke ())
84
89
85
- #current location
90
+ #current location
86
91
elif 'location' in command :
87
92
g = geocoder .ip ('me' )
88
93
print (g .city )
89
94
talk ('your current location is' + g .city )
90
95
91
- #take a screen shot
96
+ #take a screen shot
92
97
elif 'screenshot' in command :
93
98
im = pyautogui .screenshot ()
94
99
im .save ("SS1.jpg" )
95
100
96
- #Take some photo
101
+ #Take some photo
97
102
elif 'take a photo' in command :
98
- pygame .camera .init ()
99
- camlist = pygame .camera .list_cameras ()
100
- if camlist :
101
- cam = pygame .camera .Camera (camlist [0 ], (640 , 480 ))
102
- cam .start ()
103
- image = cam .get_image ()
104
- pygame .image .save (image , "filename.jpg" )
105
- else :
106
- print ("No camera on current device" )
107
-
108
-
109
- #play snake game
103
+ pygame .camera .init ()
104
+ camlist = pygame .camera .list_cameras ()
105
+ if camlist :
106
+ cam = pygame .camera .Camera (camlist [0 ], (640 , 480 ))
107
+ cam .start ()
108
+ image = cam .get_image ()
109
+ pygame .image .save (image , "filename.jpg" )
110
+ else :
111
+ print ("No camera on current device" )
112
+
113
+ # play snake game
110
114
elif 'snake game' in command :
111
- w = 500
112
- h = 500
113
- food_size = 10
114
- delay = 100
115
-
116
- offsets = {
117
- "up" : (0 , 20 ),
118
- "down" : (0 , - 20 ),
119
- "left" : (- 20 , 0 ),
120
- "right" : (20 , 0 )
121
- }
122
-
123
- def reset ():
124
- global snake , snake_dir , food_position , pen
125
- snake = [[0 , 0 ], [0 , 20 ], [0 , 40 ], [0 , 60 ], [0 , 80 ]]
126
- snake_dir = "up"
127
- food_position = get_random_food_position ()
128
- food .goto (food_position )
129
- move_snake ()
130
-
131
- def move_snake ():
132
- global snake_dir
133
-
134
- new_head = snake [- 1 ].copy ()
135
- new_head [0 ] = snake [- 1 ][0 ] + offsets [snake_dir ][0 ]
136
- new_head [1 ] = snake [- 1 ][1 ] + offsets [snake_dir ][1 ]
137
-
138
-
139
- if new_head in snake [:- 1 ]:
140
- reset ()
141
- else :
142
- snake .append (new_head )
143
-
144
-
145
- if not food_collision ():
146
- snake .pop (0 )
147
-
148
-
149
- if snake [- 1 ][0 ] > w / 2 :
150
- snake [- 1 ][0 ] -= w
151
- elif snake [- 1 ][0 ] < - w / 2 :
152
- snake [- 1 ][0 ] += w
153
- elif snake [- 1 ][1 ] > h / 2 :
154
- snake [- 1 ][1 ] -= h
155
- elif snake [- 1 ][1 ] < - h / 2 :
156
- snake [- 1 ][1 ] += h
157
-
158
-
159
- pen .clearstamps ()
160
-
161
-
162
- for segment in snake :
163
- pen .goto (segment [0 ], segment [1 ])
164
- pen .stamp ()
165
-
166
-
167
- screen .update ()
168
-
169
- turtle .ontimer (move_snake , delay )
170
-
171
- def food_collision ():
172
- global food_position
173
- if get_distance (snake [- 1 ], food_position ) < 20 :
174
- food_position = get_random_food_position ()
175
- food .goto (food_position )
176
- return True
177
- return False
178
-
179
- def get_random_food_position ():
180
- x = random .randint (- w / 2 + food_size , w / 2 - food_size )
181
- y = random .randint (- h / 2 + food_size , h / 2 - food_size )
182
- return (x , y )
183
-
184
- def get_distance (pos1 , pos2 ):
185
- x1 , y1 = pos1
186
- x2 , y2 = pos2
187
- distance = ((y2 - y1 ) ** 2 + (x2 - x1 ) ** 2 ) ** 0.5
188
- return distance
189
- def go_up ():
190
- global snake_dir
191
- if snake_dir != "down" :
192
- snake_dir = "up"
193
-
194
- def go_right ():
195
- global snake_dir
196
- if snake_dir != "left" :
197
- snake_dir = "right"
198
-
199
- def go_down ():
200
- global snake_dir
201
- if snake_dir != "up" :
202
- snake_dir = "down"
203
-
204
- def go_left ():
205
- global snake_dir
206
- if snake_dir != "right" :
207
- snake_dir = "left"
208
-
209
-
210
- screen = turtle .Screen ()
211
- screen .setup (w , h )
212
- screen .title ("Snake" )
213
- screen .bgcolor ("blue" )
214
- screen .setup (500 , 500 )
215
- screen .tracer (0 )
216
-
217
-
218
- pen = turtle .Turtle ("square" )
219
- pen .penup ()
115
+ w = 500
116
+ h = 500
117
+ food_size = 10
118
+ delay = 100
119
+
120
+ offsets = {
121
+ "up" : (0 , 20 ),
122
+ "down" : (0 , - 20 ),
123
+ "left" : (- 20 , 0 ),
124
+ "right" : (20 , 0 )
125
+ }
126
+
127
+ def reset ():
128
+ global snake , snake_dir , food_position , pen
129
+ snake = [[0 , 0 ], [0 , 20 ], [0 , 40 ], [0 , 60 ], [0 , 80 ]]
130
+ snake_dir = "up"
131
+ food_position = get_random_food_position ()
132
+ food .goto (food_position )
133
+ move_snake ()
134
+
135
+ def move_snake ():
136
+ global snake_dir
137
+
138
+ new_head = snake [- 1 ].copy ()
139
+ new_head [0 ] = snake [- 1 ][0 ] + offsets [snake_dir ][0 ]
140
+ new_head [1 ] = snake [- 1 ][1 ] + offsets [snake_dir ][1 ]
141
+
142
+ if new_head in snake [:- 1 ]:
143
+ reset ()
144
+ else :
145
+ snake .append (new_head )
220
146
147
+ if not food_collision ():
148
+ snake .pop (0 )
221
149
222
- food = turtle .Turtle ()
223
- food .shape ("square" )
224
- food .color ("yellow" )
225
- food .shapesize (food_size / 20 )
226
- food .penup ()
150
+ if snake [- 1 ][0 ] > w / 2 :
151
+ snake [- 1 ][0 ] -= w
152
+ elif snake [- 1 ][0 ] < - w / 2 :
153
+ snake [- 1 ][0 ] += w
154
+ elif snake [- 1 ][1 ] > h / 2 :
155
+ snake [- 1 ][1 ] -= h
156
+ elif snake [- 1 ][1 ] < - h / 2 :
157
+ snake [- 1 ][1 ] += h
227
158
159
+ pen .clearstamps ()
228
160
229
- screen .listen ()
230
- screen .onkey (go_up , "Up" )
231
- screen .onkey (go_right , "Right" )
232
- screen .onkey (go_down , "Down" )
233
- screen .onkey (go_left , "Left" )
161
+ for segment in snake :
162
+ pen .goto (segment [0 ], segment [1 ])
163
+ pen .stamp ()
234
164
165
+ screen .update ()
235
166
236
- reset ()
237
- turtle .done ()
167
+ turtle .ontimer (move_snake , delay )
238
168
169
+ def food_collision ():
170
+ global food_position
171
+ if get_distance (snake [- 1 ], food_position ) < 20 :
172
+ food_position = get_random_food_position ()
173
+ food .goto (food_position )
174
+ return True
175
+ return False
176
+
177
+ def get_random_food_position ():
178
+ x = random .randint (- w / 2 + food_size , w / 2 - food_size )
179
+ y = random .randint (- h / 2 + food_size , h / 2 - food_size )
180
+ return (x , y )
181
+
182
+ def get_distance (pos1 , pos2 ):
183
+ x1 , y1 = pos1
184
+ x2 , y2 = pos2
185
+ distance = ((y2 - y1 ) ** 2 + (x2 - x1 ) ** 2 ) ** 0.5
186
+ return distance
187
+
188
+ def go_up ():
189
+ global snake_dir
190
+ if snake_dir != "down" :
191
+ snake_dir = "up"
239
192
240
- #any command doesn't match nova talk this line
193
+ def go_right ():
194
+ global snake_dir
195
+ if snake_dir != "left" :
196
+ snake_dir = "right"
197
+
198
+ def go_down ():
199
+ global snake_dir
200
+ if snake_dir != "up" :
201
+ snake_dir = "down"
202
+
203
+ def go_left ():
204
+ global snake_dir
205
+ if snake_dir != "right" :
206
+ snake_dir = "left"
207
+
208
+ screen = turtle .Screen ()
209
+ screen .setup (w , h )
210
+ screen .title ("Snake" )
211
+ screen .bgcolor ("blue" )
212
+ screen .setup (500 , 500 )
213
+ screen .tracer (0 )
214
+
215
+ pen = turtle .Turtle ("square" )
216
+ pen .penup ()
217
+
218
+ food = turtle .Turtle ()
219
+ food .shape ("square" )
220
+ food .color ("yellow" )
221
+ food .shapesize (food_size / 20 )
222
+ food .penup ()
223
+
224
+ screen .listen ()
225
+ screen .onkey (go_up , "Up" )
226
+ screen .onkey (go_right , "Right" )
227
+ screen .onkey (go_down , "Down" )
228
+ screen .onkey (go_left , "Left" )
229
+
230
+ reset ()
231
+ turtle .done ()
232
+
233
+ # any command doesn't match nova talk this line
241
234
else :
242
235
talk ('Please say the command again.' )
243
-
236
+
237
+
244
238
while True :
245
- run_alexa ()
239
+ run_alexa ()
0 commit comments