Python Forum
Tkinter collision detection - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Tkinter collision detection (/thread-27015.html)



Tkinter collision detection - yeyeee - May-22-2020

Im trying to get this program to work, though nothing is really happening, I am also making a collision detection, please help
import parabola
from tkinter import *
import tkinter as tk
from math import sqrt


def start_bullet():
    """set up the projectile, and starts theupdate_bullet thread"""
    global iVelocity
    global iAngle
    iVelocity = int(entry_velocity.get())
    iAngle = int(entry_angle.get())
    parabola.setup_throw(iVelocity, iAngle)
    canvas.after(16, update_bullet)
    
    
def update_bullet():
    """increases time and draws a new frame.
    Ends the thread if the projectile hits the ground"""

    global iTime
    iTime+=iTimeInc
    parabola.set_time(iTime)
 
    x = xPos+parabola.get_x()
    y = yPos-parabola.get_y()
    
    canvas.coords(bullet, x-r, y-r, x+r, y+r)
    
   
    if(y>=500):
        return
    else:
        canvas.after(16,update_bullet)
        

window = tk.Tk()
canvas = Canvas(window, width=800,height=600, bg="black")
canvas.pack()

# Variabler
button1 = tk.Button(window,text="Fire",command=start_bullet)
button1.pack()

entry_velocity = Entry(window, width = 10)
entry_velocity.pack()
label_1 = Label(window,text ="Velocity")
label_1.pack()

entry_angle = Entry(window, width = 10,)
entry_angle.pack()
label_2 = Label(window,text = "Angle")
label_2.pack()

xPos = 100
yPos = 500
r=5 

xPos2 = 700 
yPos2 = 500
r2 = 10 

iTime = 0 
iTimeInc = 0.02 


ground = canvas.create_line(0,yPos,800,yPos, fill="green")
bullet = canvas.create_oval(xPos-r,yPos-r,xPos+r,yPos+r, fill="white")
target = canvas.create_oval(xPos2-r2,yPos2-r2,xPos2+r2,yPos2+r2, fill="red")

def getMidpoint(c):
    """returns midpoint, given coords (x1,y1,x2,y2)"""
    if(len(c)==2):
        return c
    else:
        dx = (c[0]+c[2])/2
        dy = (c[1]+c[3])/2
        return (dx,dy)
    
def getDistance(bullet,target):
    """returns distance, given c1 and c2"""
    dx = target[0]-bullet[0]
    dy = target[1]-bullet[1]
    return sqrt(dx**2 +dy**2)

    if(getDistance(bullet,target)<= r+r2):
        canvas.config(bg="red")
        

canvas.after(2000,start_bullet)

window.mainloop()



RE: Tkinter collision detection - pyzyx3qwerty - May-22-2020

Please keep your code inside the tags, not outside it


RE: Tkinter collision detection - Larz60+ - May-23-2020

If you are just doing this for fun, by all means continue.
If not, curious why you didn't choose pygame which has collision detection built in?


RE: Tkinter collision detection - yeyeee - May-23-2020

Im just very much into tkinter and that stuff right now actually
Do have a clue about why its not working?