Advertisement
fevzi02

шарики

Nov 11th, 2021
3,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. import math
  2. import random
  3. import pygame
  4. import sys
  5. class Game:
  6.     def __init__(self, number):
  7.         color_list = ["WHITE", "RED", "GREEN", "BLUE", "DARKGRAY", "PUPLE"]
  8.         self.list_class_instance = []
  9.         for numb in range(number):
  10.             x0 = random.randint(61,1080-61)
  11.             y0 = random.randint(61,720-61)
  12.             self.list_class_instance.append(Boll(x0, y0, color_list[random.randint(0,len(color_list)-1)] ))
  13.         #--------------------------------------------------------------------------------------------------
  14.         pygame.init()
  15.  
  16.         screen = pygame.display.set_mode((1080, 720))
  17.         clock = pygame.time.Clock()
  18.  
  19.         while True:
  20.             screen.fill((78, 74, 59))
  21.             for lsi in self.list_class_instance:
  22.                 pygame.draw.circle(screen, lsi.color, lsi.pos(), lsi.radius, width=0)
  23.                 if lsi.y <= lsi.radius+1 :
  24.                     lsi.setVector("y")
  25.                 if lsi.y >= 720-lsi.radius-1 :
  26.                     lsi.setVector("y")
  27.                 if lsi.x <= lsi.radius+1  :
  28.                     lsi.setVector("x")
  29.                 if lsi.x >= 1080-lsi.radius-1 :
  30.                     lsi.setVector("x")
  31.                 lsi.x += lsi.vector()[0]
  32.                 lsi.y += lsi.vector()[1]
  33.             self.exam()
  34.             for event in pygame.event.get():
  35.                 if event.type == pygame.QUIT:
  36.                     pygame.quit()
  37.                     sys.exit()
  38.             pygame.display.flip()
  39.             clock.tick(60)
  40.  
  41.     def exam(self):
  42.         self.dell_Boll()
  43.         for i in range(len(self.list_class_instance)):
  44.             for j in range(len(self.list_class_instance)):
  45.                 bool = False
  46.                 if self.list_class_instance[i] != self.list_class_instance[j]:
  47.                     crossing_distance = self.list_class_instance[i].radius + self.list_class_instance[j].radius
  48.                     if self.list_class_instance[i].pos()[0] == self.list_class_instance[j].pos()[0]:
  49.                         distance = abs(self.list_class_instance[i].pos()[1] - self.list_class_instance[j].pos()[1])
  50.                         if distance == self.list_class_instance[i].radius + self.list_class_instance[j].radius:
  51.                             bool = True
  52.                     elif self.list_class_instance[i].pos()[1] == self.list_class_instance[j].pos()[1]:
  53.                         distance = abs(self.list_class_instance[i].pos()[0] - self.list_class_instance[j].pos()[0])
  54.                         if distance == crossing_distance:
  55.                             bool = True
  56.                     else:
  57.                         x = self.list_class_instance[i].pos()[0] - self.list_class_instance[j].pos()[0]
  58.                         y = self.list_class_instance[i].pos()[1] - self.list_class_instance[j].pos()[1]
  59.                         distance = round((x ** 2 + y ** 2) ** 0.5)
  60.                         if distance <= crossing_distance:
  61.                             bool = True
  62.                     if bool:
  63.                         self.list_class_instance[i].setRadiusminus()
  64.                         self.list_class_instance[j].setRadiusminus()
  65.  
  66.     def dell_Boll(self):
  67.         for lsi in self.list_class_instance:
  68.             if lsi.radius <= 1:
  69.                 self.list_class_instance.remove(lsi)
  70.                 del lsi
  71.  
  72. class Boll:
  73.     def __init__(self, x0=360, y0=540, color_key="RED", radius=60):
  74.         self.starts_x = [-2, -1, 1, 2]
  75.         self.starts_y = -2
  76.         random.shuffle(self.starts_x)
  77.         self.radius = radius
  78.         self.x = x0
  79.         self.y = y0
  80.         color_dict = { "WHITE" : (255, 255, 255),
  81.             "RED" : (220, 0, 0), "GREEN" : (0, 220, 0),
  82.             "BLUE" : (0, 0, 225), "DARKGRAY" : (40, 40, 40),
  83.             "PUPLE" : (120, 0, 120) }
  84.         self.color = (color_dict[color_key])
  85.  
  86.     def vector(self):
  87.         return (self.starts_x[0], self.starts_y)
  88.  
  89.     def setVector(self, x_y):
  90.         if x_y == "y":
  91.             self.starts_y =- self.starts_y
  92.         if x_y == "x":
  93.             self.starts_x[0] =- self.starts_x[0]
  94.  
  95.     def pos(self):
  96.         return (self.x, self.y)
  97.  
  98.     def setRadiusminus(self):
  99.         self.radius -= 10
  100.  
  101.     def setX(self, x):
  102.         self.x = x
  103.  
  104.     def setY(self, y):
  105.         self.y = y
  106.  
  107.  
  108. Game(100)
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement