The full code import pyautogui import random import tkinter as tk x = 1400 cycle = 0 check = 1 idle_num = sleep_num = walk_left = walk_right = event_number = random.randrange(1,3,1) impath = 'C:\\Users\\fx770\\Desktop\\Project\\Buddy\\image\\' #transfer random no. And once it changed to sleeping, it must perform a sleep action to prevent instant waking up which is not sound natural.Īnd just like a normal cat, it had a very high chance to keep sleeping and sleeping and sleeping. gif loop once, the ‘event_number‘ will be randomly change in 1 to 9, so it had a probability of 4/9 to keep idling or walking toward left and right, and 1/9 chance to change from idle to sleeping.
We could see that we have 15 numbers, at the start of the program, our pet must be idling because we assigned variable ‘check’ is 1, after the. In the variable we have assigned at the beginning: check = 1 idle_num =#5 is idle to sleep sleep_num = #14 is sleep to idle,sorry for skipping walk_left = walk_right = If you have pay attention to the above code, you should notice that after our cat change from idle to sleep, the next action must be sleep, same as after changing from sleep to idle, it must be idling, idle_num = #sleep to idle elif check =3: frame = sleep_to_idle cycle ,event_number = gif_work(cycle,sleep_to_idle,event_number,1,1)#must be idle
It mean that the program will call the update function with the following assignment after 1000 ms, which is 1s and we could control the speed of the animation by changing it. If any of you is confused in window.after(1000,update,cycle,check,event_number,x) 10,11,12,13,15 = sleep elif event_number = 14: check = 3 print('from sleep to idle') window.after(100,update,cycle,check,event_number,x) #no. 6,7 = walk towards left elif event_number in walk_right: check = 5 print('walking towards right') window.after(100,update,cycle,check,event_number,x) #no 8,9 = walk towards right elif event_number in sleep_num: check = 2 print('sleep') window.after(1000,update,cycle,check,event_number,x) #no. 5 = idle to sleep elif event_number in walk_left: check = 4 print('walking towards left') window.after(100,update,cycle,check,event_number,x) #no.
1,2,3,4 = idle elif event_number = 5: check = 1 print('from idle to sleep') window.after(100,update,cycle,check,event_number,x) #no. to event def event(cycle,check,event_number,x): if event_number in idle_num: check = 0 print('idle') window.after(400,update,cycle,check,event_number,x) #no. To make the event change, we need a function to define when and the probability to do an action : idle_num = sleep_num = walk_left = walk_right = #transfer random no. Is the location of our tkinter window aka the position of our pet, ‘100x100’ is the size of our pet in pixel, ‘x’ is the x position in our screen, and ‘1050’ is the floor our pet stepping on.(it change with the resolution of your screen) We need our pet to do an action with assigning a number to every action: def update(cycle,check,event_number,x): #idle if check =0: frame = idle cycle ,event_number = gif_work(cycle,idle,event_number,1,9) #idle to sleep elif check =1: frame = idle_to_sleep cycle ,event_number = gif_work(cycle,idle_to_sleep,event_number,10,10) #sleep elif check = 2: frame = sleep cycle ,event_number = gif_work(cycle,sleep,event_number,10,15) #sleep to idle elif check =3: frame = sleep_to_idle cycle ,event_number = gif_work(cycle,sleep_to_idle,event_number,1,1) #walk toward left elif check = 4: frame = walk_positive cycle, event_number = gif_work(cycle,walk_positive,event_number,1,9) x -= 3 #walk towards right elif check = 5: frame = walk_negative cycle, event_number = gif_work(cycle,walk_negative,event_number,1,9) x -= -3 window.geometry('100x100+'+str(x)+'+1050') nfigure(image=frame) window.after(1,event,cycle,check,event_number,x) To make our pet background from black to transparent, we need a few lines of code: nfig(highlightbackground='black') window.overrideredirect(True) window.wm_attributes('-transparentcolor','black')Īs I want her to be movable and showing animation, I assign her as a label: label = tk.Label(window,bd=0,bg='black') label.pack() window.mainloop()Īnd that’s the perpetration we need to make our program works. gif to an array idle = #idle gif, 5 frames idle_to_sleep = #idle to sleep gif, 8 frames sleep = #sleep gif, 3 frames sleep_to_idle = #sleep to idle gif, 8 frames walk_positive = #walk to left gif, 8 frames walk_negative = #walk to right gif, 8 frames gif, we have to call it from local file to our python program: #call buddy's action.