-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (58 loc) · 2.19 KB
/
main.py
File metadata and controls
73 lines (58 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pygame
from map_gernator import generate_random_bus_positions,generate_random_passenger,generate_square_grid_map
from Graphic_Engine import Graphic_Engine
from Simulation_Engine import Simulation_Engine
from video_recorder import Recorder
RECORD_FLAG = True
VIDEO_OUT_FILE = "Simulation.avi"
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
TITLE = "Bus Route Simulation"
NUMBER_OF_PASSENGERS = 200
NUMBER_OF_BUSSES = 10
GRID_SIZE = 10 #10x10 grid
GRID_DISTANCE = 10
BUS_SPEED = 0.3
CLOSE_AFTER_SIMULATION = True
PASSENGER_ADD_INTERVAL = 10_000
NODE_COUNT = GRID_SIZE*GRID_SIZE
def main():
pygame.init()
size = (SCREEN_WIDTH, SCREEN_HEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption(TITLE)
running = True
clock = pygame.time.Clock()
connections,positions = generate_square_grid_map(GRID_SIZE,GRID_DISTANCE,SCREEN_WIDTH,SCREEN_HEIGHT)
simulation = Simulation_Engine(connections)
for pas in generate_random_passenger(NUMBER_OF_PASSENGERS,NODE_COUNT):
simulation.add_passenger(pas)
for bus in generate_random_bus_positions(NUMBER_OF_BUSSES,NODE_COUNT,BUS_SPEED):
simulation.add_bus(bus)
gfx = Graphic_Engine(positions,connections,screen)
recorder =None
if(RECORD_FLAG):
recorder = Recorder(SCREEN_WIDTH,SCREEN_HEIGHT,60,VIDEO_OUT_FILE)
passenger_added_time = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if(simulation.finished()):
running = False
continue
gfx.draw_all(simulation.buses,simulation.passengers)
simulation.run_iteration()
pygame.display.flip()
if(recorder is not None):
recorder.record_frame(screen)
clock.tick(60)
if(simulation.simulation_time() - passenger_added_time > PASSENGER_ADD_INTERVAL):
passenger_added_time = simulation.simulation_time()
simulation.add_passenger(generate_random_passenger(1,NODE_COUNT)[0])
if(recorder is not None):
recorder.end_recording()
print(f"The simulations ran for {simulation.simulation_time()}")
pygame.quit()
if __name__ == "__main__":
main()