Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- import pygame
- import sys
- class Game:
- def __init__(self, number):
- color_list = ["WHITE", "RED", "GREEN", "BLUE", "DARKGRAY", "PUPLE"]
- self.list_class_instance = []
- for numb in range(number):
- x0 = random.randint(61,1080-61)
- y0 = random.randint(61,720-61)
- self.list_class_instance.append(Boll(x0, y0, color_list[random.randint(0,len(color_list)-1)] ))
- #--------------------------------------------------------------------------------------------------
- pygame.init()
- screen = pygame.display.set_mode((1080, 720))
- clock = pygame.time.Clock()
- while True:
- screen.fill((78, 74, 59))
- for lsi in self.list_class_instance:
- pygame.draw.circle(screen, lsi.color, lsi.pos(), lsi.radius, width=0)
- if lsi.y <= lsi.radius+1 :
- lsi.setVector("y")
- if lsi.y >= 720-lsi.radius-1 :
- lsi.setVector("y")
- if lsi.x <= lsi.radius+1 :
- lsi.setVector("x")
- if lsi.x >= 1080-lsi.radius-1 :
- lsi.setVector("x")
- lsi.x += lsi.vector()[0]
- lsi.y += lsi.vector()[1]
- self.exam()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- pygame.display.flip()
- clock.tick(60)
- def exam(self):
- self.dell_Boll()
- for i in range(len(self.list_class_instance)):
- for j in range(len(self.list_class_instance)):
- bool = False
- if self.list_class_instance[i] != self.list_class_instance[j]:
- crossing_distance = self.list_class_instance[i].radius + self.list_class_instance[j].radius
- if self.list_class_instance[i].pos()[0] == self.list_class_instance[j].pos()[0]:
- distance = abs(self.list_class_instance[i].pos()[1] - self.list_class_instance[j].pos()[1])
- if distance == self.list_class_instance[i].radius + self.list_class_instance[j].radius:
- bool = True
- elif self.list_class_instance[i].pos()[1] == self.list_class_instance[j].pos()[1]:
- distance = abs(self.list_class_instance[i].pos()[0] - self.list_class_instance[j].pos()[0])
- if distance == crossing_distance:
- bool = True
- else:
- x = self.list_class_instance[i].pos()[0] - self.list_class_instance[j].pos()[0]
- y = self.list_class_instance[i].pos()[1] - self.list_class_instance[j].pos()[1]
- distance = round((x ** 2 + y ** 2) ** 0.5)
- if distance <= crossing_distance:
- bool = True
- if bool:
- self.list_class_instance[i].setRadiusminus()
- self.list_class_instance[j].setRadiusminus()
- def dell_Boll(self):
- for lsi in self.list_class_instance:
- if lsi.radius <= 1:
- self.list_class_instance.remove(lsi)
- del lsi
- class Boll:
- def __init__(self, x0=360, y0=540, color_key="RED", radius=60):
- self.starts_x = [-2, -1, 1, 2]
- self.starts_y = -2
- random.shuffle(self.starts_x)
- self.radius = radius
- self.x = x0
- self.y = y0
- color_dict = { "WHITE" : (255, 255, 255),
- "RED" : (220, 0, 0), "GREEN" : (0, 220, 0),
- "BLUE" : (0, 0, 225), "DARKGRAY" : (40, 40, 40),
- "PUPLE" : (120, 0, 120) }
- self.color = (color_dict[color_key])
- def vector(self):
- return (self.starts_x[0], self.starts_y)
- def setVector(self, x_y):
- if x_y == "y":
- self.starts_y =- self.starts_y
- if x_y == "x":
- self.starts_x[0] =- self.starts_x[0]
- def pos(self):
- return (self.x, self.y)
- def setRadiusminus(self):
- self.radius -= 10
- def setX(self, x):
- self.x = x
- def setY(self, y):
- self.y = y
- Game(100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement