-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpet_for_mac.py
299 lines (257 loc) · 10.7 KB
/
pet_for_mac.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
295
296
297
298
299
import tkinter as tk
import random
from tkinter import ttk
from threading import Thread
from time import sleep
from PIL import Image, ImageTk, ImageSequence
window = tk.Tk()
# Set screen width and height
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
#begin x
x = 200
#begin y
y = 700
cycle = 0
check = 1
idle_num = [1, 2, 3]
#if event number == 4, idle to sleep
walk_left = [5, 6, 7, 8]
walk_right = [9, 10, 11, 12]
sleep_num = [13, 14]
#if event number == 15, sleep to idle
event_number = random.randrange(1, 3, 1)
impath = './bgifs/'
button_pressed = False
feed_button_pressed = False
pooped_times = 0
# Variables for progress bar
initial_progress = 50
progress_value = tk.DoubleVar(value=initial_progress)
progress_step = 1 # Adjust the decrement step
feed_increment = 10 # Adjust the increment when the feed button is pressed
decrement_interval = 1000 # Adjust the time interval for decrementing (in milliseconds)
# Function to decrement the progress bar slowly
def decrement_progress():
current_value = progress_value.get()
if current_value > 5:
progress_value.set(current_value - progress_step)
window.after(decrement_interval, decrement_progress)
# Function to handle feed button click
def feed_button_click():
global feed_button_pressed
feed_button_pressed = True
current_value = progress_value.get()
new_value = min(100, current_value + feed_increment)
progress_value.set(new_value)
def on_loading(widget, value, loading_end):
print('loading...')
widget.start(150)
sleep(15)
widget.stop()
value(100)
on_ready()
def on_ready():
print("now ready to work...")
def label_click(event):
global button_pressed
global pooped_times
print("label clicked")
button_pressed = True
pooped_times += 1
def unpack_gif(src):
# Load Gif
image = Image.open(src)
# Get frames and disposal method for each frame
frames = []
disposal = []
for gifFrame in ImageSequence.Iterator(image):
disposal.append(gifFrame.disposal_method)
frames.append(gifFrame.convert('RGBA'))
# Loop through frames, and edit them based on their disposal method
output = []
lastFrame = None
thisFrame = None
for i, loadedFrame in enumerate(frames):
# Update thisFrame
thisFrame = loadedFrame
# If the disposal method is 2
if disposal[i] == 2:
# Check that this is not the first frame
if i != 0:
# Pastes thisFrame's opaque pixels over lastFrame and appends lastFrame to output
lastFrame.paste(thisFrame, mask=thisFrame.split()[3])
output.append(ImageTk.PhotoImage(lastFrame))
else:
output.append(ImageTk.PhotoImage(thisFrame))
# If the disposal method is 1 or 0
elif disposal[i] == 1 or disposal[i] == 0:
# Appends thisFrame to output
output.append(ImageTk.PhotoImage(thisFrame))
# If disposal method is anything other than 2, 1, or 0
else:
raise ValueError('Disposal Methods other than 2: Restore to Background, 1: Do Not Dispose, and 0: No Disposal are supported at this time')
# Update lastFrame
lastFrame = loadedFrame
return output
def event(cycle, check, event_number, x, y):
global button_pressed
global pooped_times
global feed_button_pressed
if feed_button_pressed or event_number == 300:
check = 8
event_number = 300
window.after(100, update, cycle, check, event_number, x, y)
feed_button_pressed = False
elif button_pressed and pooped_times < 5:
check = 6
event_number = 100
window.after(100, update, cycle, check, event_number, x, y)
button_pressed = False
elif button_pressed and pooped_times == 5:
check = 7
event_number = 200
window.after(100, update, cycle, check, event_number, x, y)
button_pressed = False
pooped_times = 0
elif event_number in idle_num:
check = 0
print('idle')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number == 4:
check = 1
print('from idle to sleep')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number in walk_left:
check = 4
print('walking towards left')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number in walk_right:
check = 5
print('walking towards right')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number in sleep_num:
check = 2
print('sleep')
window.after(1000, update, cycle, check, event_number, x, y)
elif event_number == 15:
check = 3
print('from sleep to idle')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number == 100:
check = 6
print('button press poop')
window.after(100, update, cycle, check, event_number, x, y)
elif event_number == 200:
check = 7
print('HELICOPTER')
window.after(100, update, cycle, check, event_number, x, y)
def gif_work(cycle, frames, event_number, first_num, last_num):
if cycle < len(frames) - 1:
cycle += 1
else:
cycle = 0
event_number = random.randrange(first_num, last_num + 1, 1)
return cycle, event_number
def update(cycle, check, event_number, x, y):
if check == 0:
frame = idle[cycle]
cycle, event_number = gif_work(cycle, idle, event_number, 5, 11) # if idle, go to walking next
elif check == 1:
frame = idle_to_sleep[cycle]
cycle, event_number = gif_work(cycle, idle_to_sleep, event_number, 13, 13) #go to sleep
elif check == 2:
frame = sleep[cycle]
cycle, event_number = gif_work(cycle, sleep, event_number, 14, 15) # if currently sleeping, sleep or wake
elif check == 3:
frame = sleep_to_idle[cycle]
cycle, event_number = gif_work(cycle, sleep_to_idle, event_number, 1, 1) # if wake, idle next
elif check == 4:
frame = walk_positive[cycle]
cycle, event_number = gif_work(cycle, walk_positive, event_number, 3, 12) #go to idle or walk more
x -= 3
elif check == 5:
frame = walk_negative[cycle]
cycle, event_number = gif_work(cycle, walk_negative, event_number, 3,12)#go to idle or walk more
x += 3
elif check == 6:
frame = poop[cycle]
cycle, event_number = gif_work(cycle, poop, event_number, 1,12)#go to idle or walk more
elif check == 7:
frame = helicopter[cycle]
if 20 < cycle < 67:
x += 20
y -= 15
elif cycle >= 67:
x -= 20
y += 15
cycle, event_number = gif_work(cycle, helicopter, event_number, 1,12)#go to idle or walk more
elif check == 8:
frame = feeding[cycle]
cycle, event_number = gif_work(cycle, feeding, event_number, 3,12)#feeding
# Ensure the window stays within the screen boundaries
x = max(0, min(x, screen_width - 100))
window.geometry('100x100+' + str(x) + '+'+ str(y))
label.configure(image=frame)
window.after(1, event, cycle, check, event_number, x, y)
# load in gifs
idle = [tk.PhotoImage(file=impath + 'idle.gif', format='gif -index %i' % i) for i in range(6)]
idle_to_sleep = [tk.PhotoImage(file=impath + 'sleep.gif', format='gif -index %i' % i) for i in range(7)]
sleep = [tk.PhotoImage(file=impath + 'sleeping.gif', format='gif -index %i' % i) for i in range(6)]
sleep_to_idle = [tk.PhotoImage(file=impath + 'wake.gif', format='gif -index %i' % i) for i in range(7)]
walk_positive = [tk.PhotoImage(file=impath + 'walkingleft.gif', format='gif -index %i' % i) for i in range(8)]
walk_negative = [tk.PhotoImage(file=impath + 'walkingright.gif', format='gif -index %i' % i) for i in range(8)]
poop = [tk.PhotoImage(file=impath + 'pooping.gif', format='gif -index %i' % i) for i in range(8)]
helicopter_1 = unpack_gif(src=impath + 'helicopter_1.gif')
helicopter_fast = unpack_gif(src=impath + 'helicopter_fast.gif')
helicopter_fastest = unpack_gif(src=impath + 'helicopter_fastest.gif')
tornado = unpack_gif(src=impath + 'TORNADO.gif')
feeding = [tk.PhotoImage(file=impath + 'feeding.gif', format='gif -index %i' % i) for i in range(23)]
helicopter = [] + helicopter_1 + helicopter_fast + helicopter_fastest + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado
helicopter = helicopter + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado + tornado
print(len(helicopter))
# Window configuration
window.config(highlightbackground='#5a4e44')
# Set background color and remove border
window.configure(background='#5a4e44', highlightthickness=0)
label = tk.Label(window, bd=0, bg='#5a4e44')
window.overrideredirect(True)
#draws on top
window.attributes('-topmost', True)
label.bind("<Button-1>", label_click)
label.pack()
# Create a new Toplevel window for the progress bar
progress_window = tk.Toplevel(window)
progress_window.title("Progress Bar Window")
progress_window.attributes('-topmost', True)
progress_window.geometry('30x120+' + str(0) + '+'+ str(y))
# Create an image for the feed button
feed_image = Image.open('feed_image.png') # Replace 'feed_image.png' with the actual image file
feed_image = feed_image.resize((30, 30), Image.LANCZOS) # Resize the image
feed_photo = ImageTk.PhotoImage(feed_image)
# Create a feed button with the feed image
feed_button = tk.Button(progress_window, image=feed_photo, command=feed_button_click, bd=0, bg='black')
feed_button.pack(side=tk.TOP, pady=0)
feed_button.image = feed_photo
# Create a label for the progress bar
progress_label = ttk.Label(progress_window)
progress_label.pack(side=tk.LEFT, pady=0)
# Create a style for the progress bar (pixel art style with light and dark purple colors)
style = ttk.Style()
style.theme_use('default')
style.configure("TProgressbar", thickness=10, troughcolor="#6a5acd", bordercolor="#800080", background="#9370db")
# Create a progress bar
style = ttk.Style()
style.layout("TVertical.TProgressbar",
[('Vertical.Progressbar.trough',
{'children': [('Vertical.Progressbar.pbar',
{'side': 'top', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Vertical.Progressbar.label', {'sticky': ''})])
progress_bar = ttk.Progressbar(progress_window, variable=progress_value, length=screen_height, mode='determinate', orient='vertical', style="TVertical.TProgressbar")
progress_bar.pack(side=tk.TOP, pady=0)
# Start the decrementing process
window.after(1, decrement_progress)
# Loop the program
window.after(1, update, cycle, check, event_number, x, y)
window.mainloop()