harrykokil 0 Light Poster

hi everyone,
i urgently need your help for my project.
I need to implement collision detection in my program. I have been trying for a month but so far I haven't found any good solution. I'm providing my piece of code hoping you guys can help me out. Thanks

import Tkinter as tk
    import time

    root=tk.Tk()
    root.title("Traffic Simulation")
    canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF")

    canvas.pack()

    class Car(object):
      def __init__(self, a,b,c,d, outline='blue', fill='blue'):
        self.rect = canvas.create_oval(a,b,c,d, outline=outline, fill=fill)
        self.red1=canvas.coords(canvas.create_line(195,106,195,118, fill= "red"))
       
        self.xspeed = 2
     
        self.yspeed = 0
        self.rear_bumper = a
        self.front_bumper = c
       

     
       
      def set_speed(self, xspeed, yspeed):
        self.speed = xspeed, yspeed
     
      def move(self):
        canvas.move(self.rect, self.xspeed, self.yspeed)

        vehicle=canvas.coords(self.rect)
       
       
        if  vehicle < self.red1:
                #print "jo"
                self.xspeed=2           

        elif vehicle > self.red1:                   
                self.xspeed=0
               
               
        canvas.move(self.rect, self.xspeed, self.yspeed)   
     
    # create cars

    car1 = Car(-30, 154, -20, 162, outline='blue', fill='blue')
    #car2 = Car(-60, 154, -50, 162, outline='blue', fill='blue')
    car2 = Car(-120, 154, -110, 162, outline='blue', fill='blue')
    #car4 = Car(-120, 154, -110, 162, outline='blue', fill='blue')



    car1_rear_bumper = 150 #from starting coords
    car2_front_bumper = -10 #from starting coords

    car1_speed = 2
    car2_speed = 4
    car3_speed = 2
    car4_speed = 4


    vehicle=[car1, car2, car3, car4]



    # move car
    while True:
        i=0
        car1v=canvas.coords(vehicle[i])
        #red1v=canvas.coords(red1)
        vehicle[i].move()
        vehicle[i+1].move()
        #vehicle[i+2].move()
        #vehicle[i+3].move()
        time.sleep(0.025)
           
        car1.rear_bumper += car1.xspeed
        car2.front_bumper += car2.xspeed
        car1_rear_bumper += car1_speed
        car2_front_bumper += car2_speed

        diff_between_cars = car1.rear_bumper - car2.front_bumper
        print diff_between_cars
       

        if 50 <= diff_between_cars < 60:
            car2.xspeed=1  #decelerate
        elif 10 <= diff_between_cars < 50:
            car2.move()  #match speed of front car
        elif diff_between_cars < 10:
            car2.move()
        #print red1
        #print car1v
       

        #if red1v < car1.rear_bumper:
          #vehicle[i].move(0)
          #canvas.after(2000, a)

        canvas.update()

    root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.