如何移动一组 space 中的 tkinter 行列表?

How can I move a list of tkinter lines in a set space?


    import time
    from tkinter import *
    
    class Template:
        def __init__(self):
            self.window = Tk()
            self.window.title("2D Display")
    
            self.canvas = self.canvas_display()
            self.line1 = self.line_creation(650,350,500 * .3, 1000)
            self.line3 = self.line_movement_creation(0, 350,2000, 350)
            self.horizon = self.canvas.create_line(0,350,2000, 350, width = 2, fill ="white")
            self.speedx = 0 # x movement of line3
            self.speedy = 9 # y movement of line3
            self.active = True
            self.pos1 = []
    
            self.move_active()  #Code that creates the problem
            self.canvas.update()
    
        def canvas_display(self):  #canvas
            canvas = Canvas(self.window, width=500, height=400, background='black')
            canvas.pack(expand=True, fill="both")
            canvas.update()
            return canvas

向上是初始化

        def line_creation(self,x,y,x1,y1): #creation of multple lines
            spacing = 0
            lines = []  # could list([])
            for i in range(11):
                id = self.canvas.create_line( x, y, x1 + spacing, y1, width=2, fill="white")
                lines.append(id)
                spacing += 100
                pos1 = self.canvas.coords(id)
                self.pos1 = pos1
                print(self.pos1)
    
            return lines
    

这是顶点线的创建方法

        def line_movement_creation(self,x,y,x1,y1):
            spacing1 = 0
            lines = []
            for i in range(4):
                id = self.canvas.create_line(x , y+spacing1, x1, y1 + spacing1, width=2, fill="white")
                lines.append(id)
                spacing1 += 100
                #line = [] equal all horizontal and vertical, 12 - 15 equal horizontal moving lines
            return lines

这是为水平线创建的

        def line_update(self): #line movement method
            for line in self.line3:
                self.canvas.move(line, self.speedx, self.speedy)
                #Create variables for all x and y values for the lines
                pos = self.canvas.coords(line)
                print(pos)
                if pos[3] >= 800:
                    self.canvas.move(line, self.speedx, self.speedy - 460)
    
    
        def move_active(self):
            if self.active:
                self.line_update()
                self.window.after(40, self.move_active)

这就是让线条产生运动错觉的原因。我想获取水平线列表并将它们设置在最外面的垂直线之间。所以它会停留在垂直线之间。创建像图像一样的道路。我想我需要为两者单独列出一份清单,但我不确定。所以为了澄清,有人可以帮助我展示如何使水平线不连接到屏幕的末端而是连接到水平线内部 Code Demonstration

        def run(self):
            self.window.mainloop()
    
    if __name__ == '__main__':
        Temp = Template()
        Temp.run()

所以,首先我建议你使用像 pygame 这样的游戏引擎,因为它更快并且提供了更多的选项和其他东西,但这里是 tkinter(基本上它是一些简单的三角函数):

import tkinter as tk
import math


class Canvas(tk.Canvas):
    def __init__(self, parent, width=700, height=500, **kwargs):
        super().__init__(parent, width=width, height=height, **kwargs)
        self.width = width
        self.height = height
        self.angle = 70
        self.speedy = 10
        self.change_speed_ms = 50
        self.draw_angle(self.angle, 5)
        self.lines, self.interval = self.init_lines(amount=5)
        self.draw_lines()

    @property
    def radians(self):
        return math.radians(self.angle)

    def draw_angle(self, view_angle, lines):
        orient = 0
        adjacent = self.height // 2
        step = view_angle // lines
        half = view_angle // 2
        for angle in range(orient - half, orient + half + step, step):
            rad = math.radians(angle)
            delta = math.tan(rad) * adjacent
            x1, y1 = self.width // 2, self.height // 2
            x2, y2 = x1 + delta, y1 + adjacent
            self.create_line(x1, y1, x2, y2)

    def init_lines(self, amount=5):
        interval = round((self.height // 2) / amount)
        coordinate_list = list()
        offset = self.height // 2
        for y in range(0, self.height // 2 + interval, interval):
            delta = math.tan(self.radians / 2) * y
            x1 = self.width // 2 - delta
            x2 = self.width // 2 + delta
            y1 = y2 = y + offset
            line = self.create_line(x1, y1, x2, y2)
            coordinate_list.append((line, (x1, y1, x2, y2)))
        return coordinate_list, interval

    def draw_lines(self):
        tmp_lst = list()
        for id_, (x1, y1, x2, y2) in self.lines:
            y1 += self.speedy
            if y1 > self.height + self.interval - self.speedy:
                y1 = self.height // 2
            y = y2 = y1
            adjacent = y - self.height // 2
            delta = math.tan(self.radians / 2) * adjacent
            x1 = self.width // 2 - delta
            x2 = self.width // 2 + delta
            self.coords(id_, x1, y1, x2, y2)
            tmp_lst.append((id_, (x1, y1, x2, y2)))
        self.lines = tmp_lst
        self.after(self.change_speed_ms, self.draw_lines)


root = tk.Tk()

Canvas(root, highlightthickness=0).pack()

root.mainloop()

所以这里的主要方法是Canvas().draw_lines()。首先,根据总行数以设定的间隔获取行 ID 及其坐标的列表,然后遍历它们,更改它们的 y 值并相应地计算直角三角形的对边使用tan和从当前y坐标和起点(中间)已知的相邻边。