jimkilled

CARS V2

Oct 15th, 2020
830
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # CARS main.py
  2. from time import sleep
  3. from config import *
  4. import os
  5.  
  6.  
  7. class Road():
  8.     def __init__(self, road_id, length, width, cars, lighter):
  9.         self.id = road_id
  10.         self.length = length
  11.         self.width = width
  12.         self.cars = cars
  13.         self.lighter = lighter
  14.  
  15.     def print_road(self):
  16.         self.lighter.update_lighter()
  17.         self.lighter.print_lighter()
  18.         for car in self.cars:
  19.             print()
  20.             car.move(self)
  21.             car.print_car(self)
  22.         print('\n' * 2)
  23.  
  24.  
  25. class Lighter():
  26.     def __init__(self, x, r, y, g, time):
  27.         self.x = x
  28.         self.redTime = r
  29.         self.yelTime = y
  30.         self.greTime = g
  31.         self.time = 0
  32.         self.update_time = time
  33.         self.color = 'green'
  34.  
  35.     def print_lighter(self):                    # Отрисовка светофора
  36.         if self.color == 'red':
  37.             temp = '0'
  38.         elif self.color == 'yellow':
  39.             temp = '1'
  40.         else:
  41.             temp = '2'
  42.         print(' '*self.x, temp, '      TIME:', self.time, sep='')
  43.  
  44.     def update_lighter(self):                   # Обновление светофора
  45.         self.time += self.update_time
  46.         if self.color == 'red' and self.time == self.redTime:
  47.             self.last_color = 'red'
  48.             self.color = 'yellow'
  49.             self.time = 0
  50.  
  51.         elif self.color == 'yellow' and self.time == self.yelTime:
  52.             if self.last_color == 'red':
  53.                 self.color = 'green'
  54.             else:
  55.                 self.color = 'red'
  56.             self.time = 0
  57.  
  58.         elif self.color == 'green' and self.time == self.greTime:
  59.             self.last_color = 'green'
  60.             self.color = 'yellow'
  61.             self.time = 0
  62.  
  63.  
  64. class Car():
  65.     def __init__(self, car_id, velocity, carType):
  66.         self.id = car_id
  67.         self.vel = velocity
  68.         self.type = carType
  69.         self.x = 0
  70.  
  71.     def move(self, road):
  72.         self.check_lighter(road)
  73.         if self.x >= road.length:
  74.             self.x = self.x - road.length
  75.  
  76.     def check_lighter(self, road):
  77.         if road.lighter.x - self.vel <= self.x < road.lighter.x:      # Провека
  78.             if road.lighter.color == 'green':
  79.                 self.x += self.vel
  80.             else:
  81.                 self.x += road.lighter.x - self.x - 1
  82.         else:
  83.             self.x += self.vel
  84.  
  85.     def print_car(self, road):                        # Отрисовка машины и полосы
  86.         for i in range(road.length):
  87.             if self.x == i:
  88.                 print(self.type, end='')
  89.             else:
  90.                 print('_', end='')
  91.         self.print_stat()
  92.  
  93.     def print_stat(self):
  94.         print('    ', end='')
  95.         print('ID:', self.id, end='  ')
  96.         print('VEL:', self.vel, end='  ')
  97.         print('CORD:', self.x)
  98.         print()
  99.  
  100.  
  101. def main():
  102.     roads = []
  103.     for i in range(ROAD_COUNT):
  104.         cars = []
  105.         for j in range(ROAD_WIDTH):
  106.             cars.append(Car(str(i) + str(j), 2*(j+1), CAR_TYPES[j%3]))
  107.         lighter = Lighter(ROAD_LENGTH//2+1, RED_TIME, YELLOW_TIME, GREEN_TIME, TIME)
  108.         roads.append(Road(i,ROAD_LENGTH, ROAD_WIDTH, cars, lighter))
  109.  
  110.     while True:
  111.         for road in roads:
  112.             data = 'ROAD_ID: ' + str(road.id) + ' '*3 + 'ROAD_LEN: ' + str(road.length) + ' '*3 + 'UPDATE_TIME: ' + str(TIME) + ' '*3 + 'LIGHTER_POS: ' + str(road.lighter.x)
  113.             print(data.center(ROAD_LENGTH))
  114.             road.print_road()
  115.         sleep(TIME)
  116.         os.system('cls||clear')
  117. main()
RAW Paste Data