Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import math
- import time
- # === Настройки ===
- WIDTH, HEIGHT = 640, 640
- NUM_MOLDS = 8000
- ROT_ANGLE = math.pi / 4
- SENSOR_ANGLE = math.pi / 4
- SENSOR_DIST = 10
- # === Функции ===
- def luminance(color):
- return sum(color[:3]) / (3 * 255)
- # === Класс Молда ===
- class Mold:
- def __init__(self):
- self.x = random.uniform(WIDTH/2 - 20, WIDTH/2 + 20)
- self.y = random.uniform(HEIGHT/2 - 20, HEIGHT/2 + 20)
- self.heading = random.uniform(0, 2 * math.pi)
- hue = self.heading / (2 * math.pi)
- r = 127 + 127 * math.sin(2 * math.pi * hue)
- g = 127 + 127 * math.sin(2 * math.pi * hue + 2)
- b = 127 + 127 * math.sin(2 * math.pi * hue + 4)
- self.color = (int(r), int(g), int(b))
- self.rot_angle = ROT_ANGLE
- self.sensor_angle = SENSOR_ANGLE
- self.sensor_dist = SENSOR_DIST
- def get_sensor_pos(self, angle):
- x = (self.x + self.sensor_dist * math.cos(angle)) % WIDTH
- y = (self.y + self.sensor_dist * math.sin(angle)) % HEIGHT
- return int(x), int(y)
- def update(self, surface):
- self.x = (self.x + math.cos(self.heading)) % WIDTH
- self.y = (self.y + math.sin(self.heading)) % HEIGHT
- rx, ry = self.get_sensor_pos(self.heading + self.sensor_angle)
- lx, ly = self.get_sensor_pos(self.heading - self.sensor_angle)
- fx, fy = self.get_sensor_pos(self.heading)
- r = luminance(surface.get_at((rx, ry)))
- l = luminance(surface.get_at((lx, ly)))
- f = luminance(surface.get_at((fx, fy)))
- if f < l and f < r:
- self.heading += self.rot_angle if random.random() < 0.5 else -self.rot_angle
- elif l > r:
- self.heading -= self.rot_angle
- elif r > l:
- self.heading += self.rot_angle
- def draw(self, surface):
- surface.set_at((int(self.x), int(self.y)), self.color)
- # === Основной цикл ===
- def main():
- pygame.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Colorful Slime Mold")
- clock = pygame.time.Clock()
- canvas = pygame.Surface((WIDTH, HEIGHT))
- canvas.fill((0, 0, 0))
- molds = [Mold() for _ in range(NUM_MOLDS)]
- running = True
- while running:
- screen.fill((0, 0, 0))
- fade = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
- fade.fill((0, 0, 0, 5)) # Полупрозрачная "затухающая" маска
- canvas.blit(fade, (0, 0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- for mold in molds:
- mold.update(canvas)
- mold.draw(canvas)
- screen.blit(canvas, (0, 0))
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- random.seed(time.time())
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement