This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDavesBigBrain.py
295 lines (215 loc) · 8.42 KB
/
DavesBigBrain.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
__version__ = '4.0.3'
__author__ = 'SanoKei'
# In[ ]:
'''Imports'''
from tkinter import Tk, Frame, Canvas, PhotoImage
import tkinter as tk
import configparser
from multiprocessing import Process
import os
import random
import time
import json
import math
from PIL import Image, ImageTk
from threading import Thread
from selenium import webdriver
from win32api import GetMonitorInfo, MonitorFromPoint
from win32com.client import Dispatch
from playsound import playsound
# In[ ]:
'''Initialize'''
# initialize tkinter
root = Tk()
# get the directory of the file
current_directory = os.getcwd()
davidImgPath = current_directory + '\\resources\\images\\David\\'
# In[ ]:
'''Get Stoff'''
# get taskbar height
def get_task_bar_height():
monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
monitor_area = monitor_info.get("Monitor")
work_area = monitor_info.get("Work")
return monitor_area[3]-work_area[3]
barHeight = get_task_bar_height()
# get chrome version
def get_version_via_com(filename):
parser = Dispatch("Scripting.FileSystemObject")
try:
version = parser.GetFileVersion(filename)
except Exception:
return None
return version
paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]
chrome_version = str(list(filter(None, [get_version_via_com(p) for p in paths]))[0]).split('.')[0]
# get direction to face david
def get_direction_for_david(action_rand, cords):
fileName = "move"
if (math.sqrt((root.winfo_rootx()-cords[0])**2) >= math.sqrt((root.winfo_rooty()-cords[1])**2)):
fileName += ""
else:
fileName += "Y"
if(cords[0] >= root.winfo_rootx()):
# right
fileName += "Right"
else:
fileName += "Left"
return fileName
# get a random spot on the screen
def get_random_on_screen(width,height):
x,y = random.choice(range(width - 128)),random.choice(range(height - barHeight - 128))
return [x,y]
def get_incrim_of_cords(cords,maxFrame):
new_x = (cords[0] - root.winfo_rootx()) / maxFrame
new_y = (cords[1] - root.winfo_rooty()) / maxFrame
return [new_x,new_y]
# In[ ]:
'''David'''
### Images ###
# idle
idleLeft = ["DAVE_IDLE-LEFT_0_0.gif","DAVE_IDLE-LEFT_0_1.gif","DAVE_IDLE-LEFT_0_2.gif","DAVE_IDLE-LEFT_0_3.gif","DAVE_IDLE-LEFT_0_2.gif","DAVE_IDLE-LEFT_0_1.gif"]
idleRight = ["DAVE_IDLE-RIGHT_0_0.gif","DAVE_IDLE-RIGHT_0_1.gif","DAVE_IDLE-RIGHT_0_2.gif","DAVE_IDLE-RIGHT_0_3.gif","DAVE_IDLE-RIGHT_0_2.gif","DAVE_IDLE-RIGHT_0_1.gif"]
moveLeft = ["DAVE_MOVE-LEFT_0_0.gif","DAVE_MOVE-LEFT_0_1.gif"]
moveRight = ["DAVE_MOVE-RIGHT_0_0.gif","DAVE_MOVE-RIGHT_0_1.gif"]
moveYLeft = ["DAVE_MOVE_Y-LEFT_0_0.gif","DAVE_MOVE_Y-LEFT_0_1.gif","DAVE_MOVE_Y-LEFT_0_2.gif","DAVE_MOVE_Y-LEFT_0_3.gif","DAVE_MOVE_Y-LEFT_0_2.gif","DAVE_MOVE_Y-LEFT_0_1.gif",]
moveYRight = ["DAVE_MOVE_Y-RIGHT_0_0.gif","DAVE_MOVE_Y-RIGHT_0_1.gif","DAVE_MOVE_Y-RIGHT_0_2.gif","DAVE_MOVE_Y-RIGHT_0_3.gif","DAVE_MOVE_Y-RIGHT_0_2.gif","DAVE_MOVE_Y-RIGHT_0_1.gif",]
quackLeft = ["DAVE_QUACK-LEFT_0_0.gif","DAVE_QUACK-LEFT_0_1.gif"]
quackRight = ["DAVE_QUACK-RIGHT_0_0.gif","DAVE_QUACK-RIGHT_0_1.gif"]
### Animation ###
animation = {
"idleLeft": idleLeft,
"idleRight": idleRight,
"moveLeft": moveLeft,
"moveRight": moveRight,
"moveYLeft": moveYLeft,
"moveYRight": moveYRight,
"webRight": idleRight,
"quackLeft": quackLeft,
"quackRight": quackRight
}
# In[ ]:
'''Chrome'''
### Chrome to Version ###
version_to_path = {
"91": current_directory + "\\resources\\chromeDriver\\91_chromedriver.exe",
"92": current_directory + "\\resources\\chromeDriver\\92_chromedriver.exe",
"93": current_directory + "\\resources\\chromeDriver\\93_chromedriver.exe"
}
# In[ ]:
'''Find Config'''
configParser = configparser.RawConfigParser()
configFilePath = r''+os.getcwd()+'\\config.txt'
configParser.read(configFilePath)
# In[ ]:
'''Read from config'''
### default ###
debug = bool(configParser.get('window', 'debug'))
### window ###
title = str(configParser.get('window', 'title'))
overrideredirect = bool(configParser.get('window', 'overrideredirect'))
top_most = bool(configParser.get('window', 'top_most'))
trans_color = str(configParser.get('window', 'trans_color'))
### david ###
speed = int(configParser.get('david', 'speed'))
### Actions and weights ###
actions = json.loads(configParser.get("david","actions"))
weights = json.loads(configParser.get("david","weights"))
### Files and animation ###
file_name = json.loads(configParser.get("david","file_name"))
rand = json.loads(configParser.get("david","rand"))
sleep = json.loads(configParser.get("david","sleep"))
minval = json.loads(configParser.get("david","minval"))
rick = json.loads(configParser.get("david","url"))
# In[ ]:
'''Configuration'''
### window ###
root.title(title)
root.overrideredirect(overrideredirect)
root.wm_attributes("-topmost", top_most)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", trans_color)
# In[ ]:
'''David Class'''
class David(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill="both", expand=1)
'''Dave Actions'''
def dave_action(self,action,perFrame,photo,spd,url,perm_x,perm_y,hardPerm):
### move ###
perm_x += hardPerm[0]
perm_y += hardPerm[1]
root.geometry("+{}+{}".format(str(int(math.ceil(perm_x))),str(int(math.ceil(perm_y)))))
return [perm_x,perm_y]
'''Create the animation of David'''
def createAnimation(self,imagelist,filePath,perFrame):
path = davidImgPath+filePath+"\\"
# extract width and height info
photo = PhotoImage(file=path+imagelist[0])
width = photo.width()
height = photo.height()
# create a list of image objects
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=path+imagefile)
giflist.append(photo)
# loop through the gif image objects for a while
gif = giflist[perFrame]
canvas.delete("all")
canvas.create_image(width/2.0, height/2.0, image=gif)
canvas.update()
def createAction(self,imagelist,filePath,perFrame,action,spd,url,perm_x,perm_y,hardPerm):
path = davidImgPath+filePath+"\\"
photo = PhotoImage(file=path+imagelist[0])
if("move" in imagelist[0].lower()):
return self.dave_action(action,perFrame,photo,spd,url,perm_x,perm_y,hardPerm)
else:
self.dave_action(action,perFrame,photo,spd,url,perm_x,perm_y,hardPerm)
# In[ ]:
def mainLoop(david,canvas):
# random action
action_rand = random.choices(actions, weights=weights)
index = actions.index(str(action_rand[0]))
t = random.choice(range(list(rand)[index])) + int(minval[index])
slp = list(sleep)[index]
spd = random.choice(range(speed)) + int(minval[index])
roll = random.choice(rick)
startPoint = [root.winfo_rootx(),root.winfo_rooty()]
perm_x = root.winfo_rootx()
perm_y = root.winfo_rooty()
perFrame = 0
# sterialize animation
if isinstance(action_rand, list):
act_rand = action_rand[0]
else:
act_rand = action_rand
ranSoFarAway = get_random_on_screen(root.winfo_screenwidth(),root.winfo_screenheight())
if("move" in act_rand):
act_rand = get_direction_for_david(action_rand,ranSoFarAway)
maxFrame = t * len(animation.get(act_rand))
hardPerm = get_incrim_of_cords(ranSoFarAway,maxFrame)
# animation and action main loop
for perAnimationLoop in range(t):
for perFrameAnimation in range(len(animation.get(act_rand))):
perFrame += 1
david.createAnimation(animation.get(act_rand),file_name[index],perFrameAnimation)
if("move" in act_rand.lower()):
[perm_x, perm_y] = david.createAction(animation.get(act_rand),file_name[index],perFrame,act_rand,spd,roll,perm_x,perm_y,hardPerm)
else:
david.createAction(animation.get(act_rand),file_name[index],perFrame,action_rand,spd,roll,perm_x,perm_y,hardPerm)
time.sleep(slp)
# In[ ]:
'''Run main window'''
# initialize class of David
david = David(root)
# Create the canvas
canvas = Canvas(width=128, height=128, bg=trans_color, highlightthickness=0)
canvas.pack()
while True:
mainLoop(david,canvas)
root.mainloop()