'Is it possible to know the coordinates of a line at a specific point in time in python Tkinter
This is the game that I have made I want the circle that has been generated to move across the line without actually touching it, the circle moves to the right and downwards when no keypresses are made and is supposed to jump when the spacebar is pressed, when it collides with the line (logic I made for hitreg) it will restart with a brand new line and the circle at the starting position. When it goes from one end to the other it'll restart with a new line that has more divisions so that the game is difficult as you progress through the levels. The only issue I am having right now is that I am not able to start up the game correctly without it exiting the code immediately and I think the reason might be that the coordinates I have calculated are not accurate enough and sometimes the circle crosses the line and still doesn't register a collision.
Code for the game
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
