Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tkinter collision detection
#1
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()
Yoriz wrote May-22-2020, 02:47 PM:
Please post all code, output and errors (in its entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
Please keep your code inside the tags, not outside it
pyzyx3qwerty
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Quote
#3
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?
Quote
#4
Im just very much into tkinter and that stuff right now actually
Do have a clue about why its not working?
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)