# -*- coding: utf-8 -*- # Autor: Thomas Lechner from turtle import * from datetime import datetime screen = Screen() screen.setup(800, 400) screen.clear() screen.register_shape("car_1.gif") global cars, traffic cars = [] traffic = True def street(length, width): global t tracer(False) a=8 s = Turtle() s.ht() s.pensize(8) s.pu();s.goto(-length/2,-width/2);s.pd() s.fd(length) s.pu();s.goto(s.xcor(),s.ycor()+width/2);s.pd() for i in range(a): s.bk(length/(2*a)-4) s.pu() s.bk(length/(2*a)+4) s.pd() s.pu();s.goto(s.xcor(),s.ycor()+width/2);s.pd() s.fd(length) t = Turtle(shape="circle") t.pu() t.goto(-250,120) tracer(True) def car_start(): tracer(False) car_start = Turtle(shape="car_1.gif") car_start.pu() car_start.goto(500,50) tracer(True) return car_start def car(x,y): c = car_start().clone() cars.append(c) print(cars) def space_check(car): global space dis = [] a = 0 for i in cars: if car != i and car.xcor() > i.xcor(): dis.append(car.distance(i.xcor(),i.ycor())) print(dis) for i in dis: if i < 200: a = a+1 if a != 0: space=False print(space) else: space=True return space def trafficlight(x,y): global traffic if traffic != True: traffic=True t.color("green") else: traffic=False t.color("red") def move(i): space_check(i) print(space) if cars.index(i) == 0: i.bk(2) elif space == True: i.bk(2) def tick(): screen.tracer(False) for i in cars: if traffic == True: move(i) elif i.xcor() < -200 or i.xcor() > -180: move(i) if i.xcor() < -500: i.ht() cars.remove(i) screen.tracer(True) screen.ontimer(tick, 2) street(800,200) if __name__ == "__main__": listen() onscreenclick(car, 1) onscreenclick(trafficlight,3) tick() mainloop()