Skip to content

Instantly share code, notes, and snippets.

@Stovoy
Created July 19, 2024 03:44
Show Gist options
  • Select an option

  • Save Stovoy/d5a47e71c5420b204e289ffc6dd89f13 to your computer and use it in GitHub Desktop.

Select an option

Save Stovoy/d5a47e71c5420b204e289ffc6dd89f13 to your computer and use it in GitHub Desktop.
import math
import pygame
import random
class Ecosystem:
def __init__(self, width, height):
self.width = width
self.height = height
# 0 to 1, where 0 is barren and 1 is lush
self.activity = 0
self.time = 0
self.surface = pygame.Surface((width, height))
self.sky_colors = [
# Barren gray
(200, 200, 200),
# Lush blue
(135, 206, 235)
]
self.ground_colors = [
# Barren tan
(210, 180, 140),
# Lush green
(34, 139, 34)
]
self.plants = []
def update(self, dt):
self.time += dt
self.activity = min(1, max(0, self.activity + math.sin(self.time / 4) * dt))
for plant in self.plants:
plant.update(dt, self.activity)
if random.random() < self.activity * dt:
self.plants.append(Plant(random.randint(0, self.width), self.height))
self.plants = [plant for plant in self.plants if plant.alive]
def draw(self):
sky_color = self.interpolate_color(self.sky_colors[0], self.sky_colors[1], self.activity)
self.surface.fill(sky_color)
ground_color = self.interpolate_color(self.ground_colors[0], self.ground_colors[1], self.activity)
pygame.draw.rect(self.surface, ground_color, (0, self.height * 0.7, self.width, self.height * 0.3))
for plant in self.plants:
plant.draw(self.surface)
return self.surface
@staticmethod
def interpolate_color(color1, color2, t):
return tuple(int(a + (b - a) * t) for a, b in zip(color1, color2))
class Plant:
def __init__(self, x, y):
self.x = x
self.y = y
self.size = 0
self.max_size = random.randint(20, 50)
self.growth_rate = random.uniform(5, 15)
self.color = (0, random.randint(100, 200), 0)
self.alive = True
def update(self, dt, activity):
if self.size < self.max_size:
self.size += self.growth_rate * activity * dt
elif random.random() < (1 - activity) * 0.5 * dt:
self.alive = False
def draw(self, surface):
pygame.draw.circle(surface, self.color, (self.x, self.y), int(self.size))
def main():
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Discord Ecosystem Visualization")
clock = pygame.time.Clock()
ecosystem = Ecosystem(width, height)
running = True
while running:
delta = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
ecosystem.update(delta)
screen.blit(ecosystem.draw(), (0, 0))
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment