Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CARS main.py
- from time import sleep
- from config import *
- import os
- class Road():
- def __init__(self, road_id, length, width, cars, lighter):
- self.id = road_id
- self.length = length
- self.width = width
- self.cars = cars
- self.lighter = lighter
- def print_road(self):
- self.lighter.update_lighter()
- self.lighter.print_lighter()
- for car in self.cars:
- print()
- car.move(self)
- car.print_car(self)
- print('\n' * 2)
- class Lighter():
- def __init__(self, x, r, y, g, time):
- self.x = x
- self.redTime = r
- self.yelTime = y
- self.greTime = g
- self.time = 0
- self.update_time = time
- self.color = 'green'
- def print_lighter(self): # Отрисовка светофора
- if self.color == 'red':
- temp = '0'
- elif self.color == 'yellow':
- temp = '1'
- else:
- temp = '2'
- print(' '*self.x, temp, ' ' + 'TIME:', self.time, sep='')
- def update_lighter(self): # Обновление светофора
- self.time += self.update_time
- if self.color == 'red' and self.time == self.redTime:
- self.last_color = 'red'
- self.color = 'yellow'
- self.time = 0
- elif self.color == 'yellow' and self.time == self.yelTime:
- if self.last_color == 'red':
- self.color = 'green'
- else:
- self.color = 'red'
- self.time = 0
- elif self.color == 'green' and self.time == self.greTime:
- self.last_color = 'green'
- self.color = 'yellow'
- self.time = 0
- class Car():
- def __init__(self, car_id, velocity, carType):
- self.id = car_id
- self.vel = velocity
- self.type = carType
- self.x = 0
- def move(self, road):
- self.check_lighter(road)
- if self.x >= road.length:
- self.x = self.x - road.length
- def check_lighter(self, road):
- if road.lighter.x - self.vel <= self.x < road.lighter.x: # Провека
- if road.lighter.color == 'green':
- self.x += self.vel
- else:
- self.x += road.lighter.x - self.x - 1
- else:
- self.x += self.vel
- def print_car(self, road): # Отрисовка машины и полосы
- for i in range(road.length):
- if self.x == i:
- print(self.type, end='')
- else:
- print('_', end='')
- self.print_stat()
- def print_stat(self):
- print(' ', end='')
- print('ID:', self.id, end=' ')
- print('VEL:', self.vel, end=' ')
- print('CORD:', self.x)
- print()
- def main():
- roads = []
- for i in range(ROAD_COUNT):
- cars = []
- for j in range(ROAD_WIDTH):
- cars.append(Car(str(i) + str(j), 2*(j+1), CAR_TYPES[j%3]))
- lighter = Lighter(ROAD_LENGTH//2+1, RED_TIME, YELLOW_TIME, GREEN_TIME, TIME)
- roads.append(Road(i,ROAD_LENGTH, ROAD_WIDTH, cars, lighter))
- while True:
- for road in roads:
- data = 'ROAD_ID: ' + str(road.id) + ' '*3 + 'ROAD_LEN: ' + str(road.length) + ' '*3 + 'UPDATE_TIME: ' + str(TIME) + ' '*3 + 'LIGHTER_POS: ' + str(road.lighter.x)
- print(data.center(ROAD_LENGTH))
- road.print_road()
- sleep(TIME)
- os.system('cls||clear')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement