0

hello guys im stuck for quite a while with this problem and i badly need ur help.
in fact i wanna make the red rectangle slow down when it comes close to the blue one. it would be nice if they both move at the same speed. ive tried several different techniques but has failed so far.
any help plz...

import Tkinter as tk
import time
import sys
import datetime as dt 
from Tkinter import *



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

canvas.pack()

# create car

car1=canvas.create_rectangle(10,170,30,180, outline='blue', fill='blue')
car2=canvas.create_rectangle(-30,170,-10,180, outline='red', fill='red')

def minus(self, a,b='unary'):
    if b=='unary':
        return map(lambda x:-x,a)
    else:
        return map(lambda x,y:x-y,a,b)


# move car
while True:
    time.sleep(0.025)
    canvas.move(car1, 2, 0)
    canvas.move(car2, x, 0)
    a=canvas.coords(car1)
    b=canvas.coords(car2)
    print minus(a,b)
    if (minus(a,b)) ==['0.0', '-170.0', '-20.0', '-180.0']:
        x=1
    canvas.update()

root.mainloop()
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by harrykokil
0

You are importing Tkinter twice, first with a namespace and then without, kind of goofy. This "Traffic Simulation" must be homework, because I have seen this import approach a fair number of times on several forums.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.