Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import pygame, random, math
  2.  
  3. WHITE = (255,255,255)
  4. BLACK = (0,0,0)
  5. SCREENSIZE = (500,500)
  6.  
  7. pygame.init()
  8. screen = pygame.display.set_mode(SCREENSIZE)
  9. clock = pygame.time.Clock()
  10.  
  11. list_of_points = []
  12.  
  13. def checkPointCollisions():
  14. for i in range(0, len(list_of_points)):
  15. for j in range(i+1, len(list_of_points)):
  16. if list_of_points[i] == 0 or list_of_points[j] == 0:
  17. continue
  18. if abs(list_of_points[i].coordinates[0] - list_of_points[j].coordinates[0]) < list_of_points[i].size + list_of_points[j].size and abs(list_of_points[i].coordinates[1] - list_of_points[j].coordinates[1]) < list_of_points[i].size + list_of_points[j].size:
  19. new_coordinates = [(list_of_points[i].coordinates[0] + list_of_points[j].coordinates[0] / 2), (list_of_points[i].coordinates[1] + list_of_points[j].coordinates[1] / 2)]
  20. new_point = Point(new_coordinates,
  21. [(list_of_points[i].speed[0] + list_of_points[j].speed[0])/2, (list_of_points[i].speed[1] + list_of_points[j].speed[1])/2],
  22. list_of_points[i].color,
  23. list_of_points[i].size + list_of_points[j].size)
  24.  
  25. list_of_points[i] = 0
  26. list_of_points[j] = 0
  27. list_of_points.append(new_point)
  28.  
  29.  
  30.  
  31.  
  32. class Point(object):
  33. def __init__(self, coordinates, speed, color, size):
  34. self.coordinates = coordinates
  35. self.speed = speed
  36. self.color = color
  37. self.size = size
  38.  
  39. def checkCollision(self):
  40. if SCREENSIZE[0] - self.coordinates[0] - self.size < 5 or self.coordinates[0] < 5:
  41. self.speed[0] = self.speed[0] * -1
  42. if SCREENSIZE[1] - self.coordinates[1] - self.size < 5 or self.coordinates[1] < 5:
  43. self.speed[1] = self.speed[1] * -1
  44.  
  45. def createPoint(n):
  46. for i in range(n):
  47. random_coordinates = [random.randint(0,SCREENSIZE[0]),
  48. random.randint(0,SCREENSIZE[1])]
  49. random_speed = [random.randint(-5,5), random.randint(-5,5)]
  50. if random_speed[0] == 0:
  51. random_speed[0] += 1
  52. elif random_speed[1] == 0:
  53. random_speed[1] += 1
  54. random_color = (random.randint(0,255),
  55. random.randint(0,255),
  56. random.randint(0,255))
  57. random_size = random.randint(3, 10)
  58. list_of_points.append(Point(random_coordinates, random_speed, random_color, random_size))
  59.  
  60. while True:
  61. screen.fill(BLACK)
  62.  
  63. for event in pygame.event.get():
  64. if event.type == pygame.MOUSEBUTTONDOWN:
  65. createPoint(1)
  66.  
  67. checkPointCollisions()
  68. for point in list_of_points:
  69. if point == 0:
  70. continue
  71. else:
  72. point.checkCollision()
  73. point.coordinates[0] += point.speed[0]
  74. point.coordinates[1] += point.speed[1]
  75.  
  76. for point in list_of_points:
  77. if point != 0:
  78. pygame.draw.rect(screen,
  79. point.color,
  80. (point.coordinates[0], point.coordinates[1],
  81. point.size, point.size))
  82.  
  83. pygame.display.update()
  84. clock.tick(60)
Add Comment
Please, Sign In to add comment