tkinter画布滚动滞后与图像

时间:2019-03-05 13:42:36

标签: python tkinter

我正在用tkinter创建一个platofromer,我将player置于“相机”(canvas view)的中心,并且当用户按下Arrow left或{{1 }} 玩家移动其脚架,然后摄像机跟随他。 如果没有背景图像,效果很好,但是当我添加背景图像(3600x1070)并按Arrow right时,游戏开始出现延迟。滞后是由Arrow left/right引起的,它使相机跟随播放器,但我不知道如何解决。这是一个显示我的问题的可执行示例

canvas scroll

如果您按向右箭头来模拟玩家的移动,您会看到粒子如何在下落时开始减速,您越靠近图像中心,它们的速度就越慢。

如果您删除from tkinter import * from random import * from PIL import Image, ImageTk ARROW_LEFT = 37 ARROW_RIGHT = 39 master = Tk() key_list = [] ws = master.winfo_screenwidth() hs = master.winfo_screenheight() x = (ws / 2) - (700 // 2) y = (hs / 2) - (700 // 2) master.geometry('%dx%d+%d+%d' % (700 + 4, 700 + 4, x, y)) master.title("My game") canvas = Canvas(master, width=2000, height=2000, bg="black") canvas.configure(scrollregion=(0, 0, 5000, 5000), yscrollincrement='1', xscrollincrement='1') canvas.grid(row=0) class Background: def __init__(self, canvas=None, background_path="", game_window=None): self.game_window = game_window self.canvas = canvas self.image = Image.open(background_path) self.img_copy = self.image.copy() self.image = self.img_copy.resize((ws * 2, hs)) self.background_image = ImageTk.PhotoImage(self.image) x = (2000//2) - ws//2 y = (2000/2) - hs self.background = self.canvas.create_image(x, y, image=self.background_image, tag='background', anchor=NW) class Rain: def __init__(self, master): self.canvas = canvas self.master = master self.lenght = randint(10, 20) self.rain_speed = randint(5, 30) self.particel_speed = randint(5, 50) def blob(self): x1 = randint(canvas.canvasx(0), canvas.canvasx(ws)) y1 = 0 colors = ["red", "yellow", "blue", "green", "purple"] index = randint(0, len(colors) - 1) line = self.canvas.create_line(x1, y1, x1, y1 + self.lenght, fill=colors[index], width=2, tag="rain") self.move(line, self.canvas.coords(line)) def move(self, line, coords): line_x1 = round(coords[0]) line_y1 = round(coords[1]) line_x2 = round(coords[2]) line_y2 = round(coords[3]) if line_y2 > canvas.canvasy(hs) or line_x1 < canvas.canvasx(0) or line_x1 > canvas.canvasx(ws): self.canvas.delete(line) self.blob() return else: self.canvas.coords(line, coords[0], coords[1] + 5, coords[2], coords[3] + 5) coords = self.canvas.coords(line) self.master.after(self.rain_speed, lambda: self.move(line, coords)) def rain(): for _ in range(20): Rain(master).blob() def scroll_screen_left(canvas): canvas.xview_scroll(-3, UNITS) def scroll_screen_right(canvas): canvas.xview_scroll(3, UNITS) def key_listner(event): event = event.keycode if event not in key_list: key_list.append(event) def key_organizator(): if len(key_list) > -1: combo = [ {ARROW_LEFT}, {ARROW_RIGHT}, ] if set(key_list) in combo: pass else: for key in key_list: if {key} in combo: key_list.clear() key_list.append(key) if set(key_list) in combo: if ARROW_LEFT in key_list: scroll_screen_left(canvas) # lag elif ARROW_RIGHT in key_list: scroll_screen_right(canvas) # lag master.after(10, lambda: key_organizator()) def clear_key(event): event = event.keycode if event in key_list: key_list.remove(event) BACKGROUND_1_LAYER_PATH = "assets/background/background_1.png" layer1_background = Background(canvas=canvas, background_path=BACKGROUND_1_LAYER_PATH) key_organizator() rain() master.bind("<KeyRelease>", clear_key) master.bind("<KeyPress>", key_listner) master.mainloop() 行 您可以在没有背景图片的情况下看到它的工作原理(微粒移动得更快)

0 个答案:

没有答案