Mar-09-2019, 09:56 PM
I'm trying to create a platformer with tkinter,
i've created the character and now i'm trying to create a dynamic background with a biomes system so when the player passes from a biome to another the background changes the image.
The problem is that when the player changes biome the game call a function that modify the background image and while the image is changing huge lag starts in all the game elements
(it took a few seconds for change the image), i thought to use threading but don't know how to implement it for solve my issue. In the example the is not so much lag becouse the background image is only 1 but in my game i have 3 background images that are changed at the same time and the lag increase much. There are simpliest way for do this or something that i can do for solve it?
i've created the character and now i'm trying to create a dynamic background with a biomes system so when the player passes from a biome to another the background changes the image.
from tkinter import * from PIL import Image, ImageTk from random import * current_image = None path_img_1 = "assets/background/notte.png" # images path path_img_2 = "assets/background/Hills Layer 01.png" def move_oval(oval, canvas, master, increase=2): # move the ball x1, y1, x2, y2 = canvas.coords(oval) if x1 <= 0: increase = 2 elif x2 >= canvas_width: increase = -2 x1 += increase x2 += increase canvas.coords(oval, x1, y1, x2, y2) master.after(10, lambda: move_oval(oval, canvas, master, increase=increase)) def resize_image(width, height, path_image): # create image from the paths and resize them image = Image.open(path_image) img_copy = image.copy() image = img_copy.resize((width, height)) return image def change_image(to_configure, img_to_change, new_image,master, canvas, alpha=0.0, image=None): # chenge gradually two images global current_image if alpha > 1: # if alpha is > 1 the the cycle stops current_image = image return else: new_img = Image.blend(img_to_change, new_image, alpha) # it modify a bit the image ( depends to the alpha value ) modifying_image = ImageTk.PhotoImage(new_img) # create a Photoimage from the modified image image = modifying_image canvas.itemconfigure(to_configure, image=modifying_image) # modify the canvas object with the new Photoimage canvas.update() # update the canvas master.after(50, lambda: change_image(to_configure, img_to_change, new_image, master, canvas,alpha=alpha + 0.2, image=image)) # call again the cycle with alpha increased by 0.2 master = Tk() monitor_width = master.winfo_screenwidth() monitor_height = master.winfo_screenheight() canvas_width = monitor_width canvas_height = monitor_height x = (monitor_width / 2) - (canvas_width // 2) y = (monitor_height / 2) - (canvas_height // 2) master.geometry('%dx%d+%d+%d' % (canvas_width + 4, canvas_height + 4, x, y)) master.title("My game") canvas = Canvas(master, width=canvas_width, height=canvas_height, bg="black") canvas.place(x=0) img_1 = resize_image(canvas_width, canvas_height, path_img_1) # resize the images img_2 = resize_image(canvas_width, canvas_height, path_img_2) img = ImageTk.PhotoImage(img_1) image_keeper = img canvas_image = canvas.create_image(0, 0, image=img, anchor=NW) oval = canvas.create_oval(350, 340, 370, 360, fill="red") move_oval(oval, canvas, master) button = Button(canvas, text="change image", command=lambda: change_image(to_configure=canvas_image, img_to_change=img_1, new_image=img_2, master=master, canvas=canvas, alpha=0.0, image=None)) button.place(x=0) master.mainloop()here is an executable example for my issue.
The problem is that when the player changes biome the game call a function that modify the background image and while the image is changing huge lag starts in all the game elements
(it took a few seconds for change the image), i thought to use threading but don't know how to implement it for solve my issue. In the example the is not so much lag becouse the background image is only 1 but in my game i have 3 background images that are changed at the same time and the lag increase much. There are simpliest way for do this or something that i can do for solve it?