Amazing-Python-Scripts
239 строк · 6.1 Кб
1import speech_recognition as sr2import pyttsx33import pywhatkit4import datetime5import wikipedia6import pyjokes7import geocoder8import pyautogui9
10import turtle11import random12
13import pygame14import pygame.camera15
16listener = sr.Recognizer()17engine = pyttsx3.init()18voices = engine.getProperty('voices')19engine.setProperty('voice', voices[1].id)20
21
22def talk(text):23engine.say(text)24engine.runAndWait()25
26
27talk('Hello buddy , What can i help you')28
29
30def take_command():31try:32with sr.Microphone() as source:33print('listening...')34voice = listener.listen(source)35command = listener.recognize_google(voice)36command = command.lower()37if 'nova' in command:38command = command.replace('nova', '')39print(command)40except:41pass42return command43
44
45def run_alexa():46command = take_command()47print(command)48
49# play song on youtube50if 'play' in command:51song = command.replace('play', '')52talk('playing' + song)53pywhatkit.playonyt(song)54
55# Current time56elif 'time' in command:57time = datetime.datetime.now().strftime('%H:%M')58print(time)59talk('Current time is ' + time)60
61# wikipedia answer62elif 'what is' in command:63person = command.replace('See', '')64info = wikipedia.summary(person, 2)65print(info)66talk(info)67elif 'tell me' in command:68person = command.replace('See', '')69info = wikipedia.summary(person, 2)70print(info)71talk(info)72elif 'who is' in command:73person = command.replace('See', '')74info = wikipedia.summary(person, 2)75print(info)76talk(info)77
78# fun with nova79elif 'i love you' in command:80talk('i love you too')81elif 'what are you doing' in command:82talk('I am talking to you')83elif 'are you single' in command:84talk('I am relationship with wifi')85
86elif 'joke' in command:87print(pyjokes.get_joke())88talk(pyjokes.get_joke())89
90# current location91elif 'location' in command:92g = geocoder.ip('me')93print(g.city)94talk('your current location is' + g.city)95
96# take a screen shot97elif 'screenshot' in command:98im = pyautogui.screenshot()99im.save("SS1.jpg")100
101# Take some photo102elif 'take a photo' in command:103pygame.camera.init()104camlist = pygame.camera.list_cameras()105if camlist:106cam = pygame.camera.Camera(camlist[0], (640, 480))107cam.start()108image = cam.get_image()109pygame.image.save(image, "filename.jpg")110else:111print("No camera on current device")112
113# play snake game114elif 'snake game' in command:115w = 500116h = 500117food_size = 10118delay = 100119
120offsets = {121"up": (0, 20),122"down": (0, -20),123"left": (-20, 0),124"right": (20, 0)125}126
127def reset():128global snake, snake_dir, food_position, pen129snake = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]130snake_dir = "up"131food_position = get_random_food_position()132food.goto(food_position)133move_snake()134
135def move_snake():136global snake_dir137
138new_head = snake[-1].copy()139new_head[0] = snake[-1][0] + offsets[snake_dir][0]140new_head[1] = snake[-1][1] + offsets[snake_dir][1]141
142if new_head in snake[:-1]:143reset()144else:145snake.append(new_head)146
147if not food_collision():148snake.pop(0)149
150if snake[-1][0] > w / 2:151snake[-1][0] -= w152elif snake[-1][0] < - w / 2:153snake[-1][0] += w154elif snake[-1][1] > h / 2:155snake[-1][1] -= h156elif snake[-1][1] < -h / 2:157snake[-1][1] += h158
159pen.clearstamps()160
161for segment in snake:162pen.goto(segment[0], segment[1])163pen.stamp()164
165screen.update()166
167turtle.ontimer(move_snake, delay)168
169def food_collision():170global food_position171if get_distance(snake[-1], food_position) < 20:172food_position = get_random_food_position()173food.goto(food_position)174return True175return False176
177def get_random_food_position():178x = random.randint(- w / 2 + food_size, w / 2 - food_size)179y = random.randint(- h / 2 + food_size, h / 2 - food_size)180return (x, y)181
182def get_distance(pos1, pos2):183x1, y1 = pos1184x2, y2 = pos2185distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5186return distance187
188def go_up():189global snake_dir190if snake_dir != "down":191snake_dir = "up"192
193def go_right():194global snake_dir195if snake_dir != "left":196snake_dir = "right"197
198def go_down():199global snake_dir200if snake_dir != "up":201snake_dir = "down"202
203def go_left():204global snake_dir205if snake_dir != "right":206snake_dir = "left"207
208screen = turtle.Screen()209screen.setup(w, h)210screen.title("Snake")211screen.bgcolor("blue")212screen.setup(500, 500)213screen.tracer(0)214
215pen = turtle.Turtle("square")216pen.penup()217
218food = turtle.Turtle()219food.shape("square")220food.color("yellow")221food.shapesize(food_size / 20)222food.penup()223
224screen.listen()225screen.onkey(go_up, "Up")226screen.onkey(go_right, "Right")227screen.onkey(go_down, "Down")228screen.onkey(go_left, "Left")229
230reset()231turtle.done()232
233# any command doesn't match nova talk this line234else:235talk('Please say the command again.')236
237
238while True:239run_alexa()240