Advertisement
Guest User

Untitled

a guest
Apr 24th, 2025
24
0
28 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import pygame
  2. import random
  3. import math
  4. import time
  5.  
  6. # === Настройки ===
  7. WIDTH, HEIGHT = 640, 640
  8. NUM_MOLDS = 8000
  9. ROT_ANGLE = math.pi / 4
  10. SENSOR_ANGLE = math.pi / 4
  11. SENSOR_DIST = 10
  12.  
  13. # === Функции ===
  14. def luminance(color):
  15.     return sum(color[:3]) / (3 * 255)
  16.  
  17. # === Класс Молда ===
  18. class Mold:
  19.     def __init__(self):
  20.         self.x = random.uniform(WIDTH/2 - 20, WIDTH/2 + 20)
  21.         self.y = random.uniform(HEIGHT/2 - 20, HEIGHT/2 + 20)
  22.         self.heading = random.uniform(0, 2 * math.pi)
  23.         hue = self.heading / (2 * math.pi)
  24.         r = 127 + 127 * math.sin(2 * math.pi * hue)
  25.         g = 127 + 127 * math.sin(2 * math.pi * hue + 2)
  26.         b = 127 + 127 * math.sin(2 * math.pi * hue + 4)
  27.         self.color = (int(r), int(g), int(b))
  28.         self.rot_angle = ROT_ANGLE
  29.         self.sensor_angle = SENSOR_ANGLE
  30.         self.sensor_dist = SENSOR_DIST
  31.  
  32.     def get_sensor_pos(self, angle):
  33.         x = (self.x + self.sensor_dist * math.cos(angle)) % WIDTH
  34.         y = (self.y + self.sensor_dist * math.sin(angle)) % HEIGHT
  35.         return int(x), int(y)
  36.  
  37.     def update(self, surface):
  38.         self.x = (self.x + math.cos(self.heading)) % WIDTH
  39.         self.y = (self.y + math.sin(self.heading)) % HEIGHT
  40.  
  41.         rx, ry = self.get_sensor_pos(self.heading + self.sensor_angle)
  42.         lx, ly = self.get_sensor_pos(self.heading - self.sensor_angle)
  43.         fx, fy = self.get_sensor_pos(self.heading)
  44.  
  45.         r = luminance(surface.get_at((rx, ry)))
  46.         l = luminance(surface.get_at((lx, ly)))
  47.         f = luminance(surface.get_at((fx, fy)))
  48.  
  49.         if f < l and f < r:
  50.             self.heading += self.rot_angle if random.random() < 0.5 else -self.rot_angle
  51.         elif l > r:
  52.             self.heading -= self.rot_angle
  53.         elif r > l:
  54.             self.heading += self.rot_angle
  55.  
  56.     def draw(self, surface):
  57.         surface.set_at((int(self.x), int(self.y)), self.color)
  58.  
  59. # === Основной цикл ===
  60. def main():
  61.     pygame.init()
  62.     screen = pygame.display.set_mode((WIDTH, HEIGHT))
  63.     pygame.display.set_caption("Colorful Slime Mold")
  64.     clock = pygame.time.Clock()
  65.     canvas = pygame.Surface((WIDTH, HEIGHT))
  66.     canvas.fill((0, 0, 0))
  67.  
  68.     molds = [Mold() for _ in range(NUM_MOLDS)]
  69.  
  70.     running = True
  71.     while running:
  72.         screen.fill((0, 0, 0))
  73.         fade = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
  74.         fade.fill((0, 0, 0, 5))  # Полупрозрачная "затухающая" маска
  75.         canvas.blit(fade, (0, 0))
  76.  
  77.         for event in pygame.event.get():
  78.             if event.type == pygame.QUIT:
  79.                 running = False
  80.  
  81.         for mold in molds:
  82.             mold.update(canvas)
  83.             mold.draw(canvas)
  84.  
  85.         screen.blit(canvas, (0, 0))
  86.         pygame.display.flip()
  87.         clock.tick(60)
  88.  
  89.     pygame.quit()
  90.  
  91. if __name__ == "__main__":
  92.     random.seed(time.time())
  93.     main()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement