-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (80 loc) · 3 KB
/
main.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
import tkinter as tk
import random as rand
class cube():
def __init__(self):
self.window = tk.Tk()
# load animations
self.idle = [tk.PhotoImage(file='idle.gif', format='gif -index %i' % (i)) for i in range(10)]
self.sleep = [tk.PhotoImage(file='sleep.gif', format='gif -index %i' % (i)) for i in range(5)]
self.walkL = [tk.PhotoImage(file='walkL.gif', format='gif -index %i' % (i)) for i in range(8)]
self.walkR = [tk.PhotoImage(file='walkR.gif', format='gif -index %i' % (i)) for i in range(8)]
self.frameI = 0
# init the image
self.img = self.idle[self.frameI]
# init window
self.window.wm_attributes('-transparentcolor', 'white')
self.window.overrideredirect(True)
self.window.config(background='white')
self.window.attributes('-topmost', True)
self.label = tk.Label(self.window, bd=0, bg='white')
# event
self.idleE = [1, 2]
self.sleepE = [3, 4, 5, 6]
self.walkLE = [6, 7, 8, 9]
self.walkRE = [10, 11]
self.event = rand.randrange(1, 12)
# start points
self.x = 1040
self.y = 550
self.window.geometry('100x100+{}+{}'.format(str(self.x), str(self.y)))
self.label.configure(image=self.img)
self.label.pack()
self.window.after(0, self.update)
self.window.mainloop()
def update(self):
# idle
if self.event in self.idleE:
if self.frameI < len(self.idle):
self.img = self.idle[self.frameI] # set frame to next frame
self.frameI += 1
else:
self.frameI = 0 # reset frame
self.event = rand.randrange(1, 12)
# sleep
elif self.event in self.sleepE:
if self.frameI < len(self.sleep):
self.img = self.sleep[self.frameI]
self.frameI += 1
else:
self.frameI = 0
self.event = rand.randrange(1, 12)
# walk left
elif self.event in self.walkLE:
if self.frameI < len(self.walkL):
self.img = self.walkL[self.frameI]
self.frameI += 1
self.x -= 3
else:
self.frameI = 0
self.event = rand.randrange(1, 12)
# walk right
else:
if self.frameI < len(self.walkR):
self.img = self.walkR[self.frameI]
self.frameI += 1
self.x += 3
else:
self.frameI = 0
self.event = rand.randrange(1, 12)
# stop cube from going off screen (need to check if this works)
if self.x > 1100:
self.x = 0
if self.x < 0:
self.x = 1100
# update frame
self.window.geometry('100x100+{}+{}'.format(str(self.x), str(self.y)))
self.label.configure(image=self.img)
self.label.pack()
self.window.after(300, self.update)
self.window.lift()
cube()