Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- from pygame.sprite import Group, Sprite
- class Entity(Sprite):
- def __init__(self, image, position, anchor):
- super().__init__()
- self.image = image
- self.rect = image.get_rect(**{anchor: position})
- self.center = pygame.Vector2(self.rect.center)
- self.vector = pygame.Vector2()
- self._random()
- def _random(self):
- self.vector.from_polar((1, random.randint(0, 360)))
- self.velocity = random.randrange(60, 180)
- def update(self, delta, area):
- self.center += self.vector * delta * self.velocity
- self.rect.center = self.center
- clamp = self.rect.clamp(area)
- # Boundry Collision
- if clamp != self.rect:
- if clamp.x != self.rect.x:
- self.vector.x = -self.vector.x
- if clamp.y != self.rect.y:
- self.vector.y = -self.vector.y
- self.rect = clamp
- self.center = pygame.Vector2(self.rect.center)
- class Scene:
- def __init__(self, display):
- self.display = display
- def on_draw(self, surface):
- pass
- def on_event(self, event):
- pass
- def on_update(self, delta):
- pass
- class Display:
- def __init__(self, caption, width, height, flags=0, fps=60):
- pygame.display.set_caption(caption)
- self.display = pygame.display.set_mode((width, height), flags)
- self.rect = self.display.get_rect()
- self.clock = pygame.time.Clock()
- self.running = True
- self.delta = 0
- self.fps = fps
- self.scene = Scene(self)
- def loop(self):
- while self.running:
- for event in pygame.event.get():
- self.scene.on_event(event)
- self.scene.on_update(self.delta)
- self.scene.on_draw(self.display)
- pygame.display.flip()
- # Using delta time for smooth movement.
- self.delta = self.clock.tick(self.fps) / 1000
- class ImageHandler:
- def __init__(self):
- self.radius = 10
- self.nodes = self.create_nodes()
- def create_nodes(self):
- colors = self.get_colors()
- nodes = []
- transparent = 0, 0, 0, 0
- size = self.radius * 2, self.radius * 2
- surface = pygame.Surface(size, pygame.SRCALPHA)
- surface.fill(transparent)
- for color in colors:
- pygame.draw.circle(surface, color, (self.radius, self.radius), self.radius)
- nodes.append(surface.copy())
- return nodes
- def get_colors(self):
- dark = 80
- colors = []
- for color in pygame.color.THECOLORS:
- pcolor = pygame.Color(color)
- if pcolor.a > dark and pcolor.g > dark and pcolor.b > dark:
- if not color.startswith('grey') and not color.startswith('gray'):
- colors.append(color)
- random.shuffle(colors)
- return colors
- class Main(Scene):
- def __init__(self, display):
- super().__init__(display)
- self.image = ImageHandler()
- self.nodes = Group()
- self.setup(20)
- def setup(self, count):
- count = min(count, len(self.image.nodes))
- self.nodes.empty()
- rad = self.image.radius * 2
- for i in range(count):
- position = (random.randint(rad, self.display.rect.w - rad),
- random.randint(rad, self.display.rect.h - rad))
- self.nodes.add(Entity(self.image.nodes[i], position, 'center'))
- def draw_lines(self, surface):
- rnd = random.randrange
- for i, inode in enumerate(self.nodes):
- nearest = None
- min_dist = float('inf')
- for j, jnode in enumerate(self.nodes):
- if i != j:
- dist = inode.center.distance_to(jnode.center)
- if dist < min_dist:
- min_dist = dist
- nearest = jnode
- line_color = pygame.Color(rnd(0, 255), rnd(0, 255), rnd(0, 255), 255)
- if min_dist <= 255:
- line_color.a = int(255 - min_dist)
- if min_dist <= 1000:
- pygame.draw.line(surface, line_color, inode.rect.center, nearest.rect.center)
- def on_draw(self, surface):
- surface.fill('black')
- # Draw all lines first
- self.draw_lines(surface)
- # Draw all circles
- self.nodes.draw(surface)
- def on_event(self, event):
- if event.type == pygame.QUIT:
- self.display.running = False
- def on_update(self, delta):
- self.nodes.update(delta, self.display.rect)
- def main():
- pygame.init()
- display = Display("Example", 800, 600)
- main = Main(display)
- display.scene = main
- display.loop()
- pygame.quit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement