Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import random
  2. import pylab
  3. import pygame
  4. import numpy as np
  5.  
  6. BLACK = (0, 0, 0)
  7. WHITE = (255, 255, 255)
  8. GREY = (128, 128, 128)
  9. YELLOW = (255, 255, 0)
  10. WIDTH = 12
  11. HEIGHT = 12
  12. MARGIN = 3
  13.  
  14.  
  15. def initialize_model_matrix():
  16. matrix = np.zeros((30, 50))
  17. matrix[0, 0:50] = 2
  18. matrix[-1, 0:50] = 2
  19. return matrix
  20.  
  21.  
  22. def set_people(matrix, count):
  23. people_count = count
  24. random_dict = {}
  25. for i in range(1, 29):
  26. random_dict[i] = []
  27. while people_count > 0:
  28. random_row = random.randint(1, 28)
  29. random_col = random.randint(0, 47)
  30. if random_col not in random_dict[random_row]:
  31. matrix[random_row, random_col] = 1
  32. random_dict[random_row].append(random_col)
  33. people_count -= 1
  34.  
  35.  
  36. def set_walls(matrix, offset):
  37. matrix[offset, 48:50] = 2
  38. matrix[29 - offset, 48:50] = 2
  39.  
  40.  
  41. def check_people(matrix):
  42. for i in range(1, 29, 1):
  43. for j in range(50):
  44. if matrix[i][j] == 1:
  45. return True
  46. return False
  47.  
  48.  
  49. def move_people(matrix, screen, clock):
  50. iterations_result = 0
  51. while check_people(matrix):
  52. iterations_result += 1
  53.  
  54. for is_move in [False, True]:
  55. for i in range(1, 29, 1):
  56. for j in range(49, -1, -1):
  57. if is_move:
  58. if matrix[i][j] == 3:
  59. matrix[i][j] = 0
  60. elif matrix[i][j] in [4, 5, 7]:
  61. matrix[i][j] = 1
  62. elif matrix[i][j] == 1:
  63. if j + 1 == 50:
  64. matrix[i][j] = 0
  65. continue
  66. else:
  67. if (j + 1 < 50) and matrix[i][j] == 1:
  68. if matrix[i][j + 1] in [0, 3]:
  69. matrix[i][j] = 3
  70. matrix[i][j + 1] = 7
  71. elif (matrix[i + 1][j] in [0, 3]) and not (matrix[i - 1][j] in [0, 3]):
  72. matrix[i][j] = 3
  73. matrix[i + 1][j] = 5
  74. elif not (matrix[i + 1][j] in [0, 3]) and (matrix[i - 1][j] in [0, 3]):
  75. matrix[i][j] = 3
  76. matrix[i - 1][j] = 4
  77. elif (matrix[i + 1][j] in [0, 3]) and (matrix[i - 1][j] in [0, 3]):
  78. matrix[i][j] = 3
  79. random_index = random.choice([-1, 1])
  80. if random_index == -1:
  81. matrix[i + random_index][j] = 4
  82. else:
  83. matrix[i + random_index][j] = 5
  84. screen.fill(BLACK)
  85. for i in range(30):
  86. for j in range(50):
  87. color = WHITE
  88. if matrix[i][j] == 1:
  89. color = YELLOW
  90. if matrix[i][j] == 2:
  91. color = GREY
  92. pygame.draw.rect(screen, color,
  93. [(MARGIN + WIDTH) * j + MARGIN, (MARGIN + HEIGHT) * i + MARGIN, WIDTH, HEIGHT])
  94. clock.tick(10)
  95. pygame.display.flip()
  96. return iterations_result
  97.  
  98. offset = []
  99. iterations = []
  100. for i in range(1, 15, 1):
  101. pygame.init()
  102. WINDOW_SIZE = [750, 450]
  103. screen = pygame.display.set_mode(WINDOW_SIZE)
  104. pygame.display.set_caption("Crowd simulation")
  105. clock = pygame.time.Clock()
  106. model_matrix = initialize_model_matrix()
  107. set_people(model_matrix, 500)
  108. current_matrix = np.copy(model_matrix)
  109. set_walls(current_matrix, i)
  110. iterations.append(move_people(current_matrix, screen, clock))
  111. offset.append(i)
  112. pylab.plot(offset, iterations)
  113. pylab.show()
  114. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement