Amazing-Python-Scripts

Форк
0
239 строк · 6.1 Кб
1
import speech_recognition as sr
2
import pyttsx3
3
import pywhatkit
4
import datetime
5
import wikipedia
6
import pyjokes
7
import geocoder
8
import pyautogui
9

10
import turtle
11
import random
12

13
import pygame
14
import pygame.camera
15

16
listener = sr.Recognizer()
17
engine = pyttsx3.init()
18
voices = engine.getProperty('voices')
19
engine.setProperty('voice', voices[1].id)
20

21

22
def talk(text):
23
    engine.say(text)
24
    engine.runAndWait()
25

26

27
talk('Hello buddy , What can i help you')
28

29

30
def take_command():
31
    try:
32
        with sr.Microphone() as source:
33
            print('listening...')
34
            voice = listener.listen(source)
35
            command = listener.recognize_google(voice)
36
            command = command.lower()
37
            if 'nova' in command:
38
                command = command.replace('nova', '')
39
                print(command)
40
    except:
41
        pass
42
    return command
43

44

45
def run_alexa():
46
    command = take_command()
47
    print(command)
48

49
    # play song on youtube
50
    if 'play' in command:
51
        song = command.replace('play', '')
52
        talk('playing' + song)
53
        pywhatkit.playonyt(song)
54

55
    # Current time
56
    elif 'time' in command:
57
        time = datetime.datetime.now().strftime('%H:%M')
58
        print(time)
59
        talk('Current time is ' + time)
60

61
    # wikipedia answer
62
    elif 'what is' in command:
63
        person = command.replace('See', '')
64
        info = wikipedia.summary(person, 2)
65
        print(info)
66
        talk(info)
67
    elif 'tell me' in command:
68
        person = command.replace('See', '')
69
        info = wikipedia.summary(person, 2)
70
        print(info)
71
        talk(info)
72
    elif 'who is' in command:
73
        person = command.replace('See', '')
74
        info = wikipedia.summary(person, 2)
75
        print(info)
76
        talk(info)
77

78
    # fun with nova
79
    elif 'i love you' in command:
80
        talk('i love you too')
81
    elif 'what are you doing' in command:
82
        talk('I am talking to you')
83
    elif 'are you single' in command:
84
        talk('I am relationship with wifi')
85

86
    elif 'joke' in command:
87
        print(pyjokes.get_joke())
88
        talk(pyjokes.get_joke())
89

90
    # current location
91
    elif 'location' in command:
92
        g = geocoder.ip('me')
93
        print(g.city)
94
        talk('your current location is' + g.city)
95

96
    # take a screen shot
97
    elif 'screenshot' in command:
98
        im = pyautogui.screenshot()
99
        im.save("SS1.jpg")
100

101
    # Take some photo
102
    elif 'take a photo' in command:
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
114
    elif 'snake game' in command:
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)
146

147
                if not food_collision():
148
                    snake.pop(0)
149

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
158

159
                pen.clearstamps()
160

161
                for segment in snake:
162
                    pen.goto(segment[0], segment[1])
163
                    pen.stamp()
164

165
                screen.update()
166

167
                turtle.ontimer(move_snake, delay)
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"
192

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
234
    else:
235
        talk('Please say the command again.')
236

237

238
while True:
239
    run_alexa()
240

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.