在python Tkinter中是否有可能知道特定时间点的线的坐标

Is it possible to know the coordinates of a line at a specific point in time in python Tkinter

这是我制作的游戏我希望生成的圆圈在不实际触及线的情况下移动,圆圈在没有按键时向右和向下移动,并且应该在按下空格键,当它与线(我为 hitreg 制作的逻辑)发生碰撞时,它将以全新的线和起始位置的圆圈重新开始。当它从一端移动到另一端时,它将以一条具有更多分区的新线重新开始,因此随着您在关卡中前进,游戏会变得困难。我现在遇到的唯一问题是,如果不立即退出代码,我将无法正确启动游戏,我认为原因可能是我计算的坐标不够准确,有时圆会越过线并且仍然没有发生碰撞。

游戏代码

from tkinter import *
from random import randint as rand
import keyboard, math

window = Tk()
window.geometry("1440x720")
canvas = Canvas(window, width=1440, height=720, 
bg="white")

l1, l2, l3 = [], [], []
x = 1
b = canvas.create_oval(0, 300, 10, 300 + 60)
c = b

def genLine():
    global x
    f = 0
    z = 360
    y = 1440 / x
    while x != 0:
        ran = rand(300, 420)
        u = canvas.create_line(f, z, y, ran)
        r = canvas.coords(u)
        l1.append(r)
        f = y
        x -= 1
        y += y
        z = ran

def hitReg():
    global l2, l3
    for i in l1:
        grad = ((i[3] - i[1]) / (i[2] - i[0]))
        l2.append(grad)
    for i in l2:
        for f in l1:
            x = 0
            length = f[2] - f[0]
            while x != length:
                point = x * i + f[1]
                l3.append(math.ceil( point))
                x += 0.25

def move_b():
    global l3, x
    canvas.move(c, 1, 1)
    y = canvas.coords(c)
    if keyboard.is_pressed('space'):
        canvas.move(b, 1, -5)
    elif y[1] in l3 or y[3] in l3:
        exit()
    elif y[2] >= 1440:
        x += 1
        genLine()
        hitReg()
        move_b()
    else:
        pass
    window.after(10, move_b)

genLine()
hitReg()
move_b()

canvas.pack()
window.mainloop()

主要问题是

elif y[1] in l3 or y[3] in l3:

因为 l3 具有在线的所有 y 值,它会将 y[1]y[3] 与所有 y 值进行比较,但它应该只进行比较最近的 - 与 x 相同的点作为 oval.

的中心

我正在考虑使用 canvas.find_overlapping()canvas.find_closest() 但它需要为线上的每个生命点创建小 rectange 或小 oval

最后,我每 1px 而不是 0.25px 创建生命值,这样我就可以将它们放在列表中并轻松获得

point_y = points[ int(oval_center_x) ]

稍后检查 oval_y1 < point_y < oval_y2

我也用 window.bind("<space>", function) 移动 oval 所以我不需要额外的模块 keyboard。此外,我在 Linux 上使用 Linux 和 keyboard 需要 admin 特权。

我还更改了变量和函数的名称,使它们更具可读性。

至于 exit() 我创建了函数 reset() 设置 level,删除所有行 canvas.delete(*lines),将 oval 移动到开始 canvas.moveto(oval, 0, 330) ,并生成新行(使用值 level)并生成 hit points。使用 reset(level+1) 我可以生成下一关,使用 reset(1) 我可以回到第一关。

import tkinter as tk        # PEP8: `import *` is not preferred
from random import randint  # PEP8: every module in separted line

# --- functions ---  # PEP8: all functions directly after imports
                     # PEP8: `lower_case_names` for functions and variables

def generate_lines(level):
    
    regions = []
    lines = []
        
    x1 = 0
    y1 = 360
    
    step = 1440 / level 
    
    x2 = step
    
    for i in range(level):
        y2 = randint(300, 420)  # 360-60, 360+60
        
        region = (x1, y1, x2, y2)
        regions.append(region)

        line = canvas.create_line(x1, y1, x2, y2)
        lines.append(line)
        
        x1 = x2
        y1 = y2

        x2 += step

    return lines, regions
    
def generate_hit_points(regions):
    
    points = []

    for x1,y1,x2,y2 in regions:
        
        steps = int(x2 - x1)
        dy = (y2 - y1) / steps
        
        y = y1
        
        for _ in range(steps):   # cant use `range(y1, y2, dy)` because `dy` is `float`, not `int`

            points.append(y)  # I don't need `x`
            
            y += dy

    return points
    
def move_oval():

    canvas.move(oval, 1, 1)
    
    x1, y1, x2, y2 = canvas.coords(oval)
    
    oval_center_x = int(x1+10)
    point_y = points[oval_center_x]
    #print(y1, '<', int(point_y), '<', y2, '?')

    #if y1 >= point_y or point_y >= y2:  # less readable
    if not (y1 < point_y < y2):          # more readable
        print('end')
        reset(1)
        
    elif x2 >= 1440:
        print('next level')
        reset(level+1)
        
    window.after(25, move_oval)

def on_press_space(event):
    canvas.move(oval, 0, -15)

def reset(new_level):
    # all globals moved to one function
    global lines
    global points
    global level
    
    level = new_level
    
    canvas.delete(*lines)        # remove all lines
    canvas.moveto(oval, 0, 330)  # move oval to the beginning
    
    lines, regions = generate_lines(level)
    points = generate_hit_points(regions)  

# --- main ---

lines  = []  # PEP8: every variable in separted line
points = []  # PEP8: every variable in separted line

level = 1

window = tk.Tk()

window.geometry("1440x720")

canvas = tk.Canvas(window, width=1440, height=720, bg="white")
canvas.pack()

oval = canvas.create_oval(0, 360-30, 10, 360+30)

window.bind('<space>', on_press_space)
window.bind('q', lambda event:window.destroy())

reset(1)

# start moving
move_oval()

window.mainloop()

PEP 8 -- Style Guide for Python Code