-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpet.py
57 lines (49 loc) · 2.1 KB
/
pet.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
import tkinter as tk
import time
class pet():
def __init__(self):
self.window = tk.Tk()
#code below generates string for each frame in gif
self.moveleft = [tk.PhotoImage(file='duck-left.gif', format='gif -index %i' % (i)) for i in range(10)]
self.moveright = [tk.PhotoImage(file='duck-right.gif', format='gif -index %i' % (i)) for i in range(10)]
self.frame_index = 0 #setting starting frame
self.img = self.moveleft[self.frame_index] #starting direction gif
self.timestamp = time.time()
self.window.config(background='black')
self.window.wm_attributes('-transparentcolor', 'black')
self.window.overrideredirect(True) #makes window frameless
self.window.attributes('-topmost', True) #puts window on top
self.label = tk.Label(self.window, bd=0, bg='black') #creates a label as a container for a gif
#starting points
self.x = 1040
self.y = 670
self.window.geometry('128x128+{}+{}'.format(str(self.x), str(self.y)))
self.label.configure(image=self.img)
self.label.pack()
self.window.after(0, self.update)
self.dir = -1 #starting direction
self.window.mainloop()
def changetime(self, direction):
if time.time() > self.timestamp + 0.05:
self.timestamp = time.time()
self.frame_index = (self.frame_index + 1) % 5 #speed of frames change
self.img = direction[self.frame_index]
def changedir(self):
self.dir = -(self.dir)
def go(self):
self.x = self.x + self.dir
if self.dir <0:
direction = self.moveleft
else:
direction = self.moveright
self.changetime(direction)
def update(self):
self.go()
if self.x == 560 or self.x == 1060:
self.changedir()
self.window.geometry('128x128+{}+{}'.format(str(self.x), str(self.y)))
self.label.configure(image=self.img)
self.label.pack()
self.window.after(10, self.update) #10 is frames number for my gif
self.window.lift()
pet()